TLA Line data Source code
1 : use tracing_opentelemetry::OpenTelemetryLayer;
2 : use tracing_subscriber::layer::SubscriberExt;
3 : use tracing_subscriber::prelude::*;
4 :
5 : /// Initialize logging to stderr, and OpenTelemetry tracing and exporter.
6 : ///
7 : /// Logging is configured using either `default_log_level` or
8 : /// `RUST_LOG` environment variable as default log level.
9 : ///
10 : /// OpenTelemetry is configured with OTLP/HTTP exporter. It picks up
11 : /// configuration from environment variables. For example, to change the destination,
12 : /// set `OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318`. See
13 : /// `tracing-utils` package description.
14 : ///
15 CBC 641 : pub fn init_tracing_and_logging(default_log_level: &str) -> anyhow::Result<()> {
16 641 : // Initialize Logging
17 641 : let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
18 641 : .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_log_level));
19 641 :
20 641 : let fmt_layer = tracing_subscriber::fmt::layer()
21 641 : .with_ansi(false)
22 641 : .with_target(false)
23 641 : .with_writer(std::io::stderr);
24 641 :
25 641 : // Initialize OpenTelemetry
26 641 : let otlp_layer =
27 641 : tracing_utils::init_tracing_without_runtime("compute_ctl").map(OpenTelemetryLayer::new);
28 641 :
29 641 : // Put it all together
30 641 : tracing_subscriber::registry()
31 641 : .with(env_filter)
32 641 : .with(otlp_layer)
33 641 : .with(fmt_layer)
34 641 : .init();
35 641 : tracing::info!("logging and tracing started");
36 :
37 641 : utils::logging::replace_panic_hook_with_tracing_panic_hook().forget();
38 641 :
39 641 : Ok(())
40 641 : }
|