Repository navigation
feat(react-router): Serve the managed tunnel from a route in the server build #25422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+342
−11
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bbbe510
feat(react-router): Serve the managed tunnel from a route in the serv…
logaretm 987f1f4
test(react-router): Define the tunnel flag in the Cloudflare build
logaretm a35fbd9
fix(react-router): Serve the managed tunnel under the app's `basename`
logaretm 747a7d8
test(react-router): Check the managed tunnel under `react-router dev`…
logaretm 18255ae
fix(react-router): Normalize the tunnel base path with the core util
logaretm 396cefa
fix(react-router): Serve every managed tunnel path, one route each
logaretm 37c8c7c
ref(react-router): Report the basename once with setManagedTunnelDefa…
logaretm 6bb9108
test(react-router): Cover the managed tunnel under a basename on Clou…
logaretm 283f59f
test(react-router): Send Content-Length with the tunnel test requests
logaretm e1051fd
test(react-router): Race the tunnel span with the shared helper, and …
logaretm f52abd7
ref(react-router): Let core normalize the basename
logaretm ac680dd
ref: Import the internal tunnel helpers under their `_INTERNAL_` names
logaretm d8594c2
test(react-router): Serve the managed tunnel with `serveTunnelIntegra…
logaretm 3c6d37a
fix(react-router): Only add the tunnel routes when the server build d…
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/react-router-8-framework/app/globals.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| declare const __E2E_TEST_TUNNEL__: boolean; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
dev-packages/e2e-tests/test-applications/react-router-8-framework/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| import * as Sentry from '@sentry/react-router'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://username@domain/123', | ||
| dsn: process.env.E2E_TEST_TUNNEL ? 'http://public@localhost:3031/1337' : 'https://username@domain/123', | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| tracesSampleRate: 1.0, | ||
| tunnel: `http://localhost:3031/`, // proxy server | ||
| // The tunnel variants also accept another app's DSN, so the server serves more than one tunnel path. | ||
| integrations: process.env.E2E_TEST_TUNNEL | ||
| ? [Sentry.serveTunnelIntegration({ allowedDsns: ['http://other@localhost:3031/1338'] })] | ||
| : [], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
dev-packages/e2e-tests/test-applications/react-router-8-framework/tests/tunnel.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { isServerSpanAt, watchForTunnelSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test.skip(!process.env.E2E_TEST_TUNNEL, 'Only runs in the tunnel variant'); | ||
|
|
||
| const APP_NAME = 'react-router-8-framework'; | ||
| const BASE_PATH = process.env.E2E_TEST_BASE_PATH ?? ''; | ||
| const TUNNEL_PATH = new RegExp(`^${BASE_PATH}/[a-z0-9]{1,7}$`); | ||
|
|
||
| test('answers the managed tunnel before the root middleware', async ({ page }) => { | ||
| // The variant's root middleware rejects every other POST. | ||
| expect((await page.request.post(`${BASE_PATH}/`, { data: 'x' })).status()).toBe(401); | ||
|
|
||
| const raceTunnelSpan = watchForTunnelSpan(APP_NAME, isServerSpanAt(TUNNEL_PATH)); | ||
| const tunnelResponsePromise = page.waitForResponse( | ||
| response => response.request().method() === 'POST' && TUNNEL_PATH.test(new URL(response.url()).pathname), | ||
| ); | ||
|
|
||
| await page.goto(`${BASE_PATH}/`); | ||
|
|
||
| expect((await tunnelResponsePromise).status()).toBe(200); | ||
| expect( | ||
| await raceTunnelSpan(isServerSpanAt(`${BASE_PATH}/performance`), () => | ||
| page.request.get(`${BASE_PATH}/performance`), | ||
| ), | ||
| ).toBe('anchor'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
...ackages/e2e-tests/test-applications/react-router-8-framework/workers/instrument.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| import { defineCloudflareOptions } from '@sentry/cloudflare'; | ||
| import { defineCloudflareOptions, serveTunnelIntegration } from '@sentry/cloudflare'; | ||
|
|
||
| declare const __E2E_TEST_TUNNEL__: boolean; | ||
|
|
||
| export default defineCloudflareOptions({ | ||
| dsn: 'https://username@domain/123', | ||
| dsn: __E2E_TEST_TUNNEL__ ? 'http://public@localhost:3031/1337' : 'https://username@domain/123', | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| tracesSampleRate: 1.0, | ||
| tunnel: 'http://localhost:3031/', // proxy server | ||
| integrations: __E2E_TEST_TUNNEL__ ? [serveTunnelIntegration()] : [], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { debug, getClient } from '@sentry/core'; | ||
| import { | ||
| _INTERNAL_getTunnelPaths as getTunnelPaths, | ||
| handleTunnelRequestIfMatched, | ||
| _INTERNAL_setManagedTunnelDefaults as setManagedTunnelDefaults, | ||
| } from '@sentry/core/server'; | ||
| import { DEBUG_BUILD } from '../common/debug-build'; | ||
|
|
||
| // Never matches a request, for when the managed tunnel is off. | ||
| const DISABLED_TUNNEL_PATH = '/__sentry-tunnel-disabled__'; | ||
|
|
||
| /** | ||
| * The resource routes that serve the managed tunnel, one per tunnel path. `sentryReactRouter()` adds them to the | ||
| * server build, since React Router has no request hook to answer from and a route matches only one path. Not meant | ||
| * to be used directly. | ||
| * | ||
| * @param basename The app's `basename`, which React Router matches the routes under but leaves in the request URL. | ||
| */ | ||
| export function createSentryTunnelRoutes(basename?: string): Record<string, Record<string, unknown>> { | ||
| // Runs when the server build loads. Clients created later, e.g. per request on Cloudflare, start with the basename. | ||
| setManagedTunnelDefaults({ basePath: basename }); | ||
|
|
||
| // `init` runs from `--import` before the server build loads, so the client and its tunnel paths are usually known. | ||
| if (!getClient()) { | ||
| return { 'sentry-tunnel': makeTunnelRoute('sentry-tunnel', getLazyTunnelPath) }; | ||
| } | ||
|
|
||
| return Object.fromEntries( | ||
|
logaretm marked this conversation as resolved.
|
||
| getTunnelPaths().map((path, index) => { | ||
| const id = `sentry-tunnel-${index}`; | ||
|
|
||
| return [id, makeTunnelRoute(id, () => path)]; | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The path of the single route added when `init` runs after the server build loads, e.g. per request on Cloudflare. | ||
| * React Router reads it on its first request and keeps it until the server restarts. | ||
| */ | ||
| function getLazyTunnelPath(): string { | ||
| if (!getClient()) { | ||
| DEBUG_BUILD && debug.warn('The managed tunnel stays off until restart: a request came in before `Sentry.init`.'); | ||
| return DISABLED_TUNNEL_PATH; | ||
| } | ||
|
|
||
| const [path, ...otherPaths] = getTunnelPaths(); | ||
| if (otherPaths.length) { | ||
| DEBUG_BUILD && debug.warn(`Sentry.init ran after the server build loaded, so only ${path} is served.`); | ||
| } | ||
|
|
||
| return path ?? DISABLED_TUNNEL_PATH; | ||
| } | ||
|
|
||
| function makeTunnelRoute(id: string, getPath: () => string): Record<string, unknown> { | ||
| return { | ||
| id, | ||
| parentId: undefined, | ||
| index: undefined, | ||
| caseSensitive: undefined, | ||
| get path(): string { | ||
| return getPath(); | ||
| }, | ||
| module: { | ||
| action: ({ request }: { request: Request }) => | ||
| handleTunnelRequestIfMatched(request) ?? new Response(null, { status: 404 }), | ||
| loader: () => new Response(null, { status: 405, headers: { Allow: 'POST' } }), | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { Plugin } from 'vite'; | ||
|
|
||
| const SERVER_BUILD_MODULE_ID = 'virtual:react-router/server-build'; | ||
|
|
||
| /** | ||
| * Adds the managed tunnel routes to the server build. They only serve requests when `serveTunnelIntegration` is set | ||
| * up. The server build exports the app's `basename`, which the route needs to match the request URL. | ||
| */ | ||
| export function makeTunnelRoutePlugin(): Plugin { | ||
| return { | ||
| name: 'sentry-react-router-tunnel-route', | ||
| enforce: 'post', | ||
|
|
||
| transform(code, id, options) { | ||
| if (!options?.ssr || !id.includes(SERVER_BUILD_MODULE_ID)) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| code: `${code} | ||
| import { createSentryTunnelRoutes as __sentryCreateTunnelRoutes } from '@sentry/react-router'; | ||
| if (typeof routes === 'object' && routes) { | ||
| Object.assign(routes, __sentryCreateTunnelRoutes(typeof basename === 'string' ? basename : undefined)); | ||
| } | ||
| `, | ||
| map: null, | ||
| }; | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.