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