-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(sveltekit): Support browser tracing with SvelteKit 3 #22264
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
Draft
Lms24
wants to merge
5
commits into
develop
Choose a base branch
from
feat/sveltekit-3-compat-investigation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+436
−57
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc9e2a0
wip
chargome a2b8566
feat(sveltekit): Detect native tracing from promoted `kit.tracing` co…
chargome 7e22244
.
chargome 3599250
fix(sveltekit): Support browser tracing with SvelteKit 3
Lms24 8b95aff
rebase on existing test
chargome 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
2 changes: 0 additions & 2 deletions
2
dev-packages/e2e-tests/test-applications/sveltekit-3/src/routes/+layout.svelte
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/routes/users/[id]/+page.svelte
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
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
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
142 changes: 142 additions & 0 deletions
142
packages/sveltekit/src/client/kit3BrowserTracingIntegration.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,142 @@ | ||
| import type { Client, Span } from '@sentry/core'; | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; | ||
| import { | ||
| getCurrentScope, | ||
| startBrowserTracingNavigationSpan, | ||
| startBrowserTracingPageLoadSpan, | ||
| startInactiveSpan, | ||
| WINDOW, | ||
| } from '@sentry/svelte'; | ||
| import { URL_TEMPLATE } from '@sentry/conventions/attributes'; | ||
| import type { Navigation } from '@sveltejs/kit'; | ||
| import { getCurrentNavigation, onNavigationChange, onPageRouteChange } from './navigationState.svelte'; | ||
|
|
||
| /** @internal */ | ||
| export function instrumentSvelteKit3Tracing( | ||
| client: Client, | ||
| options: { instrumentPageLoad?: boolean; instrumentNavigation?: boolean }, | ||
| ): void { | ||
| if (options.instrumentPageLoad !== false) { | ||
| _instrumentPageLoad(client); | ||
| } | ||
|
|
||
| if (options.instrumentNavigation !== false) { | ||
| _instrumentNavigations(client); | ||
| } | ||
| } | ||
|
|
||
| function _instrumentPageLoad(client: Client): void { | ||
| const initialPath = WINDOW.location?.pathname; | ||
|
|
||
| const pageLoadSpan = startBrowserTracingPageLoadSpan(client, { | ||
| name: initialPath, | ||
| op: 'pageload', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.sveltekit', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', | ||
| }, | ||
| }); | ||
|
|
||
| if (!pageLoadSpan) { | ||
| return; | ||
| } | ||
|
|
||
| // `page.route.id` isn't available synchronously when we set up (during `Sentry.init`), so we react | ||
| // to it and upgrade the pageload span from `url` to the parameterized `route` once it resolves. | ||
| onPageRouteChange(routeId => { | ||
| if (routeId) { | ||
| pageLoadSpan.updateName(routeId); | ||
| pageLoadSpan.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', [URL_TEMPLATE]: routeId }); | ||
| getCurrentScope().setTransactionName(routeId); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function _instrumentNavigations(client: Client): void { | ||
| let routingSpan: Span | undefined; | ||
| // Deduplicates the two triggers below (the `fetch` wrapper and the `$effect`) so a single | ||
| // navigation starts exactly one span, regardless of which fires first. | ||
| let activeNavigationId: string | undefined; | ||
|
|
||
| function _startNavigation(navigation: Navigation): void { | ||
| const from = navigation.from; | ||
| const to = navigation.to; | ||
| const rawRouteOrigin = from?.url.pathname || WINDOW.location?.pathname; | ||
| const rawRouteDestination = to?.url.pathname; | ||
|
|
||
| if (rawRouteOrigin === rawRouteDestination) { | ||
| return; | ||
| } | ||
|
|
||
| const navigationId = to?.url.href; | ||
| if (navigationId && navigationId === activeNavigationId) { | ||
| return; | ||
| } | ||
| activeNavigationId = navigationId; | ||
|
|
||
| const parameterizedRouteOrigin = from?.route.id; | ||
| const parameterizedRouteDestination = to?.route.id; | ||
|
|
||
| routingSpan?.end(); | ||
|
|
||
| const navigationInfo = { | ||
| 'sentry.sveltekit.navigation.type': navigation.type, | ||
| 'sentry.sveltekit.navigation.from': parameterizedRouteOrigin || undefined, | ||
| 'sentry.sveltekit.navigation.to': parameterizedRouteDestination || undefined, | ||
| }; | ||
|
|
||
| startBrowserTracingNavigationSpan( | ||
| client, | ||
| { | ||
| name: parameterizedRouteDestination || rawRouteDestination || 'unknown', | ||
| op: 'navigation', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.sveltekit', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: parameterizedRouteDestination ? 'route' : 'url', | ||
| ...(parameterizedRouteDestination && { [URL_TEMPLATE]: parameterizedRouteDestination }), | ||
| ...navigationInfo, | ||
| }, | ||
| }, | ||
| { url: to?.url.href }, | ||
| ); | ||
|
|
||
| routingSpan = startInactiveSpan({ | ||
| op: 'ui.sveltekit.routing', | ||
| name: 'SvelteKit Route Change', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.sveltekit', | ||
| ...navigationInfo, | ||
| }, | ||
| onlyIfParent: true, | ||
| }); | ||
| } | ||
|
|
||
| // SvelteKit fires its data request (e.g. a server load's `__data.json`) synchronously at | ||
| // navigation start. A `$effect` reacting to `navigating` only runs a microtask later — after that | ||
| // request has already left carrying the *previous* trace, breaking the distributed trace. By | ||
| // wrapping `fetch` (outermost, since this runs after the SDK's own fetch instrumentation) we read | ||
| // `navigating` synchronously and start the navigation span *before* the request, so it propagates | ||
| // the navigation trace and the server continues it. | ||
| const originalFetch = WINDOW.fetch?.bind(WINDOW); | ||
| if (originalFetch) { | ||
| WINDOW.fetch = (...args: Parameters<typeof fetch>): ReturnType<typeof fetch> => { | ||
| const navigation = getCurrentNavigation(); | ||
| if (navigation) { | ||
| _startNavigation(navigation); | ||
| } | ||
| return originalFetch(...args); | ||
| }; | ||
| } | ||
|
|
||
| onNavigationChange(navigation => { | ||
| if (!navigation) { | ||
| routingSpan?.end(); | ||
| routingSpan = undefined; | ||
| activeNavigationId = undefined; | ||
| return; | ||
| } | ||
|
|
||
| // Fallback for navigations that don't issue an outgoing request (the `fetch` wrapper never fires). | ||
| _startNavigation(navigation); | ||
| }); | ||
| } |
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.
I kinda don't like the dynamic import here because:
Maybe the way forward is to instruct users to switch to a new version of the integration that imports eagerly from
$app/state