Trait tracing_opentelemetry::OpenTelemetrySpanExt [−][src]
pub trait OpenTelemetrySpanExt { fn set_parent(&self, cx: Context); fn context(&self) -> Context; }
Expand description
Utility functions to allow tracing Span
s to accept and return
OpenTelemetry Context
s.
Required methods
fn set_parent(&self, cx: Context)
fn set_parent(&self, cx: Context)
Associates self
with a given OpenTelemetry trace, using the provided
parent Context
.
Examples
use opentelemetry::{propagation::TextMapPropagator, trace::TraceContextExt}; use opentelemetry::sdk::propagation::TraceContextPropagator; use tracing_opentelemetry::OpenTelemetrySpanExt; use std::collections::HashMap; use tracing::Span; // Example carrier, could be a framework header map that impls otel's `Extract`. let mut carrier = HashMap::new(); // Propagator can be swapped with b3 propagator, jaeger propagator, etc. let propagator = TraceContextPropagator::new(); // Extract otel parent context via the chosen propagator let parent_context = propagator.extract(&carrier); // Generate a tracing span as usual let app_root = tracing::span!(tracing::Level::INFO, "app_start"); // Assign parent trace from external context app_root.set_parent(parent_context.clone()); // Or if the current span has been created elsewhere: Span::current().set_parent(parent_context);
Extracts an OpenTelemetry Context
from self
.
Examples
use opentelemetry::Context; use tracing_opentelemetry::OpenTelemetrySpanExt; use tracing::Span; fn make_request(cx: Context) { // perform external request after injecting context // e.g. if the request's headers impl `opentelemetry::propagation::Injector` // then `propagator.inject_context(cx, request.headers_mut())` } // Generate a tracing span as usual let app_root = tracing::span!(tracing::Level::INFO, "app_start"); // To include tracing context in client requests from _this_ app, // extract the current OpenTelemetry context. make_request(app_root.context()); // Or if the current span has been created elsewhere: make_request(Span::current().context())