pub fn get_active_span<F, T>(f: F) -> Twhere
    F: FnOnce(SpanRef<'_>) -> T,
Expand description

Executes a closure with a reference to this thread’s current span.

Examples

use opentelemetry_api::{global, trace::{Span, Tracer}, KeyValue};
use opentelemetry_api::trace::get_active_span;

fn my_function() {
    // start an active span in one function
    global::tracer("my-component").in_span("span-name", |_cx| {
        // anything happening in functions we call can still access the active span...
        my_other_function();
    })
}

fn my_other_function() {
    // call methods on the current span from
    get_active_span(|span| {
        span.add_event("An event!", vec![KeyValue::new("happened", true)]);
    })
}