Line data Source code
1 : use std::collections::HashMap;
2 : use tracing::info;
3 : use tracing_subscriber::layer::SubscriberExt;
4 : use tracing_subscriber::prelude::*;
5 :
6 : /// Initialize logging to stderr, and OpenTelemetry tracing and exporter.
7 : ///
8 : /// Logging is configured using either `default_log_level` or
9 : /// `RUST_LOG` environment variable as default log level.
10 : ///
11 : /// OpenTelemetry is configured with OTLP/HTTP exporter. It picks up
12 : /// configuration from environment variables. For example, to change the destination,
13 : /// set `OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318`. See
14 : /// `tracing-utils` package description.
15 : ///
16 0 : pub async fn init_tracing_and_logging(default_log_level: &str) -> anyhow::Result<()> {
17 0 : // Initialize Logging
18 0 : let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
19 0 : .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_log_level));
20 0 :
21 0 : let fmt_layer = tracing_subscriber::fmt::layer()
22 0 : .with_ansi(false)
23 0 : .with_target(false)
24 0 : .with_writer(std::io::stderr);
25 :
26 : // Initialize OpenTelemetry
27 0 : let otlp_layer = tracing_utils::init_tracing("compute_ctl").await;
28 :
29 : // 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 : }
47 :
48 0 : pub fn startup_context_from_env() -> Option<opentelemetry::Context> {
49 0 : // Extract OpenTelemetry context for the startup actions from the
50 0 : // TRACEPARENT and TRACESTATE env variables, and attach it to the current
51 0 : // tracing context.
52 0 : //
53 0 : // This is used to propagate the context for the 'start_compute' operation
54 0 : // from the neon control plane. This allows linking together the wider
55 0 : // 'start_compute' operation that creates the compute container, with the
56 0 : // startup actions here within the container.
57 0 : //
58 0 : // There is no standard for passing context in env variables, but a lot of
59 0 : // tools use TRACEPARENT/TRACESTATE, so we use that convention too. See
60 0 : // https://github.com/open-telemetry/opentelemetry-specification/issues/740
61 0 : //
62 0 : // Switch to the startup context here, and exit it once the startup has
63 0 : // completed and Postgres is up and running.
64 0 : //
65 0 : // If this pod is pre-created without binding it to any particular endpoint
66 0 : // yet, this isn't the right place to enter the startup context. In that
67 0 : // case, the control plane should pass the tracing context as part of the
68 0 : // /configure API call.
69 0 : //
70 0 : // NOTE: This is supposed to only cover the *startup* actions. Once
71 0 : // postgres is configured and up-and-running, we exit this span. Any other
72 0 : // actions that are performed on incoming HTTP requests, for example, are
73 0 : // performed in separate spans.
74 0 : //
75 0 : // XXX: If the pod is restarted, we perform the startup actions in the same
76 0 : // context as the original startup actions, which probably doesn't make
77 0 : // sense.
78 0 : let mut startup_tracing_carrier: HashMap<String, String> = HashMap::new();
79 0 : if let Ok(val) = std::env::var("TRACEPARENT") {
80 0 : startup_tracing_carrier.insert("traceparent".to_string(), val);
81 0 : }
82 0 : if let Ok(val) = std::env::var("TRACESTATE") {
83 0 : startup_tracing_carrier.insert("tracestate".to_string(), val);
84 0 : }
85 0 : if !startup_tracing_carrier.is_empty() {
86 : use opentelemetry::propagation::TextMapPropagator;
87 : use opentelemetry_sdk::propagation::TraceContextPropagator;
88 0 : info!("got startup tracing context from env variables");
89 0 : Some(TraceContextPropagator::new().extract(&startup_tracing_carrier))
90 : } else {
91 0 : None
92 : }
93 0 : }
|