Skip to main content

TraceCarrier

Struct TraceCarrier 

Source
pub struct TraceCarrier {
    pub traceparent: Option<String>,
    pub tracestate: Option<String>,
}
Expand description

A W3C trace context in transit, sized to fit in a request payload.

Both fields are Option because most calls happen with no active span: no sampling, or OTLP switched off. The carrier is then two Nones that serialise to nothing. #[serde(default, skip_serializing_if = …)] makes that free on the wire. It is a payload-size optimisation for the common empty case, not a compatibility shim for older encodings. Krabka keeps no such shims: see CLAUDE.md.

This type does not derive PartialEq, and that is deliberate. RPC envelopes hold a carrier next to a request, and their equality must stay a pure function of the request payload. A derived PartialEq would silently make two identical requests compare unequal because they were traced differently.

Fields§

§traceparent: Option<String>

The validated, re-rendered W3C traceparent.

§tracestate: Option<String>

The validated, re-rendered W3C tracestate, when the peer sent one small enough to forward.

Implementations§

Source§

impl TraceCarrier

Source

pub fn capture_current() -> Self

Capture the currently active span’s trace context.

The carrier is empty when there is no active span, when the span is not sampled, or when OTLP is disabled. It is always safe to call on a hot path.

Source

pub fn from_w3c( traceparent: &str, tracestate: Option<&str>, ) -> Result<Self, TraceContextError>

Build a carrier from a peer-supplied traceparent and tracestate.

This function validates both inputs. traceparent must match ^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$ with a non-zero trace-id and a non-zero span-id. The function drops the tracestate, and does not return an error, when it is more than 512 bytes, when it has more than 32 list members, or when it is not a well-formed list. The traceparent still applies, because a lost vendor state is better than a lost trace.

This function stores neither input verbatim. It parses both, then re-renders them from the SpanContext and the TraceState, so a hostile value cannot get into a span attribute or a log line.

§Errors

Returns TraceContextError when traceparent fails validation. A caller on an ingress path should discard the error and not show it: a bad trace header must never fail the request that carried it.

Source

pub fn from_headers<'a, I, V>(headers: I) -> Self
where I: IntoIterator<Item = (&'a str, V)>, V: AsRef<[u8]>,

Build a carrier from the headers on a Kafka record.

Each header is a key plus a raw byte value. This function does the same validation as TraceCarrier::from_w3c. But a non-UTF-8, absent, or invalid value gives an empty carrier and not an error, because a consumer that cannot read the producer’s trace context must still apply the record.

Source

pub fn is_empty(&self) -> bool

true when there is no trace context to propagate.

Source

pub fn apply_to(&self, span: &Span)

Make span a child of the carried context.

Work on this side of the boundary then joins the caller’s trace. This method does nothing when the carrier is empty.

Attach the carried context to span as an OpenTelemetry link.

The carried context becomes a link, not the parent of span. This method does nothing when the carrier is empty.

A link is the correct relation when many nodes do the linked work, or when the work runs long after the operation that started it. Every follower applies a WAL record, and recovery replays that record hours after the commit. A parent relation would stretch one trace across the retention period, and would force export of every apply of every sampled write.

Source

pub fn span_context(&self) -> Option<SpanContext>

The remote SpanContext this carrier describes.

None when the carrier is empty, or when it holds a value that does not parse.

Source

pub fn headers(&self) -> impl Iterator<Item = (&str, &[u8])> + '_

The carrier as (key, value) header pairs for a Kafka record.

The iterator yields nothing when the carrier is empty.

Trait Implementations§

Source§

impl Clone for TraceCarrier

Source§

fn clone(&self) -> TraceCarrier

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TraceCarrier

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TraceCarrier

Source§

fn default() -> TraceCarrier

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for TraceCarrier

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for TraceCarrier

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more