Skip to content

feat: Answer managed tunnel requests in the runtimes - #25917

Draft
logaretm wants to merge 15 commits into
developfrom
awad/runtime-tunnel-interception
Draft

logaretm wants to merge 15 commits into
developfrom
awad/runtime-tunnel-interception

Conversation

@logaretm

Copy link
Copy Markdown
Member

Every framework needed its own middleware to serve the managed tunnel, which kept breaking on catch-all routes and ordering, fixed by answering tunnel requests in the Node, Bun, Deno, Cloudflare and Lambda runtimes and dropping the framework hooks.

This stacks on #25588 (its two commits are included until it merges).

isaacs and others added 9 commits October 29, 2026 12:22
Moves Cloudflare's `wrapRequestHandler` onto `wrapServerRequestHandler`,
which `Deno.serve` and `Bun.serve` use since the previous PRs. Cloudflare
keeps only what is unique to it: client init, the `cf` and cloud
resource contexts, the protocol from `cf.httpProtocol`, the
`isStreamingResponse` option, `captureErrors`, and the flush through
`waitUntil`, which skips the dispose for a 101 response. The helpers
that only the old code used (`captureIncomingRequestBody`,
`shouldCreateIncomingRequestSpan`, `getIncomingRequestSpanHooks` and
`addRequest`) are gone.

`wrapRequestHandler` must run on workers without the `nodejs_compat`
flag, such as Shopify Oxygen, and every `@sentry/server-utils` entry
loads `node:async_hooks`. So the wrapper moves to a new entry,
`@sentry/server-utils/request-handler`, with no Node.js imports. Deno
and Bun import it from there too. Cloudflare's module graph test allows
this one entry, and a new test in server-utils walks the entry's module
graph and fails on any Node.js import.

The wrapper gets three optional fields: `isStreamingResponse`, `onDone`
(called once when the request is done, with the response if there is
one), and a `mechanism` that can be left out to not capture errors.

For streamed responses, the shared stream monitor replaces Cloudflare's
`TransformStream`. Workers with a compatibility date before 2022-11-30
cannot construct a `ReadableStream` without the
`streams_enable_constructors` flag, so the wrapper then ends at handler
return, as the `TransformStream` fallback did. The monitor now locks the
source body only in `start()`, so the original response stays usable in
that case. The wrapper builds the new response from the original one,
which keeps `encodeBody: 'manual'`.

Visible changes for Cloudflare:

- Request spans set `client.address` from `CF-Connecting-IP` (or
  another forwarding header), as on the other runtimes. This follows
  `dataCollection.userInfo`, which is `true` by default.
- Request spans get the response header attributes, and events get the
  `response` context.
- An error from the source of a streamed response is captured (unless
  `captureErrors` is `false`), and the span gets an error status.
- If the user removed `httpServerIntegration`, request bodies follow
  `dataCollection.httpBodies`. Before, the SDK attached no body.

The minified `withSentry` bundle grows by about 2.4 KB (212,385 to
214,853 bytes), under the 211 KiB limit. Most of it is the request
session and client address code, which Cloudflare now shares.

Fixes #25577
Fixes JS-3975

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

Without `AsyncLocalStorage` (for example on Shopify Oxygen, or on
Workers without `nodejs_compat`), the SDK uses the stack fallback, and
`withInvocationIsolationScope` does not fork. The scope it passes to its
callback is a clone that is not the current isolation scope. The shared
request handler wrote the request data and body to that clone, so an
event captured in the handler, which reads the current scope, had no
request body. The `hydrogen-react-router-7` E2E test caught this.

The handler now gets the current isolation scope. With
`AsyncLocalStorage`, that is the same scope as before. Without it, the
request data goes where the body went before the move to the shared
handler, and events now also get the request URL and method.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
With `serveTunnelIntegration`, Node, Bun and Deno servers and Cloudflare Workers now answer tunnel requests before
the app sees them, whatever framework runs on top. `intercept: false` opts out, for apps that serve the tunnel from
their own code.

