|
| 1 | +import { defaultRequestInstrumentationOptions } from '@sentry-internal/tracing'; |
| 2 | +import { hasTracingEnabled } from '@sentry/core'; |
1 | 3 | import type { BrowserOptions } from '@sentry/svelte'; |
2 | | -import { configureScope, init as initSvelteSdk } from '@sentry/svelte'; |
| 4 | +import { BrowserTracing, configureScope, init as initSvelteSdk } from '@sentry/svelte'; |
| 5 | +import { addOrUpdateIntegration } from '@sentry/utils'; |
3 | 6 |
|
4 | 7 | import { applySdkMetadata } from '../common/metadata'; |
5 | 8 |
|
| 9 | +// Treeshakable guard to remove all code related to tracing |
| 10 | +declare const __SENTRY_TRACING__: boolean; |
| 11 | + |
6 | 12 | /** |
7 | 13 | * |
8 | 14 | * @param options |
9 | 15 | */ |
10 | 16 | export function init(options: BrowserOptions): void { |
11 | 17 | applySdkMetadata(options, ['sveltekit', 'svelte']); |
12 | 18 |
|
| 19 | + addClientIntegrations(options); |
| 20 | + |
13 | 21 | initSvelteSdk(options); |
14 | 22 |
|
15 | 23 | configureScope(scope => { |
16 | 24 | scope.setTag('runtime', 'browser'); |
17 | 25 | }); |
18 | 26 | } |
| 27 | + |
| 28 | +function addClientIntegrations(options: BrowserOptions): void { |
| 29 | + let integrations = options.integrations || []; |
| 30 | + |
| 31 | + // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside |
| 32 | + // will get treeshaken away |
| 33 | + if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) { |
| 34 | + if (hasTracingEnabled(options)) { |
| 35 | + const defaultBrowserTracingIntegration = new BrowserTracing({ |
| 36 | + tracePropagationTargets: [...defaultRequestInstrumentationOptions.tracePropagationTargets, /^(api\/)/], |
| 37 | + // TODO: Add SvelteKit router instrumentations |
| 38 | + // routingInstrumentation: sveltekitRoutingInstrumentation, |
| 39 | + }); |
| 40 | + |
| 41 | + integrations = addOrUpdateIntegration(defaultBrowserTracingIntegration, integrations, { |
| 42 | + // TODO: Add SvelteKit router instrumentations |
| 43 | + // options.routingInstrumentation: sveltekitRoutingInstrumentation, |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + options.integrations = integrations; |
| 49 | +} |
0 commit comments