pub fn state_for_trace_stitching() -> Option<SerializableTraceState>
Available on (crate features logging or metrics or telemetry or tracing) and crate feature tracing only.
Expand description

Returns tracing state for the current span that can be serialized and passed to other services to stitch it with their traces, so traces can cover the whole service pipeline.

The serialized trace then can be passed to start_trace by other service to continue the trace.

Returns None if the current span is not sampled and don’t have associated trace.

§Examples

use foundations::telemetry::TelemetryContext;
use foundations::telemetry::tracing::{self, test_trace, SerializableTraceState, StartTraceOptions};

// Test context is used for demonstration purposes to show the resulting traces.
let ctx = TelemetryContext::test();
let _scope = ctx.scope();

fn service1() -> String {
    let _span = tracing::span("service1_span");

    tracing::state_for_trace_stitching().unwrap().to_string()
}

fn service2(trace_state: String) {
    let _span = tracing::start_trace(
        "service2_span",
        StartTraceOptions {
            stitch_with_trace: Some(trace_state.parse().unwrap()),
            ..Default::default()
        }
    );
}

let trace_state = service1();

service2(trace_state);

assert_eq!(
    ctx.traces(Default::default()),
    vec![test_trace! {
        "service1_span" => {
            "service2_span"
        }
    }]
);