1 use console_subscriber::ConsoleLayer;
2 use lemmy_utils::LemmyError;
4 sdk::{propagation::TraceContextPropagator, Resource},
7 use opentelemetry_otlp::WithExportConfig;
8 use tracing::{subscriber::set_global_default, Subscriber};
9 use tracing_subscriber::{filter::Targets, layer::SubscriberExt, registry::LookupSpan, Layer};
11 pub fn init_tracing<S>(
12 opentelemetry_url: &str,
15 ) -> Result<(), LemmyError>
17 S: Subscriber + for<'a> LookupSpan<'a> + Send + Sync + 'static,
19 opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
21 let console_layer = ConsoleLayer::builder()
23 .server_addr(([0, 0, 0, 0], 6669))
24 .event_buffer_capacity(1024 * 1024)
27 let subscriber = subscriber.with(console_layer);
29 let tracer = opentelemetry_otlp::new_pipeline()
32 opentelemetry::sdk::trace::config()
33 .with_resource(Resource::new(vec![KeyValue::new("service.name", "lemmy")])),
36 opentelemetry_otlp::new_exporter()
38 .with_endpoint(opentelemetry_url),
40 .install_batch(opentelemetry::runtime::Tokio)?;
42 let otel_layer = tracing_opentelemetry::layer()
44 .with_filter(targets);
46 let subscriber = subscriber.with(otel_layer);
48 set_global_default(subscriber)?;