Skip to content

Commit c19a8f9

Browse files
committed
Add tracing::w3c_traceparent() helper
1 parent c0b5866 commit c19a8f9

File tree

1 file changed

+19
-2
lines changed
  • foundations/src/telemetry/tracing

1 file changed

+19
-2
lines changed

foundations/src/telemetry/tracing/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub fn trace_id() -> Option<String> {
246246
/// The serialized trace then can be passed to [`start_trace`] by other service to continue
247247
/// the trace.
248248
///
249-
/// Returns `None` if the current span is not sampled and don't have associated trace.
249+
/// Returns `None` if the current span is not sampled and doesn't have an associated trace.
250250
///
251251
/// # Examples
252252
/// ```
@@ -294,6 +294,23 @@ pub fn state_for_trace_stitching() -> Option<SerializableTraceState> {
294294
.map(|c| c.state().clone())
295295
}
296296

297+
/// Returns the value to be used as a W3C traceparent header.
298+
///
299+
/// See: <https://www.w3.org/TR/trace-context/#traceparent-header>
300+
///
301+
/// Returns `None` if the current span is not sampled and doesn't have an associated trace.
302+
pub fn w3c_traceparent() -> Option<String> {
303+
state_for_trace_stitching().map(|state| {
304+
format!(
305+
"00-{:0>16x}{:0>16x}-{:0>16x}-{:0>2x}",
306+
state.trace_id().high,
307+
state.trace_id().low,
308+
state.span_id(),
309+
state.flags()
310+
)
311+
})
312+
}
313+
297314
/// Creates a tracing span.
298315
///
299316
/// If span covers whole function body it's preferable to use [`span_fn`] macro.
@@ -312,7 +329,7 @@ pub fn state_for_trace_stitching() -> Option<SerializableTraceState> {
312329
/// {
313330
/// let _scope = ctx.scope();
314331
/// let _root = tracing::span("root");
315-
///
332+
///
316333
/// {
317334
/// let _span1 = tracing::span("span1");
318335
/// }

0 commit comments

Comments
 (0)