Skip to content

Commit 0846a80

Browse files
committed
feat(sveltekit): Serve the managed tunnel from sentryHandle under the app's base path
`sentryHandle` answers tunnel requests before isolation, tracing and routing. `sentrySvelteKit()` hands `paths.base` to the client and server SDKs, so `tunnel: true` works under a base path without repeating it.
1 parent 034ec1f commit 0846a80

23 files changed

Lines changed: 352 additions & 14 deletions

File tree

‎dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/package.json‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"clean": "npx rimraf node_modules pnpm-lock.yaml",
1212
"test:e2e": "playwright test",
1313
"test:build": "pnpm install && pnpm build",
14-
"test:assert": "pnpm test:e2e"
14+
"test:assert": "pnpm test:e2e",
15+
"test:build:tunnel": "pnpm install && E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 pnpm build",
16+
"test:assert:tunnel": "E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 playwright test tunnel"
1517
},
1618
"dependencies": {
1719
"@sentry/sveltekit": "file:../../packed/sentry-sveltekit-packed.tgz"
@@ -30,5 +32,14 @@
3032
},
3133
"volta": {
3234
"extends": "../../package.json"
35+
},
36+
"sentryTest": {
37+
"variants": [
38+
{
39+
"label": "sveltekit-3-cloudflare-workers (tunnel)",
40+
"build-command": "pnpm test:build:tunnel",
41+
"assert-command": "pnpm test:assert:tunnel"
42+
}
43+
]
3344
}
3445
}

‎dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/src/app.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// See https://svelte.dev/docs/kit/types#app.d.ts
22
// `App.Platform` (including `platform.ctx`) is declared by `@sveltejs/adapter-cloudflare`.
33
declare global {
4+
const __E2E_TEST_TUNNEL__: boolean;
5+
46
namespace App {
57
// interface Error {}
68
// interface Locals {}

‎dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/src/hooks.client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Sentry from '@sentry/sveltekit';
44
Sentry.init({
55
environment: 'qa',
66
dsn: PUBLIC_E2E_TEST_DSN,
7-
tunnel: 'http://localhost:3031/', // proxy server
7+
tunnel: __E2E_TEST_TUNNEL__ ? true : 'http://localhost:3031/', // proxy server
88
tracesSampleRate: 1.0,
99
});
1010

‎dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/src/hooks.server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const handle = sequence(
1010
environment: 'qa', // dynamic sampling bias to keep spans
1111
dsn: E2E_TEST_DSN,
1212
debug: !!process.env.DEBUG,
13-
tunnel: 'http://localhost:3031/', // proxy server
13+
tunnel: __E2E_TEST_TUNNEL__ ? true : 'http://localhost:3031/', // proxy server
1414
tracesSampleRate: 1.0,
1515
}),
1616
sentryHandle(),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect, test } from '@playwright/test';
2+
import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';
3+
4+
test.skip(!process.env.E2E_TEST_TUNNEL, 'Only runs in the tunnel variant');
5+
6+
const TUNNEL_PATH = /^\/app\/[a-z0-9]{1,7}$/;
7+
8+
test('serves the managed tunnel from the Worker under the base path', async ({ page }) => {
9+
const pageloadSpanPromise = waitForStreamedSpan(
10+
'sveltekit-3-cloudflare-workers',
11+
span => getSpanOp(span) === 'pageload' && span.is_segment,
12+
);
13+
const tunnelServerSpanPromise = waitForStreamedSpan(
14+
'sveltekit-3-cloudflare-workers',
15+
span => getSpanOp(span) === 'http.server' && TUNNEL_PATH.test(String(span.attributes['url.path']?.value ?? '')),
16+
);
17+
const tunnelResponsePromise = page.waitForResponse(
18+
response => response.request().method() === 'POST' && TUNNEL_PATH.test(new URL(response.url()).pathname),
19+
);
20+
21+
await page.goto('/app/');
22+
const tunnelResponse = await tunnelResponsePromise;
23+
24+
expect(tunnelResponse.status()).toBe(200);
25+
expect(new URL(tunnelResponse.url()).origin).toBe(new URL(page.url()).origin);
26+
await expect(pageloadSpanPromise).resolves.toBeDefined();
27+
28+
// Anchor on a regular server span requested after the tunnel traffic: spans flush in order, so a tunnel span
29+
// would arrive first.
30+
const anchorSpanPromise = waitForStreamedSpan(
31+
'sveltekit-3-cloudflare-workers',
32+
span =>
33+
getSpanOp(span) === 'http.server' &&
34+
String(span.attributes['url.path']?.value ?? '').startsWith('/app/server-load-error'),
35+
);
36+
await page.request.get('/app/server-load-error');
37+
38+
const winner = await Promise.race([
39+
tunnelServerSpanPromise.then(() => 'tunnel' as const),
40+
anchorSpanPromise.then(() => 'anchor' as const),
41+
]);
42+
expect(winner).toBe('anchor');
43+
});

