-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
DB Integration & Observability
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Summary
SpanTelemetryContextImpl tracks which span is current for an operation, so a new span can find its parent. It keeps that as an OpenTelemetry Context behind a std::mutex. A Context is a persistent linked list, so every update allocates, and reading it when no span is set allocates a DefaultSpan. That cost is paid on every traced request and, because the first span of every request sets up a context, on untraced requests too.
Keep the current span as a SpanContext value instead, and build the OpenTelemetry Context only when propagating.
Background: what the code does today
- Each Context::SetValue makes 3 heap allocations (a list node, its shared_ptr control block, and a copy of the key). A span calls setSpan twice, when it starts and when it ends, so about 6 allocations per span.
- getSpan() returns GetSpan(ctx), which allocates a DefaultSpan whenever no span is set. That is the case for the first span of every request, including requests the sampler then rejects, because Span::_start stores a telemetry context on the operation either way.
- Every access takes a mutex, and restoring the parent ref-counts a shared_ptr<Span>.
Change
- SpanTelemetryContextImpl holds the current span's SpanContext by value under a mongo::SpinLock. Spans start and end on one thread, but clone() and propagate() may run on others. getSpan()/setSpan() become getSpanContext()/setSpanContext()/exchangeSpanContext().
- Span::SpanImpl makes itself current with one swap and restores its parent's SpanContext by value when it ends.
- propagate() builds the OpenTelemetry Context only when injecting, and returns early when no span is active. Operations whose span was not sampled still carry a context and reach propagate() for every outgoing request. HttpTraceContext::Inject already skips an invalid span, so the output is unchanged.
- The context can still be constructed from an OpenTelemetry Context, which is how a remote parent extracted from an incoming request is continued.
Results
crud_tracing_bm interleaved benchmark, find by _id (IDHACK shape). Baseline, trace-miss and traced requests alternate in one process, so drift cancels. Instruction counts, median of 3 runs, 8-vCPU arm64 dev host, opt build. The base is the fork point of this change.
| Per request | Master | This change |
|---|---|---|
| Trace-miss overhead (tracing on, span not sampled) | 8,285 instr | 7,560 instr (-8.8%) |
| Traced overhead | 17,545 instr | 14,535 instr (-17.2%) |
| Request with tracing off | 76,598 instr | 76,583 instr |
Outgoing request carrying the context (TelemetryContextSerializer::toSection), per call:
| Per call | Master | This change |
|---|---|---|
| Operation with no active span | 117 ns / 962 instr | 49 ns / 242 instr |
| Operation with an active span | 242 ns / 1,950 instr | 425 ns / 3,435 instr |
The sampled-operation path is slower because the OpenTelemetry Context is now built per call. It only affects sampled operations.
Lock choice: std::mutex instead of SpinLock costs +90 instructions per untraced request and +280 per traced one. Removing the lock entirely saves at most 25, so SpinLock is kept.
Acceptance criteria
- Behavior is unchanged: parent/child relationships, remote-parent continuation, clone() sharing the sampling roll, and the propagated traceparent.
- span_telemetry_context_impl_test (including the concurrent set/clone/propagate/get test from
SERVER-131480), span_test and tracing_test pass, and the required patch passes.}}
- is related to
-
SERVER-131480 Make SpanTelemetryContextImpl thread-safe
-
- Closed
-
- related to
-
SERVER-137201 Switch from using SDK-provided Spans to using a lightweight implementation
-
- Investigating
-