wasmcloud_tracing/
traces.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::env;
use std::fs::File;
use std::io::{BufWriter, IsTerminal};
use std::path::Path;
#[cfg(feature = "otel")]
use std::sync::Arc;

#[cfg(feature = "otel")]
use anyhow::Context as _;
#[cfg(feature = "otel")]
use opentelemetry_otlp::WithExportConfig;
use tracing::{Event, Subscriber};
use tracing_flame::FlameLayer;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::format::{DefaultFields, Format, Full, Json, JsonFields, Writer};
use tracing_subscriber::fmt::time::SystemTime;
use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::EnvFilter;
#[cfg(feature = "otel")]
use tracing_subscriber::Layer;
use wasmcloud_core::logging::Level;
use wasmcloud_core::OtelConfig;
#[cfg(feature = "otel")]
use wasmcloud_core::OtelProtocol;

#[cfg(feature = "otel")]
static LOG_PROVIDER: once_cell::sync::OnceCell<opentelemetry_sdk::logs::LoggerProvider> =
    once_cell::sync::OnceCell::new();

/// A struct that allows us to dynamically choose JSON formatting without using dynamic dispatch.
/// This is just so we avoid any sort of possible slow down in logging code
enum JsonOrNot {
    Not(Format<Full, SystemTime>),
    Json(Format<Json, SystemTime>),
}

impl<S, N> FormatEvent<S, N> for JsonOrNot
where
    S: Subscriber + for<'lookup> LookupSpan<'lookup>,
    N: for<'writer> FormatFields<'writer> + 'static,
{
    fn format_event(
        &self,
        ctx: &FmtContext<'_, S, N>,
        writer: Writer<'_>,
        event: &Event<'_>,
    ) -> std::fmt::Result
    where
        S: Subscriber + for<'a> LookupSpan<'a>,
    {
        match self {
            JsonOrNot::Not(f) => f.format_event(ctx, writer, event),
            JsonOrNot::Json(f) => f.format_event(ctx, writer, event),
        }
    }
}

/// This guard prevents early `drop()`ing of the tracing related internal data structures
pub struct FlushGuard {
    _stderr: tracing_appender::non_blocking::WorkerGuard,
    _flame: Option<tracing_flame::FlushGuard<BufWriter<File>>>,
}

/// Configures a global tracing subscriber, which includes:
/// - A level filter, which forms the base and applies to all other layers
/// - A local logging layer, which is either plaintext or structured (JSON)
///
/// # Errors
///
/// This will return an error if the function has already been called, or if we fail to create any
/// of the layers
#[cfg(not(feature = "otel"))]
pub fn configure_tracing(
    _: &str,
    _: &OtelConfig,
    use_structured_logging: bool,
    flame_graph: Option<impl AsRef<Path>>,
    log_level_override: Option<&Level>,
) -> anyhow::Result<(tracing::Dispatch, FlushGuard)> {
    let flame = flame_graph.map(FlameLayer::with_file).transpose()?;
    let (flame, flame_guard) = flame.map(|(l, g)| (Some(l), Some(g))).unwrap_or_default();
    let reg = tracing_subscriber::Registry::default()
        .with(get_log_level_filter(log_level_override))
        .with(flame);
    let stderr = std::io::stderr();
    let ansi = stderr.is_terminal();
    let (stderr, stderr_guard) = tracing_appender::non_blocking(stderr);
    let fmt = tracing_subscriber::fmt::layer()
        .with_writer(stderr)
        .with_ansi(ansi);

    let dispatch = if use_structured_logging {
        reg.with(
            fmt.event_format(JsonOrNot::Json(Format::default().json()))
                .fmt_fields(JsonFields::new()),
        )
        .into()
    } else {
        reg.with(
            fmt.event_format(JsonOrNot::Not(Format::default()))
                .fmt_fields(DefaultFields::new()),
        )
        .into()
    };

    Ok((
        dispatch,
        FlushGuard {
            _stderr: stderr_guard,
            _flame: flame_guard,
        },
    ))
}

