Trait opentelemetry::trace::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
Required Methods§
sourcefn current_with_span<T: Span + Send + Sync + 'static>(span: T) -> Self
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);
sourcefn with_span<T: Span + Send + Sync + 'static>(&self, span: T) -> Self
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);
}
sourcefn span(&self) -> SpanRef<'_>
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![]));
sourcefn has_active_span(&self) -> bool
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()));
sourcefn with_remote_span_context(&self, span_context: SpanContext) -> Self
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.
Object Safety§
This trait is not object safe.