pub struct TraceContext {
pub trace_id: TraceId,
pub span_id: SpanId,
pub parent_span_id: SpanId,
pub flags: u8,
}Expand description
W3C trace context in binary form.
Carries the identifiers needed to correlate spans across service boundaries.
The flags field uses bit 0 as the “sampled” flag (W3C traceparent spec).
Fields§
§trace_id: TraceIdTrace-wide identifier (shared by all spans in the trace).
span_id: SpanIdSpan identifier for the current span.
parent_span_id: SpanIdParent span identifier (zero if this is the root span).
flags: u8W3C trace flags. Bit 0 = sampled.
Implementations§
Source§impl TraceContext
impl TraceContext
Sourcepub fn current() -> TraceContext
pub fn current() -> TraceContext
Get the current thread-local trace context.
Sourcepub fn set_current(ctx: TraceContext)
pub fn set_current(ctx: TraceContext)
Set the current thread-local trace context.
Source§impl TraceContext
impl TraceContext
Sourcepub fn new_root(random_bytes: &[u8; 24]) -> Self
pub fn new_root(random_bytes: &[u8; 24]) -> Self
Create a new root trace context with the given random source.
Generates fresh trace_id and span_id from the provided random bytes.
random_bytes must be at least 24 bytes (16 for trace_id + 8 for span_id).
Sourcepub fn child(&self, random_span_bytes: &[u8; 8]) -> Self
pub fn child(&self, random_span_bytes: &[u8; 8]) -> Self
Create a child context within the same trace.
The child inherits trace_id and flags, gets a new span_id from
the provided random bytes, and records the current span_id as its parent.
Sourcepub fn is_sampled(&self) -> bool
pub fn is_sampled(&self) -> bool
Whether the sampled flag is set.
Sourcepub fn set_sampled(&mut self, sampled: bool)
pub fn set_sampled(&mut self, sampled: bool)
Set or clear the sampled flag.
Sourcepub fn encode(&self) -> [u8; 32]
pub fn encode(&self) -> [u8; 32]
Encode to a 32-byte binary representation.
Layout: [version:1][trace_id:16][span_id:8][flags:1][reserved:6]
Sourcepub fn decode(buf: &[u8; 32]) -> Result<Self, TraceContextError>
pub fn decode(buf: &[u8; 32]) -> Result<Self, TraceContextError>
Decode from a 32-byte binary representation.
Sourcepub fn to_w3c_string(&self) -> String
pub fn to_w3c_string(&self) -> String
Format as a W3C traceparent string: 00-{trace_id}-{span_id}-{flags}.
Returns a fixed-length 55-character string.
Sourcepub fn from_w3c_string(s: &str) -> Result<Self, TraceContextError>
pub fn from_w3c_string(s: &str) -> Result<Self, TraceContextError>
Parse a W3C traceparent string.
Expected format: {version:2}-{trace_id:32}-{span_id:16}-{flags:2}
(55 characters total).
Trait Implementations§
Source§impl Clone for TraceContext
impl Clone for TraceContext
Source§fn clone(&self) -> TraceContext
fn clone(&self) -> TraceContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more