2019-08-14 02:52:43 +00:00
|
|
|
#![recursion_limit = "512"]
|
2021-03-25 19:30:15 +00:00
|
|
|
pub mod api_routes;
|
2020-07-10 18:15:41 +00:00
|
|
|
pub mod code_migrations;
|
2021-12-06 14:54:47 +00:00
|
|
|
pub mod root_span_builder;
|
2021-01-29 16:38:27 +00:00
|
|
|
pub mod scheduled_tasks;
|
2021-11-23 12:16:47 +00:00
|
|
|
|
2022-02-04 17:31:38 +00:00
|
|
|
#[cfg(feature = "console")]
|
2022-01-07 14:53:45 +00:00
|
|
|
use console_subscriber::ConsoleLayer;
|
2021-11-23 12:16:47 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
2022-01-06 19:10:20 +00:00
|
|
|
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;
|
2022-01-07 14:53:45 +00:00
|
|
|
use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
|
2021-11-23 12:16:47 +00:00
|
|
|
|
2022-01-06 19:10:20 +00:00
|
|
|
pub fn init_tracing(opentelemetry_url: Option<&str>) -> Result<(), LemmyError> {
|
2021-11-23 12:16:47 +00:00
|
|
|
LogTracer::init()?;
|
|
|
|
|
2022-01-06 19:10:20 +00:00
|
|
|
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
|
|
|
|
|
2022-01-07 14:53:45 +00:00
|
|
|
let log_description = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
|
|
|
|
|
|
|
|
let targets = log_description
|
|
|
|
.trim()
|
|
|
|
.trim_matches('"')
|
|
|
|
.parse::<Targets>()?;
|
|
|
|
|
|
|
|
let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
|
|
|
|
|
2022-02-04 17:31:38 +00:00
|
|
|
#[cfg(feature = "console")]
|
2022-01-07 14:53:45 +00:00
|
|
|
let console_layer = ConsoleLayer::builder()
|
|
|
|
.with_default_env()
|
|
|
|
.server_addr(([0, 0, 0, 0], 6669))
|
|
|
|
.event_buffer_capacity(1024 * 1024)
|
|
|
|
.spawn();
|
2021-11-23 12:16:47 +00:00
|
|
|
|
|
|
|
let subscriber = Registry::default()
|
|
|
|
.with(format_layer)
|
2022-02-04 17:31:38 +00:00
|
|
|
.with(ErrorLayer::default());
|
|
|
|
|
|
|
|
#[cfg(feature = "console")]
|
|
|
|
let subscriber = subscriber.with(console_layer);
|
2021-11-23 12:16:47 +00:00
|
|
|
|
2022-01-06 19:10:20 +00:00
|
|
|
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)?;
|
|
|
|
|
2022-01-07 14:53:45 +00:00
|
|
|
let otel_layer = tracing_opentelemetry::layer()
|
|
|
|
.with_tracer(tracer)
|
|
|
|
.with_filter(targets);
|
2022-01-06 19:10:20 +00:00
|
|
|
|
|
|
|
let subscriber = subscriber.with(otel_layer);
|
|
|
|
|
|
|
|
set_global_default(subscriber)?;
|
|
|
|
} else {
|
|
|
|
set_global_default(subscriber)?;
|
|
|
|
}
|
2021-11-23 12:16:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|