/// Configures a global tracing subscriber, which includes:
/// - A level filter, which forms the base and applies to all other layers
/// - OTEL tracing and logging layers, if OTEL configuration is provided
/// - A local logging layer, which is either plaintext or structured (JSON)
///
/// # Errors
///
/// This will return an error if the function has already been called, or if we fail to create any
/// of the layers
#[cfg(feature = "otel")]
pub fn configure_tracing(
    service_name: &str,
    otel_config: &OtelConfig,
    use_structured_logging: bool,
    flame_graph: Option<impl AsRef<Path>>,
    log_level_override: Option<&Level>,
    trace_level_override: Option<&Level>,
) -> anyhow::Result<(tracing::Dispatch, FlushGuard)> {
    let service_name = Arc::from(service_name);

    let log_level_filter = get_log_level_filter(log_level_override);
    let traces = otel_config
        .traces_enabled()
        .then(|| {
            get_otel_tracing_layer(
                Arc::clone(&service_name),
                otel_config,
                get_trace_level_filter(trace_level_override),
            )
        })
        .transpose()?;
    let logs = otel_config
        .logs_enabled()
        .then(|| get_otel_logging_layer(Arc::clone(&service_name), otel_config, log_level_override))
        .transpose()?;
    let flame = flame_graph.map(FlameLayer::with_file).transpose()?;
    let (flame, flame_guard) = flame
        .map(|(l, g)| {
            (
                Some(l.with_filter(get_trace_level_filter(trace_level_override))),
                Some(g),
            )
        })
        .unwrap_or_default();
    let registry = tracing_subscriber::Registry::default()
        .with(get_log_level_filter(log_level_override))
        .with(traces)
        .with(logs)
        .with(flame);
    let stderr = std::io::stderr();
    let ansi = stderr.is_terminal();
    let (stderr, stderr_guard) = tracing_appender::non_blocking(stderr);
    let fmt = tracing_subscriber::fmt::layer()
        .with_writer(stderr)
        .with_ansi(ansi);

    let dispatch = if use_structured_logging {
        registry
            .with(
                fmt.event_format(JsonOrNot::Json(Format::default().json()))
                    .fmt_fields(JsonFields::new())
                    .with_filter(log_level_filter),
            )
            .into()
    } else {
        registry
            .with(
                fmt.event_format(JsonOrNot::Not(Format::default()))
                    .fmt_fields(DefaultFields::new())
                    .with_filter(log_level_filter),
            )
            .into()
    };

    Ok((
        dispatch,
        FlushGuard {
            _stderr: stderr_guard,
            _flame: flame_guard,
        },
    ))
}

#[cfg(feature = "otel")]
fn get_otel_tracing_layer<S>(
    service_name: Arc<str>,
    otel_config: &OtelConfig,
    trace_level_filter: EnvFilter,
) -> anyhow::Result<impl tracing_subscriber::Layer<S>>
where
    S: Subscriber,
    S: for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    use opentelemetry::trace::TracerProvider as _;
    use opentelemetry_otlp::WithHttpConfig;
    use opentelemetry_sdk::trace::{BatchConfigBuilder, Sampler};
    use tracing_opentelemetry::OpenTelemetryLayer;

    let exporter = match otel_config.protocol {
        OtelProtocol::Http => {
            let client = crate::get_http_client(otel_config)
                .context("failed to get an http client for otel tracing exporter")?;
            opentelemetry_otlp::SpanExporter::builder()
                .with_http()
                .with_http_client(client)
                .with_endpoint(otel_config.traces_endpoint())
                .with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
                .build()
                .context("failed to build OTEL span exporter")?
        }
        OtelProtocol::Grpc => {
            // TODO(joonas): Configure tonic::transport::ClientTlsConfig via .with_tls_config(...), passing in additional certificates.
            opentelemetry_otlp::SpanExporter::builder()
                .with_tonic()
                .with_endpoint(otel_config.traces_endpoint())
                .build()
                .context("failed to build OTEL span exporter")?
        }
    };

    // NOTE(thomastaylor312): This is copied and modified from the opentelemetry-sdk crate. We
    // currently need this because providers map config back into the vars needed to configure the
    // SDK. When we update providers to be managed externally and remove host-managed ones, we can
    // remove this. But for now we need to parse all the possible options
    let sampler = match otel_config.traces_sampler.as_deref() {
        Some("always_on") => Sampler::AlwaysOn,
        Some("always_off") => Sampler::AlwaysOff,
        Some("traceidratio") => {
            let ratio = otel_config
                .traces_sampler_arg
                .as_ref()
                .and_then(|r| r.parse::<f64>().ok());
            if let Some(r) = ratio {
                Sampler::TraceIdRatioBased(r)
            } else {
                eprintln!("Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0");
                Sampler::TraceIdRatioBased(1.0)
            }
        }
        Some("parentbased_always_on") => Sampler::ParentBased(Box::new(Sampler::AlwaysOn)),
        Some("parentbased_always_off") => Sampler::ParentBased(Box::new(Sampler::AlwaysOff)),
        Some("parentbased_traceidratio") => {
            let ratio = otel_config
                .traces_sampler_arg
                .as_ref()
                .and_then(|r| r.parse::<f64>().ok());
            if let Some(r) = ratio {
                Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(r)))
            } else {
                eprintln!("Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0");
                Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(1.0)))
            }
        }
        Some(s) => {
            eprintln!("Unrecognised or unimplemented OTEL_TRACES_SAMPLER value: {s}. Falling back to default: parentbased_always_on");
            Sampler::ParentBased(Box::new(Sampler::AlwaysOn))
        }
        None => Sampler::ParentBased(Box::new(Sampler::AlwaysOn)),
    };

    let mut batch_builder = BatchConfigBuilder::default();
    if let Some(max_batch_queue_size) = otel_config.max_batch_queue_size {
        batch_builder = batch_builder.with_max_queue_size(max_batch_queue_size);
    }
    if let Some(concurrent_exports) = otel_config.concurrent_exports {
        batch_builder = batch_builder.with_max_concurrent_exports(concurrent_exports);
    }
    let batch_config = batch_builder.build();

    let processor = opentelemetry_sdk::trace::BatchSpanProcessor::builder(
        exporter,
        opentelemetry_sdk::runtime::Tokio,
    )
    .with_batch_config(batch_config)
    .build();

    let tracer = opentelemetry_sdk::trace::TracerProvider::builder()
        .with_sampler(sampler)
        .with_resource(opentelemetry_sdk::Resource::new(vec![
            opentelemetry::KeyValue::new("service.name", service_name),
        ]))
        .with_span_processor(processor)
        .build()
        .tracer("wasmcloud-tracing");

    Ok(OpenTelemetryLayer::new(tracer).with_filter(trace_level_filter))
}

