hyperlane_plugin_tracing/struct.rs
1use super::*;
2
3/// Active W3C Trace Context and request correlation metadata stored in Hyperlane [`Context`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct TraceContext {
6 /// 32-character lowercase hex 128-bit W3C trace identifier.
7 pub(crate) trace_id: String,
8 /// 16-character lowercase hex 64-bit W3C span identifier for this hop.
9 pub(crate) span_id: String,
10 /// Upstream parent span identifier if an incoming `traceparent` was propagated.
11 #[get(type(clone))]
12 pub(crate) parent_span_id: Option<String>,
13 /// Correlation `X-Request-Id` value (reused from incoming header or minted from `trace_id`).
14 pub(crate) request_id: String,
15 /// Whether this trace carries the W3C sampled flag (`0x01`).
16 #[get(type(copy))]
17 pub(crate) sampled: bool,
18 /// Optional incoming `tracestate` propagated unchanged.
19 #[get(type(clone))]
20 pub(crate) tracestate: Option<String>,
21}
22
23/// Completed request span emitted to registered [`SpanExporter`] backends.
24#[derive(Clone, Data, Debug, Eq, PartialEq)]
25pub struct SpanRecord {
26 /// 128-bit hex trace identifier.
27 pub(crate) trace_id: String,
28 /// 64-bit hex span identifier.
29 pub(crate) span_id: String,
30 /// Optional parent span identifier.
31 #[get(type(clone))]
32 pub(crate) parent_span_id: Option<String>,
33 /// Correlation request identifier.
34 pub(crate) request_id: String,
35 /// HTTP request method string.
36 pub(crate) method: String,
37 /// HTTP request path.
38 pub(crate) path: String,
39 /// HTTP response status code.
40 #[get(type(copy))]
41 pub(crate) status_code: usize,
42 /// Total request processing time in microseconds.
43 #[get(type(copy))]
44 pub(crate) duration_us: u64,
45 /// Whether this trace was marked sampled.
46 #[get(type(copy))]
47 pub(crate) sampled: bool,
48}
49
50/// Configuration for [`TracingPlugin`].
51#[derive(Clone, Data, Debug, Eq, PartialEq)]
52pub struct TracingConfig {
53 /// Emits `traceparent` on responses when `true`.
54 #[get(type(copy))]
55 pub(crate) emit_traceparent: bool,
56 /// Emits `X-Request-Id` on responses when `true`.
57 #[get(type(copy))]
58 pub(crate) emit_request_id: bool,
59 /// Emits `Server-Timing` latency header on responses when `true`.
60 #[get(type(copy))]
61 pub(crate) emit_server_timing: bool,
62 /// Metric label used in `Server-Timing` (default `"app"`).
63 pub(crate) server_timing_label: String,
64 /// Default sampled flag assigned to newly originated traces.
65 #[get(type(copy))]
66 pub(crate) default_sampled: bool,
67}
68
69/// Process-wide [`TracingPlugin`] singleton storing [`TracingConfig`] and [`SpanExporter`] hooks.
70#[derive(Clone)]
71pub struct TracingPlugin {
72 /// Active tracing configuration.
73 pub(crate) config: TracingConfig,
74 /// Registered span exporters invoked upon response completion.
75 pub(crate) exporters: Arc<Vec<SharedSpanExporter>>,
76}
77
78/// Opt-in request middleware establishing [`TraceContext`] and setting correlation headers early.
79#[derive(Clone, Copy, Debug, Default)]
80#[hyperlane::request_middleware(-130)]
81pub struct TracingRequestHook;
82
83/// Opt-in response middleware recording latency, setting `Server-Timing`, and exporting spans.
84#[derive(Clone, Copy, Debug, Default)]
85#[hyperlane::response_middleware(90)]
86pub struct TracingResponseHook;