hermod_api/services/
telemetry.rs

1//! Contains functions that configure and initialize tracing telemetry, as well as additional helpers.
2use actix_web::rt::task::JoinHandle;
3use opentelemetry_otlp::WithExportConfig;
4use tracing::subscriber::set_global_default;
5use tracing::Subscriber;
6use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
7use tracing_log::LogTracer;
8use tracing_subscriber::fmt::MakeWriter;
9use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
10
11/// Compose multiple layers into a `tracing`'s subscriber.
12///
13/// # Implementation Notes
14///
15/// We are using `impl Subscriber` as return type to avoid having to spell out the actual
16/// type of the returned subscriber, which is indeed quite complex.
17pub fn get_subscriber(
18    name: String,
19    env_filter: String,
20    sink: impl MakeWriter + Send + Sync + 'static,
21    endpoint_url: &str,
22) -> impl Subscriber + Sync + Send {
23    let tracer = opentelemetry_otlp::new_pipeline()
24        .tracing()
25        .with_exporter(
26            opentelemetry_otlp::new_exporter()
27                .tonic()
28                .with_endpoint(endpoint_url),
29        )
30        .install_batch(opentelemetry::runtime::Tokio)
31        .expect("Failed to install tracing pipeline.");
32    let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
33
34    let env_filter =
35        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
36    let formatting_layer = BunyanFormattingLayer::new(name, sink);
37
38    Registry::default()
39        .with(env_filter)
40        .with(JsonStorageLayer)
41        .with(formatting_layer)
42        .with(telemetry)
43}
44
45/// Creates a subscriber that dumps traces into a sink. Used for automated testing.
46pub fn get_subscriber_test(
47    name: String,
48    env_filter: String,
49    sink: impl MakeWriter + Send + Sync + 'static,
50) -> impl Subscriber + Sync + Send {
51    let env_filter =
52        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
53    let formatting_layer = BunyanFormattingLayer::new(name, sink);
54
55    Registry::default()
56        .with(env_filter)
57        .with(JsonStorageLayer)
58        .with(formatting_layer)
59}
60
61/// Register a subscriber as global default to process span data.
62///
63/// It should only be called once!
64pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) {
65    LogTracer::init().expect("Failed to set logger");
66    set_global_default(subscriber).expect("Failed to set subscriber");
67}
68
69/// Spawn a blocking function within the current tracing Span
70pub fn spawn_blocking_with_tracing<F, R>(f: F) -> JoinHandle<R>
71where
72    F: FnOnce() -> R + Send + 'static,
73    R: Send + 'static,
74{
75    let current_span = tracing::Span::current();
76    actix_web::rt::task::spawn_blocking(move || current_span.in_scope(f))
77}