Title: Next.js middleware: `normalizedRequest` never reaches `tracesSampler` on the edge runtime, so #21833 has no effect there · Issue #22200 · getsentry/sentry-javascript · GitHub
Open Graph Title: Next.js middleware: `normalizedRequest` never reaches `tracesSampler` on the edge runtime, so #21833 has no effect there · Issue #22200 · getsentry/sentry-javascript
X Title: Next.js middleware: `normalizedRequest` never reaches `tracesSampler` on the edge runtime, so #21833 has no effect there · Issue #22200 · getsentry/sentry-javascript
Description: Summary #21833 wired the isolation scope's normalizedRequest into the tracesSampler sampling context for root spans, so a custom sampler can decide from the incoming request. Its stated motivation is exactly the use case below ("drop hea...
Open Graph Description: Summary #21833 wired the isolation scope's normalizedRequest into the tracesSampler sampling context for root spans, so a custom sampler can decide from the incoming request. Its stated motivation ...
X Description: Summary #21833 wired the isolation scope's normalizedRequest into the tracesSampler sampling context for root spans, so a custom sampler can decide from the incoming request. Its stated motivat...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/22200
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Next.js middleware: `normalizedRequest` never reaches `tracesSampler` on the edge runtime, so #21833 has no effect there","articleBody":"## Summary\n\n[#21833](https://github.com/getsentry/sentry-javascript/pull/21833) wired the isolation scope's `normalizedRequest` into the `tracesSampler` sampling context for root spans, so a custom sampler can decide from the incoming request. Its stated motivation is exactly the use case below (\"drop health-check routes\").\n\n**On the Next.js edge runtime (middleware) the value never arrives.** `tracesSampler` is called for every `middleware GET` root span with `normalizedRequest: undefined`, so the request cannot be inspected at all. The Node runtime, in the same app and with the same sampler, gets the full header bag.\n\nThe practical consequence is that **middleware traces cannot be sampled by anything about the request** (no URL, no route, no User-Agent), so a sampler cannot drop synthetic traffic there.\n\n## Versions\n\n| | |\n|---|---|\n| `@sentry/nextjs` | 10.63.0 |\n| `@sentry/core` | 10.63.0 |\n| `@sentry/vercel-edge` | 10.63.0 |\n| `next` | 16.2.10 |\n| Node | 22.22.0 |\n\nReproduced on a real `next build && next start`, and observed in production on Vercel.\n\n## Evidence\n\nI instrumented my `tracesSampler` to log what it is handed, then drove one request with a marked User-Agent and one without:\n\n```\n[SAMPLER-PROBE] {\"name\":\"GET /health\", \"hasNormalizedRequest\":true, \"headerKeys\":[\"host\",\"user-agent\",\"accept\"], \"ua\":\"Mozilla/5.0 ... my-e2e/1\", \"rate\":0}\n[SAMPLER-PROBE] {\"name\":\"middleware GET\", \"hasNormalizedRequest\":false, \"headerKeys\":[], \"rate\":1}\n[SAMPLER-PROBE] {\"name\":\"GET /health\", \"hasNormalizedRequest\":true, \"headerKeys\":[\"host\",\"user-agent\",\"accept\"], \"ua\":\"Mozilla/5.0 ...\", \"rate\":1}\n[SAMPLER-PROBE] {\"name\":\"middleware GET\", \"hasNormalizedRequest\":false, \"headerKeys\":[], \"rate\":1}\n```\n\n`hasNormalizedRequest` is **`false` for every middleware span**, including the one whose request carried the marker. The header bag is empty. The Node span, one line above and in the same request, gets the full bag.\n\n`beforeSendTransaction` cannot recover it either: a `event.request.headers['user-agent']` check never fires on these events, which suggests the finished transaction has no request data either, not just the sampling context.\n\n## What I traced in the source (the chain looks correct, and the value still does not arrive)\n\n- `@sentry/nextjs/build/cjs/common/wrapMiddlewareWithSentry.js:33` calls `isolationScope.setSDKProcessingMetadata({ normalizedRequest: winterCGRequestToRequestData(req) })`, **before** `startSpan` on line 51.\n- `winterCGRequestToRequestData` (`@sentry/core` `utils/request.js`) returns `{ method, url, query_string, headers }` with the full header dict.\n- `@sentry/core` `tracing/trace.js` builds the sampling context with `normalizedRequest: getIsolationScope().getScopeData().sdkProcessingMetadata.normalizedRequest`.\n- `tracing/sampling.js` calls `options.tracesSampler(...)` first, with no short-circuit.\n- The sampler is definitely installed on edge (it is in `sentry.edge.config.ts`, and it runs: the probe above is its own output).\n\nSo every link exists, and the isolation scope the wrapper writes to does not appear to be the one the sampler reads from on this runtime. I have not chased which of the two it is.\n\n## Steps to reproduce\n\n1. Next.js app with `@sentry/nextjs` and a `middleware.ts` matching normal routes.\n2. In `sentry.edge.config.ts` **and** `sentry.server.config.ts`, install the same sampler:\n\n```ts\nSentry.init({\n dsn: process.env.SENTRY_DSN,\n tracesSampler: (ctx) => {\n console.log(JSON.stringify({\n name: ctx.name,\n hasNormalizedRequest: Boolean(ctx.normalizedRequest),\n headerKeys: Object.keys(ctx.normalizedRequest?.headers ?? {}),\n }));\n return 1;\n },\n});\n```\n\n3. `next build && next start`, then `curl http://localhost:3000/any-page`.\n4. The `middleware GET` line reports `hasNormalizedRequest: false` with an empty `headerKeys`. The Node route's line reports `true` with the headers.\n\n## Expected\n\n`tracesSampler` receives `normalizedRequest` for middleware root spans on the edge runtime, as it does on Node, so #21833's \"decide from the incoming request\" works on every runtime.\n\n## Actual\n\n`normalizedRequest` is `undefined` for every edge (middleware) root span, so the sampler has only `ctx.name` to go on.\n\n## Workaround, for anyone who finds this\n\nThe only thing the sampler is given on edge is the span name, so I match on it and refuse the trace outright:\n\n```ts\nconst isMiddlewareSpan = (name?: string) =>\n typeof name === 'string' && (name === 'middleware' || name.startsWith('middleware '));\n\ntracesSampler: (ctx) => (isMiddlewareSpan(ctx.name) ? 0 : /* your real policy */ 1),\n```\n\nThis costs the middleware traces of real users too, which is only acceptable because those spans land with no url, no route and no User-Agent anyway, so they cannot be attributed to a user or an endpoint. Errors are unaffected (`captureException` is governed by `sampleRate`, not `tracesSampler`).\n\nFor context on why this mattered enough to chase: these unsampleable middleware spans, plus the Supabase auth call each one makes as a child of the same trace, were ~90% of the spans our CI was still sending after we thought we had silenced it.","author":{"url":"https://github.com/gerokeller","@type":"Person","name":"gerokeller"},"datePublished":"2026-07-12T14:57:17Z","url":"https://github.com/getsentry/sentry-javascript/issues/22200"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:35f42a56-360f-2da7-0f34-0ada90a9bb16 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8BD8:2F6550:1B82F3B:247B6DD:6B0426E1 |
| html-safe-nonce | e587c84b14457af8b0d41b22f31b0c0202c7daa21d68143a7908a371cfcd1d3f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QkQ4OjJGNjU1MDoxQjgyRjNCOjI0N0I2REQ6NkIwNDI2RTEiLCJ2aXNpdG9yX2lkIjoiNDIxNzgxMzc5Mzc4MDI3OTAwOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fb9b25bacceb28966cdec1e609c41d47f52468fa03161931f93909ce15810aa8 |
| hovercard-subject-tag | issue:4867508465 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/22200/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d448bba0ca4375f060baf0f4ee556166180ad826461e20f27569b2a4eb55c8b2/getsentry/sentry-javascript/issues/22200 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d448bba0ca4375f060baf0f4ee556166180ad826461e20f27569b2a4eb55c8b2/getsentry/sentry-javascript/issues/22200 |
| og:image:alt | Summary #21833 wired the isolation scope's normalizedRequest into the tracesSampler sampling context for root spans, so a custom sampler can decide from the incoming request. Its stated motivation ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gerokeller |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width