]> Untitled Git - lemmy.git/commitdiff
Make opentelemetry dependency optional
authorFelix Ableitner <me@nutomic.com>
Tue, 10 May 2022 12:06:32 +0000 (14:06 +0200)
committerFelix Ableitner <me@nutomic.com>
Wed, 11 May 2022 12:29:01 +0000 (14:29 +0200)
.drone.yml
Cargo.toml
src/lib.rs
src/main.rs
src/telemetry.rs [new file with mode: 0644]

index b7d75eb0a1b4712e2901516413018605c3b9fefa..934297d5cac749b34d896556d4fff59fc7e6c050 100644 (file)
@@ -22,10 +22,15 @@ steps:
     commands:
       - /root/.cargo/bin/cargo fmt -- --check
 
-  - name: check lemmy_api_common with minimal deps
+  - name: check with different features
     image: clux/muslrust:1.60.0
     commands:
+      # api with minimal deps
       - cargo check -p lemmy_api_common
+      # opentelemetry console
+      - cargo check --features console
+      # default features
+      - cargo check
 
   - name: cargo clippy
     image: clux/muslrust:1.60.0
index cd5abaa3553e916c51ceeb3c792fe54943625dd0..117c1c3b6faa9ab50b2d4914dde328493ff5a61e 100644 (file)
@@ -14,7 +14,8 @@ doctest = false
 debug = 0
 
 [features]
-console = ["console-subscriber"]
+console = ["console-subscriber", "opentelemetry", "opentelemetry-otlp", "tracing-opentelemetry",
+    "reqwest-tracing/opentelemetry_0_16"]
 default = []
 
 [workspace]
@@ -54,14 +55,14 @@ tracing-actix-web = { version = "0.5.1", default-features = false }
 tracing-error = "0.2.0"
 tracing-log = "0.1.2"
 tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
-console-subscriber = { version = "0.1.3", optional = true }
 url = { version = "2.2.2", features = ["serde"] }
 reqwest = { version = "0.11.10", features = ["json"] }
 reqwest-middleware = "0.1.5"
-reqwest-tracing = { version = "0.2.1", features = ["opentelemetry_0_16"] }
+reqwest-tracing = "0.2.1"
 clokwerk = "0.3.5"
 doku = "0.11.0"
-opentelemetry = { version = "0.17.0", features = ["rt-tokio"] }
-opentelemetry-otlp = "0.10.0"
-tracing-opentelemetry = "0.17.2"
 parking_lot = "0.12.0"
+console-subscriber = { version = "0.1.3", optional = true }
+opentelemetry = { version = "0.17.0", features = ["rt-tokio"], optional = true }
+opentelemetry-otlp = { version = "0.10.0", optional = true }
+tracing-opentelemetry = { version = "0.17.2", optional = true }
index 7f19fa8e582e10efab01fa4e15e3b48ea195f9cc..1280934c936ced11f7e20441c169e2b2ae257c47 100644 (file)
@@ -3,25 +3,18 @@ pub mod api_routes;
 pub mod code_migrations;
 pub mod root_span_builder;
 pub mod scheduled_tasks;
-
 #[cfg(feature = "console")]
-use console_subscriber::ConsoleLayer;
+pub mod telemetry;
+
 use lemmy_utils::LemmyError;
-use opentelemetry::{
-  sdk::{propagation::TraceContextPropagator, Resource},
-  KeyValue,
-};
-use opentelemetry_otlp::WithExportConfig;
 use tracing::subscriber::set_global_default;
 use tracing_error::ErrorLayer;
 use tracing_log::LogTracer;
 use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
 
