Rust tracing/opentelemetry adding context attributes dynamically

118 Views Asked by At

I have a Rust app that is using tracing + OpenTelemetry. I would like to be able to dynamically set key/value pairs that I can use in a custom tracing::Subscriber layer. In Java, this is usually accomplished with MDC.

In Rust, I have tried setting attributes on the opentelemetry::Context following the documentation but when I try to access it in my subscriber, its None.

I have also tried setting attributes using tracing::span!(..., foo=bar) however this does not appear to allow dynamically adding attributes.

Here is my current tracing/otel config

 let tracer = opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(opentelemetry_otlp::new_exporter().tonic())
        .install_batch(opentelemetry_sdk::runtime::AsyncStd)
        .unwrap();
    let telemetry = OpenTelemetryLayer::new(tracer);
    let custom_layer = create_custom_layer();

    tracing_subscriber::registry()
        .with(telemetry)
        .with(custom_layer)
        .init();
0

There are 0 best solutions below