-
Notifications
You must be signed in to change notification settings - Fork 0
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
minor: support @sentry/sveltekit routing instrumentation #98
Changes from 5 commits
a3618c6
890d0d7
3d88028
cec8eae
0df4485
261b748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { hasTracingEnabled } from '@sentry/core' | ||
import { BrowserOptions, BrowserTracing } from '@sentry/svelte' | ||
import { addOrUpdateIntegration } from '@sentry/utils' | ||
import { svelteKitRoutingInstrumentation } from './router.js' | ||
|
||
export const addClientIntegrations = (options: BrowserOptions) => { | ||
let integrations = options.integrations || [] | ||
|
||
if (hasTracingEnabled(options)) { | ||
const defaultBrowserTracingIntegration = new BrowserTracing({ | ||
routingInstrumentation: svelteKitRoutingInstrumentation | ||
}) | ||
|
||
integrations = addOrUpdateIntegration( | ||
defaultBrowserTracingIntegration, | ||
integrations, | ||
{ | ||
'options.routingInstrumentation': svelteKitRoutingInstrumentation | ||
} | ||
) | ||
} | ||
|
||
options.integrations = integrations | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SDK_VERSION } from '@sentry/core' | ||
import { BrowserOptions } from '@sentry/svelte' | ||
|
||
const PACKAGE_NAME_PREFIX = 'npm:@sentry/' | ||
|
||
export const applySdkMetadata = (options: BrowserOptions, names: string[]) => { | ||
options._metadata = options._metadata || {} | ||
options._metadata.sdk = options._metadata.sdk || { | ||
name: 'sentry.javascript.sveltekit', | ||
packages: names.map((name) => ({ | ||
name: `${PACKAGE_NAME_PREFIX}${name}`, | ||
version: SDK_VERSION | ||
})), | ||
version: SDK_VERSION | ||
} | ||
} | ||
Comment on lines
+1
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function - export const applySdkMetadata = (options: BrowserOptions, names: string[]) => {
- options._metadata = options._metadata || {}
- options._metadata.sdk = options._metadata.sdk || {
- name: 'sentry.javascript.sveltekit',
- packages: names.map((name) => ({
- name: `${PACKAGE_NAME_PREFIX}${name}`,
- version: SDK_VERSION
- })),
- version: SDK_VERSION
- }
- }
+ export const applySdkMetadata = (options: BrowserOptions, names: string[]): BrowserOptions => {
+ const newOptions = { ...options };
+ newOptions._metadata = newOptions._metadata || {}
+ newOptions._metadata.sdk = newOptions._metadata.sdk || {
+ name: 'sentry.javascript.sveltekit',
+ packages: names.map((name) => ({
+ name: `${PACKAGE_NAME_PREFIX}${name}`,
+ version: SDK_VERSION
+ })),
+ version: SDK_VERSION
+ }
+ return newOptions;
+ } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { WINDOW } from '@sentry/svelte' | ||
|
||
export const restoreFetch = (actualFetch: typeof fetch) => { | ||
const globalWithSentryFetchProxy = WINDOW | ||
|
||
// @ts-expect-error TODO: fix this | ||
globalWithSentryFetchProxy._sentryFetchProxy = | ||
globalWithSentryFetchProxy.fetch | ||
|
||
globalWithSentryFetchProxy.fetch = actualFetch | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { navigating, page } from '$app/stores' | ||
import { getActiveTransaction } from '@sentry/core' | ||
import { WINDOW } from '@sentry/svelte' | ||
import type { Transaction, TransactionContext } from '@sentry/types' | ||
import type { Span } from '@sentry/types' | ||
|
||
const DEFAULT_TAGS = { | ||
'routing.instrumentation': '@sentry/sveltekit' | ||
} | ||
|
||
export const svelteKitRoutingInstrumentation = <T extends Transaction>( | ||
startTransactionFn: (context: TransactionContext) => T | undefined, | ||
startTransactionOnPageLoad?: boolean, | ||
startTransactionOnLocationChange?: boolean | ||
) => { | ||
if (startTransactionOnPageLoad) { | ||
instrumentPageload(startTransactionFn) | ||
} | ||
|
||
if (startTransactionOnLocationChange) { | ||
instrumentNavigations(startTransactionFn) | ||
} | ||
} | ||
|
||
const instrumentPageload = <T extends Transaction>( | ||
startTransactionFn: (context: TransactionContext) => T | undefined | ||
) => { | ||
const initialPath = WINDOW && WINDOW.location && WINDOW.location.pathname | ||
|
||
const pageloadTransaction = startTransactionFn({ | ||
name: initialPath, | ||
op: 'pageload', | ||
origin: 'auto.pageload.sveltekit', | ||
description: initialPath, | ||
tags: { | ||
...DEFAULT_TAGS | ||
}, | ||
metadata: { | ||
source: 'url' | ||
} | ||
}) | ||
|
||
page.subscribe((page) => { | ||
if (!page) { | ||
return | ||
} | ||
|
||
const routeId = page.route && page.route.id | ||
|
||
if (pageloadTransaction && routeId) { | ||
pageloadTransaction.setName(routeId, 'route') | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Use the `navigating` store to start a transaction on navigations. | ||
*/ | ||
const instrumentNavigations = <T extends Transaction>( | ||
startTransactionFn: (context: TransactionContext) => T | undefined | ||
) => { | ||
let routingSpan: Span | undefined = undefined | ||
let activeTransaction | ||
|
||
navigating.subscribe((navigation) => { | ||
if (!navigation) { | ||
// `navigating` emits a 'null' value when the navigation is completed. | ||
// So in this case, we can finish the routing span. If the transaction was an IdleTransaction, | ||
// it will finish automatically and if it was user-created users also need to finish it. | ||
if (routingSpan) { | ||
routingSpan.finish() | ||
routingSpan = undefined | ||
} | ||
return | ||
} | ||
|
||
const from = navigation.from | ||
const to = navigation.to | ||
|
||
// for the origin we can fall back to window.location.pathname because in this emission, it still is set to the origin path | ||
const rawRouteOrigin = | ||
(from && from.url.pathname) || | ||
(WINDOW && WINDOW.location && WINDOW.location.pathname) | ||
|
||
const rawRouteDestination = to && to.url.pathname | ||
|
||
// We don't want to create transactions for navigations of same origin and destination. | ||
// We need to look at the raw URL here because parameterized routes can still differ in their raw parameters. | ||
if (rawRouteOrigin === rawRouteDestination) { | ||
return | ||
} | ||
|
||
const parameterizedRouteOrigin = from && from.route.id | ||
const parameterizedRouteDestination = to && to.route.id | ||
|
||
activeTransaction = getActiveTransaction() | ||
|
||
if (!activeTransaction) { | ||
activeTransaction = startTransactionFn({ | ||
name: parameterizedRouteDestination || rawRouteDestination || 'unknown', | ||
op: 'navigation', | ||
origin: 'auto.navigation.sveltekit', | ||
metadata: { source: parameterizedRouteDestination ? 'route' : 'url' }, | ||
tags: { | ||
...DEFAULT_TAGS | ||
} | ||
}) | ||
} | ||
|
||
if (activeTransaction) { | ||
if (routingSpan) { | ||
// If a routing span is still open from a previous navigation, we finish it. | ||
routingSpan.finish() | ||
} | ||
routingSpan = activeTransaction.startChild({ | ||
op: 'ui.sveltekit.routing', | ||
description: 'SvelteKit Route Change', | ||
origin: 'auto.ui.sveltekit' | ||
}) | ||
activeTransaction.setTag('from', parameterizedRouteOrigin) | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BrowserOptions, configureScope, init } from '@sentry/svelte' | ||
import { addClientIntegrations } from './addClientIntegrations.js' | ||
import { applySdkMetadata } from './applySdkMetadata.js' | ||
import { restoreFetch } from './restoreFetch.js' | ||
import { switchToFetchProxy } from './switchToFetchProxy.js' | ||
|
||
export const sentryInit = (options: BrowserOptions) => { | ||
applySdkMetadata(options, ['sveltekit', 'svelte']) | ||
|
||
addClientIntegrations(options) | ||
|
||
// 1. Switch window.fetch to our fetch proxy we injected earlier | ||
const actualFetch = switchToFetchProxy() | ||
|
||
// 2. Initialize the SDK which will instrument our proxy | ||
init(options) | ||
|
||
// 3. Restore the original fetch now that our proxy is instrumented | ||
if (actualFetch) { | ||
restoreFetch(actualFetch) | ||
} | ||
|
||
configureScope((scope) => { | ||
scope.setTag('runtime', 'browser') | ||
}) | ||
} | ||
Comment on lines
+1
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The // 3. Restore the original fetch now that our proxy is instrumented
if (actualFetch && typeof actualFetch === 'function') {
restoreFetch(actualFetch)
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { WINDOW } from '@sentry/svelte' | ||
|
||
export function switchToFetchProxy() { | ||
const globalWithSentryFetchProxy = WINDOW | ||
|
||
const actualFetch = globalWithSentryFetchProxy.fetch | ||
|
||
// @ts-expect-error TODO: fix this | ||
if (globalWithSentryFetchProxy._sentryFetchProxy && actualFetch) { | ||
globalWithSentryFetchProxy.fetch = | ||
// @ts-expect-error TODO: fix this | ||
globalWithSentryFetchProxy._sentryFetchProxy | ||
return actualFetch | ||
} | ||
|
||
return undefined | ||
} | ||
Comment on lines
+1
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function - if (globalWithSentryFetchProxy._sentryFetchProxy && actualFetch) {
+ if (typeof globalWithSentryFetchProxy._sentryFetchProxy === 'function' && actualFetch) { Also, the function returns |
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.
The function
addClientIntegrations
modifies theoptions
object passed to it. This can lead to unexpected side effects if the caller is not aware of this behavior. Consider returning a new object with the updated integrations instead of modifying the input object. This would make the function pure and easier to reason about.Then, the caller can decide whether to replace the original options object or not.