lemmy/src/lib.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

#![recursion_limit = "512"]
2021-03-25 19:30:15 +00:00
pub mod api_routes;
pub mod code_migrations;
pub mod root_span_builder;
pub mod scheduled_tasks;
2021-11-23 12:16:47 +00:00
use lemmy_utils::LemmyError;
use opentelemetry::{
sdk::{propagation::TraceContextPropagator, Resource},
KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
2021-11-23 12:16:47 +00:00
use tracing::subscriber::set_global_default;
use tracing_error::ErrorLayer;
use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
2021-11-23 12:16:47 +00:00
pub fn init_tracing(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
2021-11-23 12:16:47 +00:00
LogTracer::init()?;
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
2021-11-23 12:16:47 +00:00
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let format_layer = tracing_subscriber::fmt::layer();
2021-11-23 12:16:47 +00:00
let subscriber = Registry::default()
.with(env_filter)
.with(format_layer)
.with(ErrorLayer::default());
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);
let subscriber = subscriber.with(otel_layer);
set_global_default(subscriber)?;
} else {
set_global_default(subscriber)?;
}
2021-11-23 12:16:47 +00:00
Ok(())
}