|
1 |
| -import { BrowserTracing as OriginalBrowserTracing } from '@sentry/svelte'; |
| 1 | +import { navigating, page } from '$app/stores'; |
| 2 | +import { |
| 3 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 5 | + getActiveSpan, |
| 6 | + startInactiveSpan, |
| 7 | +} from '@sentry/core'; |
| 8 | +import { |
| 9 | + BrowserTracing as OriginalBrowserTracing, |
| 10 | + WINDOW, |
| 11 | + browserTracingIntegration as originalBrowserTracingIntegration, |
| 12 | + startBrowserTracingNavigationSpan, |
| 13 | + startBrowserTracingPageLoadSpan, |
| 14 | +} from '@sentry/svelte'; |
| 15 | +import type { Client, Integration, Span } from '@sentry/types'; |
2 | 16 | import { svelteKitRoutingInstrumentation } from './router';
|
3 | 17 |
|
4 | 18 | /**
|
5 | 19 | * A custom BrowserTracing integration for Sveltekit.
|
| 20 | + * |
| 21 | + * @deprecated use `browserTracingIntegration()` instead. |
6 | 22 | */
|
7 | 23 | export class BrowserTracing extends OriginalBrowserTracing {
|
8 | 24 | public constructor(options?: ConstructorParameters<typeof OriginalBrowserTracing>[0]) {
|
9 | 25 | super({
|
| 26 | + // eslint-disable-next-line deprecation/deprecation |
10 | 27 | routingInstrumentation: svelteKitRoutingInstrumentation,
|
11 | 28 | ...options,
|
12 | 29 | });
|
13 | 30 | }
|
14 | 31 | }
|
| 32 | + |
| 33 | +/** |
| 34 | + * A custom `BrowserTracing` integration for SvelteKit. |
| 35 | + * |
| 36 | + * @param options |
| 37 | + */ |
| 38 | +export function browserTracingIntegration( |
| 39 | + options: Parameters<typeof originalBrowserTracingIntegration>[0] = {}, |
| 40 | +): Integration { |
| 41 | + const integration = { |
| 42 | + ...originalBrowserTracingIntegration({ |
| 43 | + ...options, |
| 44 | + instrumentNavigation: false, |
| 45 | + instrumentPageLoad: false, |
| 46 | + }), |
| 47 | + }; |
| 48 | + |
| 49 | + return { |
| 50 | + ...integration, |
| 51 | + afterAllSetup: client => { |
| 52 | + integration.afterAllSetup(client); |
| 53 | + |
| 54 | + if (options.instrumentPageLoad !== false) { |
| 55 | + _instrumentPageload(client); |
| 56 | + } |
| 57 | + |
| 58 | + if (options.instrumentNavigation !== false) { |
| 59 | + _instrumentNavigations(client); |
| 60 | + } |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function _instrumentPageload(client: Client): void { |
| 66 | + const initialPath = WINDOW && WINDOW.location && WINDOW.location.pathname; |
| 67 | + |
| 68 | + startBrowserTracingPageLoadSpan(client, { |
| 69 | + name: initialPath, |
| 70 | + op: 'pageload', |
| 71 | + origin: 'auto.pageload.sveltekit', |
| 72 | + description: initialPath, |
| 73 | + tags: { |
| 74 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 75 | + }, |
| 76 | + attributes: { |
| 77 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const pageloadSpan = getActiveSpan(); |
| 82 | + |
| 83 | + page.subscribe(page => { |
| 84 | + if (!page) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const routeId = page.route && page.route.id; |
| 89 | + |
| 90 | + if (pageloadSpan && routeId) { |
| 91 | + pageloadSpan.updateName(routeId); |
| 92 | + pageloadSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route'); |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Use the `navigating` store to start a transaction on navigations. |
| 99 | + */ |
| 100 | +function _instrumentNavigations(client: Client): void { |
| 101 | + let routingSpan: Span | undefined = undefined; |
| 102 | + let activeSpan: Span | undefined; |
| 103 | + |
| 104 | + navigating.subscribe(navigation => { |
| 105 | + if (!navigation) { |
| 106 | + // `navigating` emits a 'null' value when the navigation is completed. |
| 107 | + // So in this case, we can finish the routing span. If the transaction was an IdleTransaction, |
| 108 | + // it will finish automatically and if it was user-created users also need to finish it. |
| 109 | + if (routingSpan) { |
| 110 | + routingSpan.end(); |
| 111 | + routingSpan = undefined; |
| 112 | + } |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const from = navigation.from; |
| 117 | + const to = navigation.to; |
| 118 | + |
| 119 | + // for the origin we can fall back to window.location.pathname because in this emission, it still is set to the origin path |
| 120 | + const rawRouteOrigin = (from && from.url.pathname) || (WINDOW && WINDOW.location && WINDOW.location.pathname); |
| 121 | + |
| 122 | + const rawRouteDestination = to && to.url.pathname; |
| 123 | + |
| 124 | + // We don't want to create transactions for navigations of same origin and destination. |
| 125 | + // We need to look at the raw URL here because parameterized routes can still differ in their raw parameters. |
| 126 | + if (rawRouteOrigin === rawRouteDestination) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const parameterizedRouteOrigin = from && from.route.id; |
| 131 | + const parameterizedRouteDestination = to && to.route.id; |
| 132 | + |
| 133 | + activeSpan = getActiveSpan(); |
| 134 | + |
| 135 | + if (!activeSpan) { |
| 136 | + startBrowserTracingNavigationSpan(client, { |
| 137 | + name: parameterizedRouteDestination || rawRouteDestination || 'unknown', |
| 138 | + op: 'navigation', |
| 139 | + origin: 'auto.navigation.sveltekit', |
| 140 | + attributes: { |
| 141 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: parameterizedRouteDestination ? 'route' : 'url', |
| 142 | + }, |
| 143 | + tags: { |
| 144 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 145 | + }, |
| 146 | + }); |
| 147 | + activeSpan = getActiveSpan(); |
| 148 | + } |
| 149 | + |
| 150 | + if (activeSpan) { |
| 151 | + if (routingSpan) { |
| 152 | + // If a routing span is still open from a previous navigation, we finish it. |
| 153 | + routingSpan.end(); |
| 154 | + } |
| 155 | + routingSpan = startInactiveSpan({ |
| 156 | + op: 'ui.sveltekit.routing', |
| 157 | + name: 'SvelteKit Route Change', |
| 158 | + attributes: { |
| 159 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.sveltekit', |
| 160 | + }, |
| 161 | + }); |
| 162 | + activeSpan.setAttribute('sentry.sveltekit.navigation.from', parameterizedRouteOrigin || undefined); |
| 163 | + } |
| 164 | + }); |
| 165 | +} |
0 commit comments