‎dev-packages/e2e-tests/test-applications/sveltekit-3-cloudflare-workers/vite.config.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { sveltekit } from '@sveltejs/kit/vite';
44
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
55
import { defineConfig } from 'vite';
66

7+
// The tunnel variant serves the app under a base path to cover that the managed tunnel follows it.
8+
const tunnelVariant = !!process.env.E2E_TEST_TUNNEL;
9+
710
export default defineConfig({
11+
define: {
12+
__E2E_TEST_TUNNEL__: JSON.stringify(tunnelVariant),
13+
},
814
plugins: [
915
sentrySvelteKit({
1016
autoUploadSourceMaps: false,
@@ -17,6 +23,7 @@ export default defineConfig({
1723
prerender: {
1824
handleHttpError: 'ignore',
1925
},
26+
paths: tunnelVariant ? { base: '/app' } : undefined,
2027
}),
2128
],
2229
});

‎dev-packages/e2e-tests/test-applications/sveltekit-3/package.json‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
1313
"test:prod": "TEST_ENV=production playwright test",
1414
"test:build": "pnpm install && pnpm build",
15-
"test:assert": "pnpm test:prod"
15+
"test:assert": "pnpm test:prod",
16+
"test:build:tunnel": "pnpm install && E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 pnpm build",
17+
"test:assert:tunnel": "E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 TEST_ENV=production playwright test tunnel"
1618
},
1719
"dependencies": {
1820
"@sentry/sveltekit": "file:../../packed/sentry-sveltekit-packed.tgz"
@@ -31,5 +33,14 @@
3133
"volta": {
3234
"extends": "../../package.json"
3335
},
34-
"type": "module"
36+
"type": "module",
37+
"sentryTest": {
38+
"variants": [
39+
{
40+
"label": "sveltekit-3 (tunnel)",
41+
"build-command": "pnpm test:build:tunnel",
42+
"assert-command": "pnpm test:assert:tunnel"
43+
}
44+
]
45+
}
3546
}

‎dev-packages/e2e-tests/test-applications/sveltekit-3/src/app.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// See https://kit.svelte.dev/docs/types#app
22
// for information about these interfaces
33
declare global {
4+
const __E2E_TEST_TUNNEL__: boolean;
5+
46
namespace App {
57
// interface Error {}
68
// interface Locals {}

‎dev-packages/e2e-tests/test-applications/sveltekit-3/src/hooks.client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Sentry from '@sentry/sveltekit';
44
Sentry.init({
55
environment: 'qa', // dynamic sampling bias to keep transactions
66
dsn: PUBLIC_E2E_TEST_DSN,
7-
tunnel: `http://localhost:3031/`, // proxy server
7+
tunnel: __E2E_TEST_TUNNEL__ ? true : `http://localhost:3031/`, // proxy server
88
tracesSampleRate: 1.0,
99
});
1010

‎dev-packages/e2e-tests/test-applications/sveltekit-3/src/instrumentation.server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Sentry.init({
77
environment: 'qa', // dynamic sampling bias to keep transactions
88
dsn: E2E_TEST_DSN,
99
debug: !!process.env.DEBUG,
10-
tunnel: `http://localhost:3031/`, // proxy server
10+
tunnel: __E2E_TEST_TUNNEL__ ? true : `http://localhost:3031/`, // proxy server
1111
tracesSampleRate: 1.0,
1212
});

0 commit comments

Comments
 (0)