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
Merged
+447
−30
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
40f95ac
feat(sveltekit): Serve the managed tunnel from `sentryHandle` under t…
logaretm f25fb1c
fix(sveltekit): Keep source maps of the hooks files under a base path
logaretm 973b78d
fix(sveltekit): Serve a relative `tunnel` path under the base path too
logaretm b520a89
test(sveltekit): Check the managed tunnel under `vite dev` too
logaretm 66e0467
test(sveltekit): Check no span of a tunnel request is sent with Kit t…
logaretm 28f46dc
ref(sveltekit): Import the tunnel handler from @sentry/core/server
logaretm da4e314
test(sveltekit): Send Content-Length with the tunnel test requests
logaretm c027b46
test(sveltekit): Race the tunnel span with a shared test-utils helper…
logaretm 1e973de
ref(sveltekit): Let core skip an empty base path, and keep only the t…
logaretm c511ae9
fix(sveltekit): Keep the base path module when tree-shaking treats mo…
logaretm 4827b43
ref: Import the internal tunnel helpers under their `_INTERNAL_` names
logaretm c7a9e01
test(sveltekit): Read the tunnel variant flag from declared env vars
logaretm 6435834
ref(sveltekit): Serve the managed tunnel under the base path set when…
logaretm 51ea50e
test(sveltekit): Read the tunnel variant flag from declared env vars …
logaretm 4e229d2
fix(sveltekit): Match every hooks file form SvelteKit accepts, with o…
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
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
4 changes: 2 additions & 2 deletions
4
dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/src/hooks.client.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
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
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
4 changes: 2 additions & 2 deletions
4
dev-packages/e2e-tests/test-applications/sveltekit-3/src/hooks.client.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
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,20 @@ | ||
| import type { Options } from '@sentry/core'; | ||
| import { _INTERNAL_applyTunnelDefaults as 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'; | ||
|
|
||
| /** | ||
| * Returns the app's `paths.base`, once a module `sentrySvelteKit()` prepends it to has loaded. | ||
| */ | ||
| export function getTunnelBasePath(): string | undefined { | ||
| return (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL]; | ||
| } | ||
|
|
||
| /** | ||
| * Sends to 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 { | ||
| options.tunnel = applyTunnelDefaults(options.tunnel, { basePath: getTunnelBasePath() }); | ||
| } |
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.