Commit 4b92c64
authored
fix(nextjs): universal random tunnel path support (#18257)
Summary for changelog: The `tunnelRoute: true` option didn't work well with Turbopack due to repeated runs of the config files leading to different tunnel URLs in client, server and edge runtimes, this PR fixes that while also fixing Sentry requests spans not being dropped by the sampler.
When using Next.js with Turbopack and the Sentry tunnel route feature
(`tunnelRoute: true`), several issues prevented events from being sent
properly:
### 1. Tunnel Route Consistency (Turbopack)
**Problem**: Random tunnel routes were generated separately for client
and server builds in Turbopack.
**Solution**: Implemented processs-level caching in
`withSentryConfig.ts`:
- Extract tunnel route resolution into `resolveTunnelRoute()` function
- Use `process.env` to store the random tunnel value across
server/client builds.
### 2. Filter Tunnel Request Spans
**Problem**: Requests to the tunnel route (before rewrite) and to Sentry
ingest URLs (after rewrite) were creating spans that polluted Sentry
with internal instrumentation noise, spans were being created by the
middleware and OTEL node.js fetch instrumentation.
**Solution**: Implemented server-side span filtering:
- Created `dropMiddlewareTunnelRequests()` utility to detect and drop
tunnel-related spans
- Filter spans originating from `Middleware.execute` (Next.js
middleware)
- Filter spans originating from `auto.http.otel.node_fetch` (Node.js
fetch instrumentation)
- Check both local tunnel paths and Sentry ingest URLs (using
`isSentryRequestSpan` from `@sentry/opentelemetry`)
- Mark matching spans with `TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION` to
prevent them from being sent
- I tried `beforeSampling` hook but it didn't work for some reason, so I
stuck with the drop attribute.
----
The final issue was excluding the tunnel requests from the
middleware/proxy, but there are many blockers for a solution:
1. The `config` must be statically analyzable, so we cannot expose
`withSentryMiddlewareConfig` wrapper of any kind.
2. Warning the user doesn't help much because they can't do anything
about it since the tunnel route is random.
3. Tested out writing a loader for turbopack/webpack to inject the
tunnel into the matcher as an array but user existing matcher can match
still.
4. Only way is to inject an exclusion match into the user existing
matcher, if it is an array then we need to inject it into each single
entry.
I may explore this further later with a loader for both
webpack/turbopack, and figure out a reliable way to inject the negative
matchers into the user expressions.1 parent 6240191 commit 4b92c64
File tree
29 files changed
+576
-7
lines changed- dev-packages/e2e-tests/test-applications/nextjs-16-tunnel
- app
- public
- tests
- packages/nextjs/src
- common/utils
- config
- turbopack
- edge
- server
29 files changed
+576
-7
lines changedLines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
Binary file not shown.
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
0 commit comments