pub struct TraceContext {
pub trace_id: String,
pub span_id: String,
pub trace_flags: String,
pub trace_state: Option<String>,
}Expand description
W3C Trace Context for distributed tracing
Compatible with OpenTelemetry, Jaeger, Zipkin, and other distributed tracing systems. See: https://www.w3.org/TR/trace-context/
§Example
use celers_broker_sql::{MysqlBroker, TraceContext};
use celers_core::{Broker, SerializedTask};
// Create a trace context
let trace_ctx = TraceContext::new(
"4bf92f3577b34da6a3ce929d0e0e4736",
"00f067aa0ba902b7"
);
// Enqueue task with trace context
let task = SerializedTask::new("my_task".to_string(), vec![]);
broker.enqueue_with_trace_context(task, trace_ctx).await?;
// Extract trace context when processing
if let Some(msg) = broker.dequeue().await? {
if let Some(ctx) = broker.extract_trace_context(&msg.task.metadata.id).await? {
println!("Processing task with trace_id: {}", ctx.trace_id);
}
}Fields§
§trace_id: StringW3C Trace ID (32 hex characters, 16 bytes)
span_id: StringW3C Span ID (16 hex characters, 8 bytes)
trace_flags: StringTrace flags (8-bit field, typically “01” for sampled)
trace_state: Option<String>Optional trace state for vendor-specific data
Implementations§
Source§impl TraceContext
impl TraceContext
Sourcepub fn new(trace_id: impl Into<String>, span_id: impl Into<String>) -> Self
pub fn new(trace_id: impl Into<String>, span_id: impl Into<String>) -> Self
Create a new trace context with trace_id and span_id
§Arguments
trace_id- 32 hex character trace ID (W3C format)span_id- 16 hex character span ID (W3C format)
§Example
use celers_broker_sql::TraceContext;
let ctx = TraceContext::new(
"4bf92f3577b34da6a3ce929d0e0e4736",
"00f067aa0ba902b7"
);
assert_eq!(ctx.trace_flags, "01"); // Sampled by defaultSourcepub fn from_traceparent(traceparent: &str) -> Result<Self>
pub fn from_traceparent(traceparent: &str) -> Result<Self>
Create trace context from W3C traceparent header value
Format: “00-{trace_id}-{span_id}-{flags}”
§Example
use celers_broker_sql::TraceContext;
let ctx = TraceContext::from_traceparent(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
).unwrap();
assert_eq!(ctx.trace_id, "4bf92f3577b34da6a3ce929d0e0e4736");
assert_eq!(ctx.span_id, "00f067aa0ba902b7");Sourcepub fn to_traceparent(&self) -> String
pub fn to_traceparent(&self) -> String
Convert to W3C traceparent header value
§Example
use celers_broker_sql::TraceContext;
let ctx = TraceContext::new(
"4bf92f3577b34da6a3ce929d0e0e4736",
"00f067aa0ba902b7"
);
assert_eq!(
ctx.to_traceparent(),
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
);Sourcepub fn is_sampled(&self) -> bool
pub fn is_sampled(&self) -> bool
Check if trace is sampled (should be recorded)
Sourcepub fn create_child_span(&self) -> Self
pub fn create_child_span(&self) -> Self
Generate a new child span ID for this trace
§Example
use celers_broker_sql::TraceContext;
let parent_ctx = TraceContext::new(
"4bf92f3577b34da6a3ce929d0e0e4736",
"00f067aa0ba902b7"
);
let child_ctx = parent_ctx.create_child_span();
// Same trace ID, different span ID
assert_eq!(child_ctx.trace_id, parent_ctx.trace_id);
assert_ne!(child_ctx.span_id, parent_ctx.span_id);Trait Implementations§
Source§impl Clone for TraceContext
impl Clone for TraceContext
Source§fn clone(&self) -> TraceContext
fn clone(&self) -> TraceContext
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TraceContext
impl Debug for TraceContext
Source§impl<'de> Deserialize<'de> for TraceContext
impl<'de> Deserialize<'de> for TraceContext
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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 PartialEq for TraceContext
impl PartialEq for TraceContext
Source§impl Serialize for TraceContext
impl Serialize for TraceContext
impl Eq for TraceContext
impl StructuralPartialEq for TraceContext
Auto Trait Implementations§
impl Freeze for TraceContext
impl RefUnwindSafe for TraceContext
impl Send for TraceContext
impl Sync for TraceContext
impl Unpin for TraceContext
impl UnsafeUnpin for TraceContext
impl UnwindSafe for TraceContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more