Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/sveltekit/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Integration } from '@sentry/core';
import { setRouteProvider } from '@sentry/core';
import { browserTracingIntegration as originalBrowserTracingIntegration } from '@sentry/svelte';
import { routeProvider } from './routeCache';
// The `sentrySvelteKit()` Vite plugin redirects this to the Svelte 4 or Svelte 5 variant per Kit
// version; without the plugin it resolves via `exports` to the Svelte 4 variant, so builds don't break.
import { instrumentSvelteKitTracing } from '@sentry/sveltekit/browser-tracing-variant';
Expand All @@ -20,6 +22,10 @@ export function browserTracingIntegration(

return {
...integration,
setup: client => {
setRouteProvider(routeProvider, client);
integration.setup?.(client);
},
afterAllSetup: client => {
integration.afterAllSetup(client);
instrumentSvelteKitTracing(client, options);
Expand Down
15 changes: 15 additions & 0 deletions packages/sveltekit/src/client/routeCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createCachedRouteProvider } from '@sentry/core';

// SvelteKit has no public route matcher, and `page.route.id` is not available synchronously, so the
// provider answers from route ids the instrumentation has already seen rather than by matching.
export const routeProvider = createCachedRouteProvider();

/**
* Records the parameterized route id SvelteKit reported for a path.
*
* Called from both the Kit 2 and Kit 3 instrumentation, since `page.route.id` is the only place the
* route id is available.
*/
export function recordRouteId(pathname: string | undefined, routeId: string | null | undefined): void {
routeProvider.record(pathname, routeId);
}
5 changes: 5 additions & 0 deletions packages/sveltekit/src/client/svelte4BrowserTracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Navigation, Page } from '@sveltejs/kit';
// eslint-disable-next-line typescript/no-deprecated
import { navigating, page } from '$app/stores';
import type { Readable } from 'svelte/store';
import { recordRouteId } from './routeCache';

/**
* SvelteKit 2 / Svelte 4 browser tracing (`$app/stores`). Selected at build time, so it's only
Expand Down Expand Up @@ -62,6 +63,8 @@ function _instrumentPageload(client: Client, pageStore: Readable<Page>): void {

const routeId = pageState.route?.id;

recordRouteId(pageState.url?.pathname, routeId);

if (routeId) {
pageloadSpan.updateName(routeId);
pageloadSpan.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', [URL_TEMPLATE]: routeId });
Expand Down Expand Up @@ -105,6 +108,8 @@ function _instrumentNavigations(client: Client, navigatingStore: Readable<Naviga
const parameterizedRouteOrigin = from?.route.id;
const parameterizedRouteDestination = to?.route.id;

recordRouteId(to?.url.pathname, parameterizedRouteDestination);

if (routingSpan) {
// If a routing span is still open from a previous navigation, we finish it.
routingSpan.end();
Expand Down
5 changes: 5 additions & 0 deletions packages/sveltekit/src/client/svelte5BrowserTracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { SENTRY_OP, URL_TEMPLATE } from '@sentry/conventions/attributes';
import type { Navigation } from '@sveltejs/kit';
import { getCurrentNavigation, onNavigationChange, onPageRouteChange } from './navigationState.svelte';
import { recordRouteId } from './routeCache';

/**
* SvelteKit 3 / Svelte 5 browser tracing (`$app/state` runes). Selected at build time, so it's only
Expand Down Expand Up @@ -55,6 +56,8 @@ function _instrumentPageLoad(client: Client): void {
// `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 => {
recordRouteId(WINDOW.location?.pathname, routeId);

if (routeId) {
pageLoadSpan.updateName(routeId);
pageLoadSpan.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', [URL_TEMPLATE]: routeId });
Expand Down Expand Up @@ -88,6 +91,8 @@ function _instrumentNavigations(client: Client): void {
const parameterizedRouteOrigin = from?.route.id;
const parameterizedRouteDestination = to?.route.id;

recordRouteId(to?.url.pathname, parameterizedRouteDestination);

routingSpan?.end();

const navigationInfo = {
Expand Down
Loading