Skip to content

perf(server-utils): Skip Fastify hook spans on unsampled requests - #26711

Open
JonasBa wants to merge 1 commit into
getsentry:developfrom
JonasBa:perf/fastify-skip-hook-spans-unsampled
Open

JonasBa wants to merge 1 commit into
getsentry:developfrom
JonasBa:perf/fastify-skip-hook-spans-unsampled

Conversation

@JonasBa

@JonasBa JonasBa commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

On unsampled requests, the Fastify integration no longer starts a span for each hook and for the route handler. Hooks and handlers run as before, with the same this, arguments, return values and errors. Sampled requests produce the same spans as before.

Why

On an unsampled request, every hook span is a non-recording span that is never sent, but each one still forks the scope, sets an active span and builds its attributes, so apps with many hooks pay for it on every request. The check uses spanIsSampled, which is how startSpan decides a child's sampling, rather than isRecording(): onResponse hooks run after the request span has ended and fall back to the active span, and an ended sampled span is not recording. Tradeoff: the skipped spans no longer record a sample_rate client outcome.

Fastify 5.12, 20 no-op async instance hooks (5 each on onRequest, preHandler, onSend and onResponse), app.inject() in a loop with the AsyncLocalStorage context strategy, median of 7 runs of 20,000 requests, Node 26 on an M4 Pro:

tracesSampleRate Build Spans started per request µs per request
0 develop 22 93.9
0 this PR 1 15.8
1 develop 22 106.5
1 this PR 22 106.8

Closes: #26710

Co-Authored-By: Cursor Agent <cursoragent@cursor.com>
@github-actions github-actions Bot added the external PR from an external contributor label Nov 20, 2026
@JonasBa
JonasBa marked this pull request as ready for review November 20, 2026 18:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fastify integration starts a span per hook on unsampled requests

1 participant