#[cfg(feature = "otel")]
fn get_otel_logging_layer<S>(
    service_name: Arc<str>,
    otel_config: &OtelConfig,
    log_level_override: Option<&Level>,
) -> anyhow::Result<impl tracing_subscriber::Layer<S>>
where
    S: Subscriber,
    S: for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    use opentelemetry_otlp::WithHttpConfig;

    let exporter = match otel_config.protocol {
        OtelProtocol::Http => {
            let client = crate::get_http_client(otel_config)
                .context("failed to get an http client for otel logging exporter")?;
            opentelemetry_otlp::LogExporter::builder()
                .with_http()
                .with_http_client(client)
                .with_endpoint(otel_config.logs_endpoint())
                .with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
                .build()
                .context("failed to create OTEL http log exporter")?
        }
        OtelProtocol::Grpc => {
            // TODO(joonas): Configure tonic::transport::ClientTlsConfig via .with_tls_config(...), passing in additional certificates.
            opentelemetry_otlp::LogExporter::builder()
                .with_tonic()
                .with_endpoint(otel_config.logs_endpoint())
                .build()
                .context("failed to create OTEL http log exporter")?
        }
    };

    let processor = opentelemetry_sdk::logs::BatchLogProcessor::builder(
        exporter,
        opentelemetry_sdk::runtime::Tokio,
    )
    .build();

    let log_provider = opentelemetry_sdk::logs::LoggerProvider::builder()
        .with_resource(opentelemetry_sdk::Resource::new(vec![
            opentelemetry::KeyValue::new("service.name", service_name),
        ]))
        .with_log_processor(processor)
        .build();

    // Prevent the exporter/provider from being dropped
    LOG_PROVIDER
        .set(log_provider)
        .map_err(|_| anyhow::anyhow!("Logger provider already initialized"))?;

    let log_layer = opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge::new(
        LOG_PROVIDER.get().unwrap(),
    )
    .with_filter(get_log_level_filter(log_level_override));

    Ok(log_layer)
}

#[cfg(feature = "otel")]
fn get_trace_level_filter(trace_level_override: Option<&Level>) -> EnvFilter {
    if let Some(trace_level) = trace_level_override {
        let level = wasi_level_to_tracing_level(trace_level);
        EnvFilter::default().add_directive(level.into())
    } else {
        EnvFilter::default().add_directive(LevelFilter::DEBUG.into())
    }
}

fn get_log_level_filter(log_level_override: Option<&Level>) -> EnvFilter {
    if let Some(log_level) = log_level_override {
        let level = wasi_level_to_tracing_level(log_level);
        // SAFETY: We can unwrap here because we control all inputs
        let mut filter = EnvFilter::builder()
            .with_default_directive(level.into())
            .parse("")
            .unwrap()
            .add_directive("async_nats=info".parse().unwrap())
            .add_directive("cranelift_codegen=warn".parse().unwrap())
            .add_directive("hyper=info".parse().unwrap())
            .add_directive("oci_client=info".parse().unwrap());

        // Allow RUST_LOG to override the other directives
        if let Ok(rust_log) = env::var("RUST_LOG") {
            match rust_log
                .split(',')
                .map(str::parse)
                .collect::<Result<Vec<_>, _>>()
            {
                Ok(directives) => {
                    for directive in directives {
                        filter = filter.add_directive(directive);
                    }
                }
                Err(err) => {
                    eprintln!("ERROR: Ignoring invalid RUST_LOG directive: {err}");
                }
            }
        }

        filter
    } else {
        EnvFilter::default().add_directive(LevelFilter::INFO.into())
    }
}

fn wasi_level_to_tracing_level(level: &Level) -> LevelFilter {
    match level {
        Level::Error | Level::Critical => LevelFilter::ERROR,
        Level::Warn => LevelFilter::WARN,
        Level::Info => LevelFilter::INFO,
        Level::Debug => LevelFilter::DEBUG,
        Level::Trace => LevelFilter::TRACE,
    }
}