The tunnel never throws into the runtime: before a request is claimed any failure hands it to the app, after that it
ends in a 502. The forward to Sentry is aborted when the browser goes away, and the http server instrumentation skips
tunnel requests.
The runtimes answer managed tunnel requests now, so the framework SDKs stop answering them in their own request
hooks. They keep passing their base path, and TanStack Start's `tunnelRoute` still enables the tunnel.
The Node, Bun and Deno SDKs install their tunnel hooks in `init` rather than from `serveTunnelIntegration`, so a
tunnel a framework SDK enables, like TanStack Start's `tunnelRoute`, is answered too. The hooks answer nothing
unless the tunnel is on, so `serveTunnelIntegration` is core's again on every runtime.
Lambda runs no HTTP server, so `wrapHandler` answers tunnel requests from API Gateway, function URL and ALB events
before the handler runs.
`sentryCloudflareNitroPlugin` passes the body Nitro already read, so the runtime can answer tunnel requests. On
Nitro v3, which has no `localFetch`, it wraps `fetch` instead of throwing.
Pages builds get the same Worker entry wrap as Workers again, which answers managed tunnel requests. It was dropped
in #19558 only to keep it out of the bundle.
Lambda runs no HTTP server, so when `init` runs from a preload with the tunnel on, it redirects `_HANDLER` to a
shim that answers tunnel requests from HTTP events before the handler. `@sentry/aws-serverless` keeps its own
shim, and the handler resolution and tunnel helpers move to `@sentry/node` so both share them.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 5845bd4 (PR test merge using develop at 0041bc6)

Against: 0041bc6 (develop) · base run #64433

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.39 kB - -
@sentry/browser - with treeshaking flags 28.53 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.42 kB - -
@sentry/browser (incl. Tracing) 52.7 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.71 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.68 kB - -
@sentry/browser (incl. Tracing, Replay) 93.01 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.6 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.75 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.74 kB - -
@sentry/browser (incl. Feedback) 47.97 kB - -
@sentry/browser (incl. sendFeedback) 35.49 kB - -
@sentry/browser (incl. FeedbackAsync) 40.6 kB - -
@sentry/browser (incl. Metrics) 31.42 kB - -
@sentry/browser (incl. Logs) 31.72 kB - -
@sentry/browser (incl. Metrics & Logs) 32.44 kB - -
@sentry/react 32.27 kB - -
@sentry/react (incl. Tracing) 55.07 kB - -
@sentry/vue 38.66 kB - -
@sentry/vue (incl. Tracing) 55.72 kB - -
@sentry/svelte 30.42 kB - -
@sentry/remix (Remix 3 client bundle) 57.79 kB - -
CDN Bundle 32.18 kB - -
CDN Bundle (incl. Tracing) 53.23 kB - -
CDN Bundle (incl. Logs, Metrics) 34.45 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.17 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.86 kB - -
CDN Bundle (incl. Tracing, Replay) 91.44 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.55 kB - -
CDN Bundle - uncompressed 94.62 kB - -
CDN Bundle (incl. Tracing) - uncompressed 157.58 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.09 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 163.31 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 232.65 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 279.27 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 284.99 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.97 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 298.69 kB - -
@sentry/nextjs (client) 57.87 kB - -
@sentry/sveltekit (client) 53.22 kB - -
@sentry/core/server 44.58 kB +0.78% +345 B 🔺
@sentry/core/browser 13.93 kB - -
@sentry/node 156.77 kB +0.91% +1.41 kB 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.08 kB - -
@sentry/node - without tracing 98.11 kB +1.49% +1.44 kB 🔺
@sentry/node - without channel injection 137.22 kB +1.1% +1.49 kB 🔺
@sentry/aws-serverless 105.78 kB +0.89% +926 B 🔺
@sentry/cloudflare (withSentry) - minified 201.91 kB +2.03% +4 kB 🔺
@sentry/cloudflare (withSentry) 501.89 kB +2.04% +10 kB 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 222.33 kB +1.83% +3.99 kB 🔺

View base workflow run

@github-actions

Copy link
Copy Markdown
Contributor

One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 2 not successful, 52 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Failing after 32s Details
❌ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Failing after 2m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 31s Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 40s Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 25s Details
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 29s Details
✅ CI: Optional E2E Tests / E2E node-eve Test (optional) Successful in 57s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 16s Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 47s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 24s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Successful in 18s Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 25s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 24s Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 18s Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 25s Details

A framework's server bundle can carry its own copy of `@sentry/core`, so the base path and `tunnelRoute` defaults it set never reached the runtime's copy that answers the tunnel.
Its dynamic `require` made webpack warn in Next.js builds. Only the handler shims need it.
Non-POST requests to the tunnel path now go through the app.
A tunnel request from a prerendered page on a cold server reached the app, because the server config only ran from SSR pages and the middleware. Builds now also import it from the server entry, and the server `init` keeps the first client, so a `--import` preload doesn't initialize twice.
Runs the same cases against Node, Cloudflare, Bun and Deno servers, checking the app keeps serving after each, the tunnel answers what it owns, and nothing is reported to the app's project.
`@sentry/node` doesn't answer the tunnel on Bun, and Bun drops pipelined requests behind an async one. `packages/bun` runs the same cases against Bun's servers.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants