opentelemetry::trace

Trait TraceContextExt

Source
pub trait TraceContextExt {
    // Required methods
    fn current_with_span<T: Span + Send + Sync + 'static>(span: T) -> Self;
    fn with_span<T: Span + Send + Sync + 'static>(&self, span: T) -> Self;
    fn span(&self) -> SpanRef<'_>;
    fn has_active_span(&self) -> bool;
    fn with_remote_span_context(&self, span_context: SpanContext) -> Self;
}
Expand description

Methods for storing and retrieving trace data in a Context.

See Context for examples of setting and retrieving the current context.

Required Methods§

Source

fn current_with_span<T: Span + Send + Sync + 'static>(span: T) -> Self

Returns a clone of the current context with the included Span.

§Examples
use opentelemetry::{global, trace::{TraceContextExt, Tracer}, Context};

let tracer = global::tracer("example");

// build a span
let span = tracer.start("parent_span");

// create a new context from the currently active context that includes this span
let cx = Context::current_with_span(span);

// create a child span by explicitly specifying the parent context
let child = tracer.start_with_context("child_span", &cx);
Source

fn with_span<T: Span + Send + Sync + 'static>(&self, span: T) -> Self

Returns a clone of this context with the included span.

§Examples
use opentelemetry::{global, trace::{TraceContextExt, Tracer}, Context};

fn fn_with_passed_in_context(cx: &Context) {
    let tracer = global::tracer("example");

    // build a span
    let span = tracer.start("parent_span");

    // create a new context from the given context that includes the span
    let cx_with_parent = cx.with_span(span);

    // create a child span by explicitly specifying the parent context
    let child = tracer.start_with_context("child_span", &cx_with_parent);
}
Source

fn span(&self) -> SpanRef<'_>

Returns a reference to this context’s span, or the default no-op span if none has been set.

§Examples
use opentelemetry::{trace::TraceContextExt, Context};

// Add an event to the currently active span
Context::map_current(|cx| cx.span().add_event("An event!", vec![]));
Source

fn has_active_span(&self) -> bool

Returns whether or not an active span has been set.

§Examples
use opentelemetry::{trace::TraceContextExt, Context};

assert!(!Context::map_current(|cx| cx.has_active_span()));
Source

fn with_remote_span_context(&self, span_context: SpanContext) -> Self

Returns a copy of this context with the span context included.

This is useful for building propagators.

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.

Implementors§