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 0 : pub fn init_tracing_and_logging(default_log_level: &str) -> anyhow::Result<()> {
16 0 : // Initialize Logging
17 0 : let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
18 0 : .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_log_level));
19 0 :
20 0 : let fmt_layer = tracing_subscriber::fmt::layer()
21 0 : .with_ansi(false)
22 0 : .with_target(false)
23 0 : .with_writer(std::io::stderr);
24 0 :
25 0 : // Initialize OpenTelemetry
26 0 : let otlp_layer =
27 0 : tracing_utils::init_tracing_without_runtime("compute_ctl").map(OpenTelemetryLayer::new);
28 0 :
29 0 : // Put it all together
30 0 : tracing_subscriber::registry()
31 0 : .with(env_filter)
32 0 : .with(otlp_layer)
33 0 : .with(fmt_layer)
34 0 : .init();
35 0 : tracing::info!("logging and tracing started");
36 :
37 0 : utils::logging::replace_panic_hook_with_tracing_panic_hook().forget();
38 0 :
39 0 : Ok(())
40 0 : }
41 :
42 : /// Replace all newline characters with a special character to make it
43 : /// easier to grep for log messages.
44 0 : pub fn inlinify(s: &str) -> String {
45 0 : s.replace('\n', "\u{200B}")
46 0 : }
|