-pub fn init_tracing(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
+pub fn init_logging(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
   LogTracer::init()?;
 
-  opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
-
   let log_description = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
 
   let targets = log_description
@@ -31,41 +24,15 @@ pub fn init_tracing(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
 
   let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
 
-  #[cfg(feature = "console")]
-  let console_layer = ConsoleLayer::builder()
-    .with_default_env()
-    .server_addr(([0, 0, 0, 0], 6669))
-    .event_buffer_capacity(1024 * 1024)
-    .spawn();
-
   let subscriber = Registry::default()
     .with(format_layer)
     .with(ErrorLayer::default());
 
-  #[cfg(feature = "console")]
-  let subscriber = subscriber.with(console_layer);
-
-  if let Some(url) = opentelemetry_url {
-    let tracer = opentelemetry_otlp::new_pipeline()
-      .tracing()
-      .with_trace_config(
-        opentelemetry::sdk::trace::config()
-          .with_resource(Resource::new(vec![KeyValue::new("service.name", "lemmy")])),
-      )
-      .with_exporter(
-        opentelemetry_otlp::new_exporter()
-          .tonic()
-          .with_endpoint(url),
-      )
-      .install_batch(opentelemetry::runtime::Tokio)?;
-
-    let otel_layer = tracing_opentelemetry::layer()
-      .with_tracer(tracer)
-      .with_filter(targets);
-
-    let subscriber = subscriber.with(otel_layer);
-
-    set_global_default(subscriber)?;
+  if let Some(_url) = opentelemetry_url {
+    #[cfg(feature = "console")]
+    crate::telemetry::init_tracing(_url, subscriber, targets)?;
+    #[cfg(not(feature = "console"))]
+    tracing::error!("Feature `console` must be enabled for opentelemetry tracing");
   } else {
     set_global_default(subscriber)?;
   }
index f304a5fcc8649eabf91998e1ba677ef2a3326917..be7d028cd2ffd46c42791c053b3d19fb1b708a7b 100644 (file)
@@ -20,7 +20,7 @@ use lemmy_routes::{feeds, images, nodeinfo, webfinger};
 use lemmy_server::{
   api_routes,
   code_migrations::run_advanced_migrations,
-  init_tracing,
+  init_logging,
   root_span_builder::QuieterRootSpanBuilder,
   scheduled_tasks,
 };
@@ -54,7 +54,7 @@ async fn main() -> Result<(), LemmyError> {
 
   let settings = Settings::init().expect("Couldn't initialize settings.");
 
-  init_tracing(settings.opentelemetry_url.as_deref())?;
+  init_logging(settings.opentelemetry_url.as_deref())?;
 
   // Set up the r2d2 connection pool
   let db_url = match get_database_url_from_env() {
diff --git a/src/telemetry.rs b/src/telemetry.rs
new file mode 100644 (file)
index 0000000..9f8c9f3
--- /dev/null
@@ -0,0 +1,51 @@
+use console_subscriber::ConsoleLayer;
+use lemmy_utils::LemmyError;
+use opentelemetry::{
+  sdk::{propagation::TraceContextPropagator, Resource},
+  KeyValue,
+};
+use opentelemetry_otlp::WithExportConfig;
+use tracing::{subscriber::set_global_default, Subscriber};
+use tracing_subscriber::{filter::Targets, layer::SubscriberExt, registry::LookupSpan, Layer};
+
+pub fn init_tracing<S>(
+  opentelemetry_url: &str,
+  subscriber: S,
+  targets: Targets,
+) -> Result<(), LemmyError>
+where
+  S: Subscriber + for<'a> LookupSpan<'a> + Send + Sync + 'static,
+{
+  opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
+
+  let console_layer = ConsoleLayer::builder()
+    .with_default_env()
+    .server_addr(([0, 0, 0, 0], 6669))
+    .event_buffer_capacity(1024 * 1024)
+    .spawn();
+
+  let subscriber = subscriber.with(console_layer);
+
+  let tracer = opentelemetry_otlp::new_pipeline()
+    .tracing()
+    .with_trace_config(
+      opentelemetry::sdk::trace::config()
+        .with_resource(Resource::new(vec![KeyValue::new("service.name", "lemmy")])),
+    )
+    .with_exporter(
+      opentelemetry_otlp::new_exporter()
+        .tonic()
+        .with_endpoint(opentelemetry_url),
+    )
+    .install_batch(opentelemetry::runtime::Tokio)?;
+
+  let otel_layer = tracing_opentelemetry::layer()
+    .with_tracer(tracer)
+    .with_filter(targets);
+
+  let subscriber = subscriber.with(otel_layer);
+
+  set_global_default(subscriber)?;
+
+  Ok(())
+}