1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Async tracing surface (DESIGN.md §2 async gap).
//!
//! [`scope!`](crate::scope) guards are thread-bound: they enter/exit via
//! thread-local stacks, so a guard held across an `.await` point records
//! against whichever thread the task happens to be polled on. This module
//! provides the async-correct equivalents:
//!
//! - [`root_span!`](crate::root_span) — start (or continue) a trace and
//! install it as the thread-local parent. Bind it per task, before the
//! first `.await`.
//! - [`in_observed_span`] / [`ObservedFutureExt`] — wrap a future in a
//! fastrace span that is entered on every poll, so the span follows the
//! task across threads and suspension points.
//! - [`extract_traceparent`] / [`inject_traceparent`] — W3C
//! `traceparent`-only propagation helpers (DESIGN.md §11b), thin wrappers
//! over fastrace's codec.
//! - [`extract_headers`] / [`inject_headers`] — full W3C trace-context
//! propagation: `traceparent` **and** `tracestate`, via fastrace's
//! [`W3CTraceContext`](fastrace::collector::W3CTraceContext). Use at RPC
//! boundaries where vendor state must survive propagation.
//!
//! With feature `int-futures`, the [`futures`] module re-exports
//! `fastrace-futures` (`StreamExt::in_span` etc.) for `Stream`/`Sink`
//! instrumentation; `FutureExt` itself lives in fastrace core.
use Future;
use SpanContext;
use FutureExt as _;
use LocalParentGuard;
use sealed;
/// `Stream`/`Sink` tracing adapters from `fastrace-futures`
/// (`StreamExt::in_span`, …). `Future` instrumentation needs no extra crate —
/// use [`in_observed_span`].
pub use fastrace_futures as futures;
/// Start a new trace: create a root span with a random
/// [`SpanContext`] and install it as the thread-local parent.
///
/// The returned guard keeps the trace context as the local parent until
/// dropped — child spans (`scope!`, `LocalSpan`, [`in_observed_span`])
/// attach to it. The root span itself is a zero-duration marker submitted
/// immediately; the guard is what carries the trace.
///
/// Drop order matters like any guard: bind it (`let _root = …`) and keep it
/// alive for the whole task/request.
/// Continue an incoming trace: create a root span whose parent is `ctx`
/// (same trace id) and install it as the thread-local parent.
///
/// Use at service boundaries with a context from [`extract_traceparent`].
/// Same drop semantics as [`root_span`].
/// Wrap a future in a fastrace span that enters on poll (async-correct —
/// unlike [`scope!`](crate::scope) guards, which are thread-bound and must
/// never cross `.await`).
///
/// Implemented via [`fastrace::future::FutureExt::in_span`] with
/// [`Span::enter_with_local_parent`](fastrace::Span::enter_with_local_parent):
/// on every `poll` the span becomes the local parent, so nested `scope!` /
/// `LocalSpan` calls inside the future attach to it regardless of which
/// thread the executor polls on. Without an active local parent (see
/// [`root_span!`](crate::root_span)) the span is a no-op, matching fastrace
/// semantics.
/// Extension trait so futures can be wrapped fluently:
/// `my_future.in_observed_span("load")`.
///
/// See [`in_observed_span`] for semantics. Sealed: blanket-implemented for
/// every [`Future`] — not intended for user implementation.
/// Extract a [`SpanContext`] from a W3C `traceparent` header value.
///
/// Thin wrapper over [`SpanContext::decode_w3c_traceparent`]; returns `None`
/// for malformed headers. Only the `traceparent` portion is handled —
/// `tracestate` is not carried by [`SpanContext`] (see fastrace's
/// `W3CTraceContext` if state is needed).
/// Render a [`SpanContext`] as a W3C `traceparent` header value
/// (`00-<trace-id>-<span-id>-<flags>`). Thin wrapper over
/// [`SpanContext::encode_w3c_traceparent`].
/// Decode a W3C trace context from HTTP headers — traceparent AND
/// tracestate (the full propagation contract, unlike [`extract_traceparent`]
/// which carries only traceparent). Returns `None` when the set is
/// malformed or lacks a valid `traceparent`.
/// Render a W3C trace context as HTTP headers — `traceparent` plus
/// `tracestate` when present (the full round-trip complement of
/// [`extract_headers`]).