Repository navigation
Conversation
Previously, a request whose trace was not sampled still got a full span: attributes, wall-clock and monotonic timestamps, response listeners, and status and exception recording. None of it could be exported, since Span.end() buffers only sampled spans, so all of it was wasted work that only propagation and parenting actually needed. Now the handlers check the span once, right after it is created, and skip everything from there that only serves the export: the attribute work (URL parsing, header reads, setAttribute calls) for HTTP/1 servers and clients, HTTP/2 servers, and undici, the timestamps in the Span constructor and end(), and the response 'close' and 'end' listeners, the stashed span, status, exception events, and error handling that records onto the span. What propagation needs stays: trace and span IDs, trace flags, tracestate and baggage, removing a stale tracestate from outgoing requests, making the span current for the request handler, and the self-trace suppression. A sampled span exports the same fields as before, though it now reads its start time just after its trace context is set up rather than just before. An unsampled traced request now costs about a third less of microtel's own time, for HTTP and fetch alike, in CPU profiles of 50k requests. Sampled requests are unchanged. Signed-off-by: Bryan English <bryan@bryanenglish.com>
bengl
marked this pull request as ready for review
November 3, 2026 19:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A trace that isn't sampled is dropped as a whole, but microtel still creates a span for every request in it, so the trace context reaches the services downstream and spans made while handling the request have a parent. Those spans were never exported, so the attributes, timestamps, and listeners they collected were wasted.
What changed. Each handler checks the span once, right after creating it, and an unsampled span skips everything only the export reads:
setAttribute()calls, for HTTP/1 servers and clients, HTTP/2 servers, and undiciSpanconstructor and inend()What propagation and parenting need stays: trace and span IDs, trace flags,
tracestateandbaggage, removing a staletracestatefrom outgoing requests, making the span current for the request handler, and the self-trace check. A sampled span exports the same fields as before, behind one more branch, though it now reads its start time just after its trace context is set up rather than just before. ASpan.isSampledgetter exposes the decision.Performance. On a quiet machine, against a copy of main, on Node 26.12.1. microtel's own CPU time in profiles of 50k traced requests at
sampleRatio: 0, over 7 interleaved runs:Sampled requests didn't change: 193.1 ms on main against 191.4 ms, over 8 interleaved runs at the default ratio.
Throughput of the new unsampled benchmarks, over 8 interleaved node:bench runs:
Raw samples
benchmark/http-unsampled.bench.jsandbenchmark/fetch-unsampled.bench.jsare new, so the CI benchmark comparison tracks this configuration from now on. The shared benchmark helper now passessampleRatiothrough toinit().Not taken. Lazy attribute or timestamp storage. An unsampled span simply has none of the state the export reads, behind one undefined check in
setAttribute()andaddEvent(), and every span keeps the same shape.Tests. A child process at
sampleRatio: 0drives sampled and unsampled requests through an HTTP frontend, fetch, and HTTP/2, and reports what each hop saw. An unsampled request leaves no microtel listener on its responses, the same counts plain Node has, and the HTTP/2 stream and the fetch hold no span for the export-only handlers. A direct test checks that an unsampled span gets no timestamps, attributes, or events, and thatend()only marks it ended. Itstraceparent,tracestate, andbaggagestill propagate, replacing a caller's stale ones the same way a sampled request does, and only the request that arrived sampled exports anything.