|
| 1 | +import { getActiveTransaction } from '@sentry/core'; |
| 2 | +import { WINDOW } from '@sentry/svelte'; |
| 3 | +import type { Span, Transaction, TransactionContext } from '@sentry/types'; |
| 4 | + |
| 5 | +import { navigating, page } from '$app/stores'; |
| 6 | + |
| 7 | +const DEFAULT_TAGS = { |
| 8 | + 'routing.instrumentation': '@sentry/sveltekit', |
| 9 | +}; |
| 10 | + |
| 11 | +/** |
| 12 | + * Automatically creates pageload and navigation transactions for the client-side SvelteKit router. |
| 13 | + * |
| 14 | + * This instrumentation makes use of SvelteKit's `page` and `navigating` stores which can be accessed |
| 15 | + * anywhere on the client side. |
| 16 | + * |
| 17 | + * @param startTransactionFn the function used to start (idle) transactions |
| 18 | + * @param startTransactionOnPageLoad controls if pageload transactions should be created (defaults to `true`) |
| 19 | + * @param startTransactionOnLocationChange controls if navigation transactions should be created (defauls to `true`) |
| 20 | + */ |
| 21 | +export function svelteKitRoutingInstrumentation<T extends Transaction>( |
| 22 | + startTransactionFn: (context: TransactionContext) => T | undefined, |
| 23 | + startTransactionOnPageLoad: boolean = true, |
| 24 | + startTransactionOnLocationChange: boolean = true, |
| 25 | +): void { |
| 26 | + if (startTransactionOnPageLoad) { |
| 27 | + instrumentPageload(startTransactionFn); |
| 28 | + } |
| 29 | + |
| 30 | + if (startTransactionOnLocationChange) { |
| 31 | + instrumentNavigations(startTransactionFn); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function instrumentPageload(startTransactionFn: (context: TransactionContext) => Transaction | undefined): void { |
| 36 | + const initialPath = WINDOW && WINDOW.location && WINDOW.location.pathname; |
| 37 | + |
| 38 | + const pageloadTransaction = startTransactionFn({ |
| 39 | + name: initialPath, |
| 40 | + op: 'pageload', |
| 41 | + description: initialPath, |
| 42 | + tags: { |
| 43 | + ...DEFAULT_TAGS, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + page.subscribe(page => { |
| 48 | + if (!page) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const routeId = page.route && page.route.id; |
| 53 | + |
| 54 | + if (pageloadTransaction && routeId) { |
| 55 | + pageloadTransaction.setName(routeId, 'route'); |
| 56 | + } |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Use the `navigating` store to start a transaction on navigations. |
| 62 | + */ |
| 63 | +function instrumentNavigations(startTransactionFn: (context: TransactionContext) => Transaction | undefined): void { |
| 64 | + let routingSpan: Span | undefined = undefined; |
| 65 | + let activeTransaction: Transaction | undefined; |
| 66 | + |
| 67 | + navigating.subscribe(navigation => { |
| 68 | + if (!navigation) { |
| 69 | + // `navigating` emits a 'null' value when the navigation is completed. |
| 70 | + // So in this case, we can finish the routing span. If the transaction was an IdleTransaction, |
| 71 | + // it will finish automatically and if it was user-created users also need to finish it. |
| 72 | + if (routingSpan) { |
| 73 | + routingSpan.finish(); |
| 74 | + routingSpan = undefined; |
| 75 | + } |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const routeDestination = navigation.to && navigation.to.route.id; |
| 80 | + const routeOrigin = navigation.from && navigation.from.route.id; |
| 81 | + |
| 82 | + if (routeOrigin === routeDestination) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + activeTransaction = getActiveTransaction(); |
| 87 | + |
| 88 | + if (!activeTransaction) { |
| 89 | + activeTransaction = startTransactionFn({ |
| 90 | + name: routeDestination || (WINDOW && WINDOW.location && WINDOW.location.pathname), |
| 91 | + op: 'navigation', |
| 92 | + metadata: { source: 'route' }, |
| 93 | + tags: { |
| 94 | + ...DEFAULT_TAGS, |
| 95 | + }, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + if (activeTransaction) { |
| 100 | + if (routingSpan) { |
| 101 | + // If a routing span is still open from a previous navigation, we finish it. |
| 102 | + routingSpan.finish(); |
| 103 | + } |
| 104 | + routingSpan = activeTransaction.startChild({ |
| 105 | + op: 'ui.sveltekit.routing', |
| 106 | + description: 'SvelteKit Route Change', |
| 107 | + }); |
| 108 | + activeTransaction.setTag('from', routeOrigin); |
| 109 | + } |
| 110 | + }); |
| 111 | +} |
0 commit comments