Skip to main content

running_process/broker/server/
trace_context.rs

1//! Trace context captured from the v1 broker frame.
2
3use crate::broker::protocol::Frame;
4
5/// W3C trace context plus the broker frame request id.
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
7pub struct TraceContext {
8    /// Broker frame request id.
9    pub request_id: u64,
10    /// W3C traceparent header value.
11    pub traceparent: String,
12    /// W3C tracestate header value.
13    pub tracestate: String,
14}
15
16impl TraceContext {
17    /// Capture trace metadata from a broker frame.
18    pub fn from_frame(frame: &Frame) -> Self {
19        Self {
20            request_id: frame.request_id,
21            traceparent: frame.traceparent.clone(),
22            tracestate: frame.tracestate.clone(),
23        }
24    }
25
26    /// Return the non-empty W3C headers in backend-forwarding order.
27    pub fn backend_headers(&self) -> Vec<(&'static str, String)> {
28        let mut headers = Vec::new();
29        if !self.traceparent.is_empty() {
30            headers.push(("traceparent", self.traceparent.clone()));
31        }
32        if !self.tracestate.is_empty() {
33            headers.push(("tracestate", self.tracestate.clone()));
34        }
35        headers
36    }
37}