Repository navigation
feat(sveltekit): Serve the managed tunnel from sentryHandle under the app's base path
#25415
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
+356
−15
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1fc91a1
feat(sveltekit): Serve the managed tunnel from `sentryHandle` under t…
logaretm c97becb
fix(sveltekit): Keep source maps of the hooks files under a base path
logaretm 00cef96
fix(sveltekit): Serve a relative `tunnel` path under the base path too
logaretm 22a2be0
test(sveltekit): Check the managed tunnel under `vite dev` too
logaretm 594c46a
test(sveltekit): Check no span of a tunnel request is sent with Kit t…
logaretm a66dae5
ref(sveltekit): Import the tunnel handler from @sentry/core/server
logaretm 03782ad
test(sveltekit): Send Content-Length with the tunnel test requests
logaretm a4fb042
test(sveltekit): Race the tunnel span with a shared test-utils helper…
logaretm 51947f5
ref(sveltekit): Let core skip an empty base path, and keep only the t…
logaretm 5cbcc07
fix(sveltekit): Keep the base path module when tree-shaking treats mo…
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
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/src/app.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
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
23 changes: 23 additions & 0 deletions
23
dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/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,23 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, isServerSpanAt, waitForStreamedSpan, watchForTunnelSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test.skip(!process.env.E2E_TEST_TUNNEL, 'Only runs in the tunnel variant'); | ||
|
|
||
| const APP_NAME = 'sveltekit-3-cloudflare-workers'; | ||
| const TUNNEL_PATH = /^\/app\/[a-z0-9]{1,7}$/; | ||
|
|
||
| test('serves the managed tunnel from the Worker under the base path', async ({ page }) => { | ||
| const pageloadSpanPromise = waitForStreamedSpan(APP_NAME, span => getSpanOp(span) === 'pageload' && span.is_segment); | ||
| 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('/app/'); | ||
|
|
||
| expect((await tunnelResponsePromise).status()).toBe(200); | ||
| await expect(pageloadSpanPromise).resolves.toBeDefined(); | ||
| expect( | ||
| await raceTunnelSpan(isServerSpanAt('/app/server-load-error'), () => page.request.get('/app/server-load-error')), | ||
| ).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
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/sveltekit-3/src/app.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
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
38 changes: 38 additions & 0 deletions
38
dev-packages/e2e-tests/test-applications/sveltekit-3/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,38 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError, watchForTunnelSpan } from '@sentry-internal/test-utils'; | ||
| import type { SerializedStreamedSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test.skip(!process.env.E2E_TEST_TUNNEL, 'Only runs in the tunnel variant'); | ||
|
|
||
| const APP_NAME = 'sveltekit-3'; | ||
| const TUNNEL_PATH = /^\/app\/[a-z0-9]{1,7}$/; | ||
|
|
||
| // Kit's own `sveltekit.handle.root` span starts with only `http.url`, no `url.path`. | ||
| function getSpanPath(span: SerializedStreamedSpan): string { | ||
| const url = String(span.attributes['http.url']?.value ?? span.attributes['url.path']?.value ?? ''); | ||
|
|
||
| return url.startsWith('http') ? new URL(url).pathname : url; | ||
| } | ||
|
|
||
| test('sends client events through the managed tunnel under the base path, without spans of it', async ({ page }) => { | ||
| const errorEventPromise = waitForError(APP_NAME, event => event?.exception?.values?.[0]?.value === 'Click Error'); | ||
| const raceTunnelSpan = watchForTunnelSpan(APP_NAME, span => TUNNEL_PATH.test(getSpanPath(span))); | ||
|
|
||
| // The pageload envelope is the first tunnel request, and it's only sent once the app hydrated. | ||
| const tunnelResponsePromise = page.waitForResponse( | ||
| response => response.request().method() === 'POST' && TUNNEL_PATH.test(new URL(response.url()).pathname), | ||
| ); | ||
| await page.goto('/app/client-error'); | ||
|
|
||
| expect((await tunnelResponsePromise).status()).toBe(200); | ||
|
|
||
| await page.getByText('Throw error').click(); | ||
| await expect(errorEventPromise).resolves.toBeDefined(); | ||
|
|
||
| expect( | ||
| await raceTunnelSpan( | ||
| span => getSpanPath(span).endsWith('/app/users/123'), | ||
| () => page.request.get('/app/users/123'), | ||
| ), | ||
| ).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
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,37 @@ | ||
| import type { SerializedStreamedSpan } from '@sentry/core'; | ||
| import { getSpanOp, waitForStreamedSpan } from './event-proxy-server'; | ||
|
|
||
| type SpanMatcher = (span: SerializedStreamedSpan) => boolean; | ||
|
|
||
| /** | ||
| * Matches an `http.server` span whose `url.path` equals or matches `path`. | ||
| */ | ||
| export function isServerSpanAt(path: string | RegExp): SpanMatcher { | ||
| return span => { | ||
| const urlPath = String(span.attributes['url.path']?.value ?? ''); | ||
|
|
||
| return getSpanOp(span) === 'http.server' && (typeof path === 'string' ? urlPath === path : path.test(urlPath)); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Starts listening for a span of a tunnel request. Call it before the tunnel traffic, then call the returned function | ||
| * with a regular request made after it. Spans flush in order, so it resolves to `'tunnel'` if the tunnel request | ||
| * created a span, and to `'anchor'` otherwise, without waiting on a timeout. | ||
| */ | ||
| export function watchForTunnelSpan( | ||
| proxyServerName: string, | ||
| isTunnelSpan: SpanMatcher, | ||
| ): (isAnchorSpan: SpanMatcher, requestAnchor: () => Promise<unknown>) => Promise<'tunnel' | 'anchor'> { | ||
| const tunnelSpanPromise = waitForStreamedSpan(proxyServerName, isTunnelSpan); | ||
|
|
||
| return async (isAnchorSpan, requestAnchor) => { | ||
| const anchorSpanPromise = waitForStreamedSpan(proxyServerName, isAnchorSpan); | ||
| await requestAnchor(); | ||
|
|
||
| return Promise.race([ | ||
| tunnelSpanPromise.then(() => 'tunnel' as const), | ||
| anchorSpanPromise.then(() => 'anchor' as const), | ||
| ]); | ||
| }; | ||
| } | ||
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,14 @@ | ||
| import type { Options } from '@sentry/core'; | ||
| import { applyTunnelDefaults, GLOBAL_OBJ } from '@sentry/core'; | ||
|
|
||
| /** Set by `sentrySvelteKit()` to the app's `paths.base`. */ | ||
| export const TUNNEL_BASE_PATH_GLOBAL = '__sentry_sveltekit_base_path'; | ||
|
|
||
| /** | ||
| * Serves the managed tunnel under the app's base path, which SvelteKit requires for every request it handles. | ||
| * A `basePath` the user set wins. | ||
| */ | ||
| export function applyTunnelBasePath(options: Pick<Options, 'tunnel'>): void { | ||
| const basePath = (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL]; | ||
| options.tunnel = applyTunnelDefaults(options.tunnel, { basePath }); | ||
| } |
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,50 @@ | ||
| import { escapeStringForRegex } from '@sentry/core'; | ||
| import MagicString from 'magic-string'; | ||
| import type { Plugin } from 'vite'; | ||
| import { TUNNEL_BASE_PATH_GLOBAL } from '../common/tunnel'; | ||
| import type { ResolvedKitConfig } from './kitConfig'; | ||
| import { getHooksFileName } from './svelteConfig'; | ||
|
|
||
| const VIRTUAL_BASE_PATH_FILE = '\0sentry-sveltekit-tunnel-base-path'; | ||
|
|
||
| /** | ||
| * Hands the app's `paths.base` to the client and server SDKs so `tunnel: true` works under a base path without | ||
| * the user repeating it. It's imported first in the hooks files, so it's set before any `init` they import. | ||
| */ | ||
| export function makeTunnelBasePathPlugin(getKitConfig: () => Promise<ResolvedKitConfig>): Plugin { | ||
| let basePath = ''; | ||
| let entryFileRegexp: RegExp | undefined; | ||
|
|
||
| return { | ||
| name: 'sentry-sveltekit-tunnel-base-path', | ||
|
|
||
| configResolved: async () => { | ||
| const kitConfig = await getKitConfig(); | ||
| basePath = kitConfig.paths?.base || ''; | ||
|
|
||
| const hooksFiles = [getHooksFileName(kitConfig, 'server'), getHooksFileName(kitConfig, 'client')] | ||
| .map(escapeStringForRegex) | ||
| .join('|'); | ||
| // oxlint-disable-next-line sdk/no-regexp-constructor -- not end user input + escaped anyway | ||
| entryFileRegexp = new RegExp(`/(?:${hooksFiles}|src/instrumentation\\.server)\\.(?:js|ts|mjs|mts)$`); | ||
| }, | ||
|
|
||
| resolveId: id => (id === VIRTUAL_BASE_PATH_FILE ? { id, moduleSideEffects: true } : null), | ||
|
|
||
| load: id => | ||
| id === VIRTUAL_BASE_PATH_FILE | ||
| ? `globalThis[${JSON.stringify(TUNNEL_BASE_PATH_GLOBAL)}] = ${JSON.stringify(basePath)};` | ||
| : null, | ||
|
|
||
| transform: (code, id) => { | ||
| if (!basePath || !entryFileRegexp?.test(id)) { | ||
| return null; | ||
| } | ||
|
|
||
| const ms = new MagicString(code); | ||
| ms.prepend(`import ${JSON.stringify(VIRTUAL_BASE_PATH_FILE)};\n`); | ||
|
|
||
| return { code: ms.toString(), map: ms.generateMap({ hires: true }) }; | ||
| }, | ||
| }; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded span ops in tests
Low Severity
New tests assert
'url.path','http.server', and'pageload'as string literals. The review rules require@sentry/conventionsconstants (URL_PATH,HTTP_SERVER, and the pageload op) in test assertions as well as SDK source, so these checks drift if the constants change. Flagged because it was mentioned in the review rules file.Additional Locations (2)
dev-packages/e2e-tests/test-applications/sveltekit-3/tests/tunnel.test.ts#L10-L12dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/tests/tunnel.test.ts#L9-L10Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 5cbcc07. Configure here.