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 fn init_tracing_and_logging(
17 0 : default_log_level: &str,
18 0 : ) -> anyhow::Result<Option<tracing_utils::Provider>> {
19 : // Initialize Logging
20 0 : let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
21 0 : .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_log_level));
22 :
23 0 : let fmt_layer = tracing_subscriber::fmt::layer()
24 0 : .with_ansi(false)
25 0 : .with_target(false)
26 0 : .with_writer(std::io::stderr);
27 :
28 : // Initialize OpenTelemetry
29 0 : let provider =
30 0 : tracing_utils::init_tracing("compute_ctl", tracing_utils::ExportConfig::default());
31 0 : let otlp_layer = provider.as_ref().map(tracing_utils::layer);
32 :
33 : // Put it all together
34 0 : tracing_subscriber::registry()
35 0 : .with(env_filter)
36 0 : .with(otlp_layer)
37 0 : .with(fmt_layer)
38 0 : .init();
39 0 : tracing::info!("logging and tracing started");
40 :
41 0 : utils::logging::replace_panic_hook_with_tracing_panic_hook().forget();
42 :
43 0 : Ok(provider)
44 0 : }
45 :
46 : /// Replace all newline characters with a special character to make it
47 : /// easier to grep for log messages.
48 0 : pub fn inlinify(s: &str) -> String {
49 0 : s.replace('\n', "\u{200B}")
50 0 : }
51 :
52 0 : pub fn startup_context_from_env() -> Option<opentelemetry::Context> {
53 : // Extract OpenTelemetry context for the startup actions from the
54 : // TRACEPARENT and TRACESTATE env variables, and attach it to the current
55 : // tracing context.
56 : //
57 : // This is used to propagate the context for the 'start_compute' operation
58 : // from the neon control plane. This allows linking together the wider
59 : // 'start_compute' operation that creates the compute container, with the
60 : // startup actions here within the container.
61 : //
62 : // There is no standard for passing context in env variables, but a lot of
63 : // tools use TRACEPARENT/TRACESTATE, so we use that convention too. See
64 : // https://github.com/open-telemetry/opentelemetry-specification/issues/740
65 : //
66 : // Switch to the startup context here, and exit it once the startup has
67 : // completed and Postgres is up and running.
68 : //
69 : // If this pod is pre-created without binding it to any particular endpoint
70 : // yet, this isn't the right place to enter the startup context. In that
71 : // case, the control plane should pass the tracing context as part of the
72 : // /configure API call.
73 : //
74 : // NOTE: This is supposed to only cover the *startup* actions. Once
75 : // postgres is configured and up-and-running, we exit this span. Any other
76 : // actions that are performed on incoming HTTP requests, for example, are
77 : // performed in separate spans.
78 : //
79 : // XXX: If the pod is restarted, we perform the startup actions in the same
80 : // context as the original startup actions, which probably doesn't make
81 : // sense.
82 0 : let mut startup_tracing_carrier: HashMap<String, String> = HashMap::new();
83 0 : if let Ok(val) = std::env::var("TRACEPARENT") {
84 0 : startup_tracing_carrier.insert("traceparent".to_string(), val);
85 0 : }
86 0 : if let Ok(val) = std::env::var("TRACESTATE") {
87 0 : startup_tracing_carrier.insert("tracestate".to_string(), val);
88 0 : }
89 0 : if !startup_tracing_carrier.is_empty() {
90 : use opentelemetry::propagation::TextMapPropagator;
91 : use opentelemetry_sdk::propagation::TraceContextPropagator;
92 0 : info!("got startup tracing context from env variables");
93 0 : Some(TraceContextPropagator::new().extract(&startup_tracing_carrier))
94 : } else {
95 0 : None
96 : }
97 0 : }
|