Skip to content

fix(platform): Restore middleware spans at 1% with request attributes - #18887

Merged
sergical merged 3 commits into
masterfrom
sd/middleware-span-sampling
Jul 31, 2026
Merged

fix(platform): Restore middleware spans at 1% with request attributes#18887
sergical merged 3 commits into
masterfrom
sd/middleware-span-sampling

Conversation

@sergical

@sergical sergical commented Jul 30, 2026

Copy link
Copy Markdown
Member

DESCRIBE YOUR PR

#18775 dropped middleware root spans entirely; this restores them at a blind 1% (~24k/day), blind because Next.js creates the Middleware.execute root in a detached sandbox before any request data reaches Sentry, so tracesSampler can never classify it. middleware.ts tags that span with middleware.outcome (redirect/rewrite/passthrough), url.path, traffic_type, and device_type — attributes rather than a span name, because the SDK's enhanceMiddlewareRootSpan unconditionally rewrites Middleware.execute names to middleware {METHOD} on the send path and ignores sentry.source. isMiddlewareRootSpan now matches both http.server.middleware (SDK v10) and middleware (v11) so detection survives the upgrade.

Note that v10 still emits a duplicate nested middleware span, so volume may be ~2x until getsentry/sentry-javascript#22904 lands — unrelated to this rate, and a non-zero rate is what makes that fix observable here at all. To verify after deploy: spans · span.op:http.server.middleware · release:<new sha> is non-zero at ~1% of request volume with middleware.outcome populated, and docs.request.classified grouped by traffic_type is unchanged.

IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs to go live.

  • Urgent deadline (GA date, etc.):
  • Other deadline:
  • None: Not urgent, can wait up to 1 week+

SLA

  • Teamwork makes the dream work, so please add a reviewer to your PRs.
  • Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it.
    Thanks in advance for your help!

PRE-MERGE CHECKLIST

Make sure you've checked the following before merging your changes:

  • Checked Vercel preview for correctness, including links
  • PR was reviewed and approved by any necessary SMEs (subject matter experts)
  • PR was reviewed and approved by a member of the Sentry docs team

🤖 Generated with Claude Code

@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 31, 2026 3:53pm
sentry-docs Ready Ready Preview Jul 31, 2026 3:53pm

Request Review

Comment thread middleware.ts Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 42e0e02. Configure here.

Comment thread middleware.ts Outdated

@chargome chargome left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

re: Duplicate middleware spans, we fixed this on the upcoming v11 version but I"ll try to backport the fix to v10 today

sergical and others added 2 commits July 31, 2026 08:41
Middleware roots now sample at a fixed 1% (~24k spans/day at current traffic) instead of 0. Because the rate is applied blind, bots are included in proportion to their share of traffic; middleware.ts stamps traffic_type onto the span so they stay filterable at query time.

middleware.ts also names the span, which is what made these spans worth keeping. Next.js collapses the name to 'middleware GET'; setting sentry.source to 'custom' from inside the middleware stops the SDK reclaiming it. Spans are named by outcome (redirect / rewrite / passthrough) rather than by path — the docs site has thousands of paths plus every file under /mdx-images/, so naming by URL would blow up transaction-name cardinality. The path is kept as the url.path attribute.

Note that send-time filtering is not an option here: beforeSendSpan is typed (span: SpanJSON) => SpanJSON with no null return, and with traceLifecycle 'stream' it is the only span hook, so spans cannot be dropped after creation. The blind sample rate is the only lever.

docs.request.classified remains the unsampled system of record for traffic counting and is untouched.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
No behavior change. Every span implementation reachable here returns `this` from updateName (SentrySpan, SentryNonRecordingSpan, OTel sdk-trace-base Span, OTel API NonRecordingSpan), so the chained form was safe, but core's own updateSpanName helper uses separate statements and this doesn't depend on a fluent return we don't control.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…naming it

The outcome-based span name never survived. The SDK's enhanceMiddlewareRootSpan rewrites the name of every Middleware.execute span to `middleware {METHOD}` on the send path, reading Next.js' next.span_name attribute and ignoring sentry.source, so the rename here was silently discarded. Drop it and keep the redirect/rewrite/passthrough breakdown as the middleware.outcome attribute, which is queryable and keeps name cardinality flat.

Also match both middleware span ops in the sampler: http.server.middleware in v10, middleware in v11. next.span_type stays the load-bearing check either way.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sergical sergical changed the title fix(platform): Restore middleware spans at 1% with route detail fix(platform): Restore middleware spans at 1% with request attributes Jul 31, 2026
@sergical
sergical merged commit 16d8de5 into master Jul 31, 2026
24 checks passed
@sergical
sergical deleted the sd/middleware-span-sampling branch July 31, 2026 15:56
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.

2 participants