TraceContextExt

Trait TraceContextExt 

Source
pub trait TraceContextExt {
    // Provided methods
    fn inject_trace_context<S>(&mut self, context: DatadogContext)
       where S: Strategy<Self> { ... }
    fn extract_trace_context<S>(&self) -> DatadogContext
       where S: Strategy<Self> { ... }
}
Expand description

Extension trait for extracting/injecting DatadogContext using a Strategy.

This trait allows handling of trace context in arbitrary container types. To implement it for additional types, you need to implement Strategy for them.

You should not need to override this trait’s default implementations.

See this example:

use tracing_datadog::context::{DatadogContext, TraceContextExt, Strategy};

struct MyType(String);

impl TraceContextExt for MyType {}

struct MyTypeStrategy;

impl Strategy<MyType> for MyTypeStrategy {
    fn inject(my_type: &mut MyType, context: DatadogContext) {
        my_type.0 = format!("{}:{}", context.trace_id, context.parent_id);
    }

    fn extract(my_type: &MyType) -> DatadogContext {
        let Some((trace_id, span_id)) = my_type.0.split_once(':') else {
            return DatadogContext::default();
        };

        DatadogContext {
            trace_id: trace_id.parse().unwrap_or_default(),
            parent_id: span_id.parse().unwrap_or_default(),
        }
    }
}

// Round-trip through MyType.
let before = DatadogContext { trace_id: 123, parent_id: 456 };
let mut my_type = MyType(String::new());

my_type.inject_trace_context::<MyTypeStrategy>(before);
let after = my_type.extract_trace_context::<MyTypeStrategy>();

assert_eq!(before.trace_id, after.trace_id);
assert_eq!(before.parent_id, after.parent_id);

See W3CTraceContextHeaders for a real example implementation.

Provided Methods§

Source

fn inject_trace_context<S>(&mut self, context: DatadogContext)
where S: Strategy<Self>,

Source

fn extract_trace_context<S>(&self) -> DatadogContext
where S: Strategy<Self>,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl TraceContextExt for HeaderMap

Implementors§