Skip to content

fix(platform): Re-enable middleware span sampling - #18886

Draft
sentry-junior[bot] wants to merge 1 commit into
masterfrom
cg/reenable-middleware-spans
Draft

fix(platform): Re-enable middleware span sampling#18886
sentry-junior[bot] wants to merge 1 commit into
masterfrom
cg/reenable-middleware-spans

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Re-enables middleware span ingestion on docs by removing the hard drop of Next.js Middleware.execute roots in tracesSampler.

Why

#18775 intentionally sampled middleware roots at 0 because they were unclassifiable noise at the time. We want middleware spans back for investigation / visibility.

SDK instrumentation still emits http.server.middleware on current @sentry/nextjs — only the app sampler was suppressing them. No SDK bump required.

Change

  • Remove isMiddlewareRootSpan early-return (return 0) from src/tracesSampler.ts
  • Middleware roots now use the same traffic-based rates as other roots (header classification when available, otherwise UA / unknown)
  • Keep docs.request.classified in middleware.ts as the unsampled request counter

Verify after deploy

span.op:http.server.middleware release:<new sha>

Should go non-zero on sentry/docs.

Risks

  • Middleware roots may still often classify as unknown when sampled before request headers are available, so volume can be higher than classifiable server spans
  • Possible temporary duplicate child spans from the SDK wrapper until the unreleased JS SDK change that stops starting a wrapper span lands

Test plan

  • Review sampler diff
  • After merge/deploy, confirm middleware spans appear for the new release
  • Confirm docs.request.classified still flows

Requested by Charly Gomez via Junior.

--

View Junior Session

Stop hard-dropping Middleware.execute roots in tracesSampler so
middleware spans are sampled again with the normal traffic rules.
Keep docs.request.classified as the unsampled traffic counter.

Co-Authored-By: Charly Gomez <charly.gomez@sentry.io>
@sentry-junior
sentry-junior Bot requested review from chargome and sergical July 30, 2026 14:49
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
develop-docs Ready Ready Preview Jul 30, 2026 2:59pm
sentry-docs Ready Ready Preview Jul 30, 2026 2:59pm

Request Review

@sergical

Copy link
Copy Markdown
Member

Superseded by #18887 — same goal, different sampling shape.

This PR removes isMiddlewareRootSpan entirely, so middleware roots fall through to the normal classification path. Worth noting what that resolves to in practice: there's no user-agent available on a middleware root — Next.js creates the Middleware.execute span in a detached sandbox before any request data reaches Sentry — so the UA fallback never has anything to match on and the rate is always SAMPLE_RATES.unknown (30%). At current traffic (~2.4M requests/day through the middleware, per docs.request.classified) that's ~720k spans/day, which is roughly 3x the ~264k/day that motivated #18775.

#18887 keeps the detection and returns 1% instead of 0 (~24k/day), and additionally names the spans from inside middleware.tsmiddleware GET redirect / rewrite / passthrough with url.path, traffic_type, and device_type attributes. The naming is the part that makes them worth ingesting; without it you get 720k/day of bare middleware GET with no route detail, which is what the old beforeSendTransaction hack was trying to work around.

Also carried your duplicate-child-span note over to #18887 as an open question — if that SDK behavior is real, actual volume there could be ~2x the estimate. Happy to raise the 1% if 24k/day turns out too thin for the investigation you had in mind.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant