Function opentelemetry::trace::get_active_span

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

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

§Examples

use opentelemetry::{global, trace::{Span, Tracer}, KeyValue};
use opentelemetry::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)]);
    })
}