Skip to content

Commit 7dc9260

Browse files
authored
ref(tracing): Implement changes to new browserTracingIntegration (getsentry#10393)
This implements changes to the new integration, based on feedback from the team. The main thing this addresses is the way we disable the default span creation. The original idea was to make this work with custom router instrumentations (e.g. for react router) that may be added as separate integrations. This required us to keep track of if another, non-default implementation has been added, to opt out of the defaults. With this change, we do not do this anymore. Instead, the defaults should be configured by wrapping integrations. Some examples for how this should then work: 1. Angular: ```ts export function browserTracingIntegration( options?: Parameters<typeof originalBrowserTracingIntegration>[0], ): Integration { // do not use default navigation, as we provide our own // actual code for this is in the angular service options.instrumentNavigation = false; return originalBrowserTracingIntegration(options); } ``` 2. Vue ```ts export function browserTracingIntegration( options?: Parameters<typeof originalBrowserTracingIntegration>[0] & { router?: VueRouter }, ): Integration { if (options.router) { options.instrumentNavigation = false; const integration = originalBrowserTracingIntegration(options); const originalSetupAfterAll = integration.setupAfterAll; integration.setupAfterAll = (client: Client) => { setupVueRoutingInstrumentation(client); // some method originalSetupAfterAll(client); } } return originalBrowserTracingIntegration(options); } ``` 3. React ```ts export function browserTracingIntegration( options?: Parameters<typeof originalBrowserTracingIntegration>[0] & { router?: ReactRouterInstrumentation }, ): Integration { if (options.router) { options.instrumentNavigation = false; options.instrumentPageLoad = false; const integration = originalBrowserTracingIntegration(options); const originalSetupAfterAll = integration.setupAfterAll; integration.setupAfterAll = (client: Client) => { // setup custom routing instrumentation options.router(client); // or whatever API we want there originalSetupAfterAll(client); } } return originalBrowserTracingIntegration(options); } ```
1 parent 78c6a9d commit 7dc9260

File tree

4 files changed

+11
-53
lines changed

4 files changed

+11
-53
lines changed

packages/browser/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ export {
6060
browserTracingIntegration,
6161
startBrowserTracingNavigationSpan,
6262
startBrowserTracingPageLoadSpan,
63-
disableDefaultBrowserTracingNavigationSpan,
64-
disableDefaultBrowserTracingPageLoadSpan,
6563
} from '@sentry-internal/tracing';
6664
export type { RequestInstrumentationOptions } from '@sentry-internal/tracing';
6765
export {

packages/tracing-internal/src/browser/browserTracingIntegration.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
7474

7575
/**
7676
* If a span should be created on page load.
77+
* If this is set to `false`, this integration will not start the default page load span.
7778
* Default: true
7879
*/
7980
instrumentPageLoad: boolean;
8081

8182
/**
8283
* If a span should be created on navigation (history change).
84+
* If this is set to `false`, this integration will not start the default navigation spans.
8385
* Default: true
8486
*/
8587
instrumentNavigation: boolean;
@@ -143,9 +145,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
143145
...defaultRequestInstrumentationOptions,
144146
};
145147

146-
let shouldUseDefaultPageLoadSpan = true;
147-
let shouldUseDefaultNavigationSpan = true;
148-
149148
/**
150149
* The Browser Tracing integration automatically instruments browser pageload/navigation
151150
* actions as transactions, and captures requests, metrics and errors as spans.
@@ -308,13 +307,6 @@ export const _browserTracingIntegration = ((_options: Partial<BrowserTracingOpti
308307

309308
if (client.on) {
310309
client.on('startNavigationSpan', (context: StartSpanOptions) => {
311-
// We check this inside of the hook handler, so that if a custom instrumentation triggers this,
312-
// we don't need to check this option in the instrumentation, but can simply invoke it
313-
// without needing to know the options of this integration
314-
if (!options.instrumentNavigation) {
315-
return;
316-
}
317-
318310
if (activeSpan) {
319311
DEBUG_BUILD && logger.log(`[Tracing] Finishing current transaction with op: ${spanToJSON(activeSpan).op}`);
320312
// If there's an open transaction on the scope, we need to finish it before creating an new one.
@@ -324,13 +316,6 @@ export const _browserTracingIntegration = ((_options: Partial<BrowserTracingOpti
324316
});
325317

326318
client.on('startPageLoadSpan', (context: StartSpanOptions) => {
327-
// We check this inside of the hook handler, so that if a custom instrumentation triggers this,
328-
// we don't need to check this option in the instrumentation, but can simply invoke it
329-
// without needing to know the options of this integration
330-
if (!options.instrumentPageLoad) {
331-
return;
332-
}
333-
334319
if (activeSpan) {
335320
DEBUG_BUILD && logger.log(`[Tracing] Finishing current transaction with op: ${spanToJSON(activeSpan).op}`);
336321
// If there's an open transaction on the scope, we need to finish it before creating an new one.
@@ -340,7 +325,7 @@ export const _browserTracingIntegration = ((_options: Partial<BrowserTracingOpti
340325
});
341326
}
342327

343-
if (options.instrumentPageLoad && client.emit && shouldUseDefaultPageLoadSpan) {
328+
if (options.instrumentPageLoad && client.emit) {
344329
const context: StartSpanOptions = {
345330
name: WINDOW.location.pathname,
346331
// pageload should always start at timeOrigin (and needs to be in s, not ms)
@@ -370,17 +355,14 @@ export const _browserTracingIntegration = ((_options: Partial<BrowserTracingOpti
370355

371356
if (from !== to) {
372357
startingUrl = undefined;
373-
// We check this in here again, as a custom instrumentation may have been triggered in the meanwhile
374-
if (shouldUseDefaultNavigationSpan) {
375-
const context: StartSpanOptions = {
376-
name: WINDOW.location.pathname,
377-
op: 'navigation',
378-
origin: 'auto.navigation.browser',
379-
metadata: { source: 'url' },
380-
};
381-
382-
startBrowserTracingNavigationSpan(context);
383-
}
358+
const context: StartSpanOptions = {
359+
name: WINDOW.location.pathname,
360+
op: 'navigation',
361+
origin: 'auto.navigation.browser',
362+
metadata: { source: 'url' },
363+
};
364+
365+
startBrowserTracingNavigationSpan(context);
384366
}
385367
});
386368
}
@@ -420,7 +402,6 @@ export function startBrowserTracingPageLoadSpan(spanOptions: StartSpanOptions):
420402
}
421403

422404
client.emit('startPageLoadSpan', spanOptions);
423-
shouldUseDefaultPageLoadSpan = false;
424405
}
425406

426407
/**
@@ -434,23 +415,6 @@ export function startBrowserTracingNavigationSpan(spanOptions: StartSpanOptions)
434415
}
435416

436417
client.emit('startNavigationSpan', spanOptions);
437-
shouldUseDefaultNavigationSpan = false;
438-
}
439-
440-
/**
441-
* Use this method if you want to disable the default navigation span.
442-
* This is useful if you want to add custom routing instrumentation.
443-
*/
444-
export function disableDefaultBrowserTracingNavigationSpan(disable = true): void {
445-
shouldUseDefaultNavigationSpan = !disable;
446-
}
447-
448-
/**
449-
* Use this method if you want to disable the default page load span.
450-
* This is useful if you want to add custom routing instrumentation.
451-
*/
452-
export function disableDefaultBrowserTracingPageLoadSpan(disable = true): void {
453-
shouldUseDefaultPageLoadSpan = !disable;
454418
}
455419

456420
/** Returns the value of a meta tag */

packages/tracing-internal/src/browser/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export {
77
browserTracingIntegration,
88
startBrowserTracingNavigationSpan,
99
startBrowserTracingPageLoadSpan,
10-
disableDefaultBrowserTracingNavigationSpan,
11-
disableDefaultBrowserTracingPageLoadSpan,
1210
} from './browserTracingIntegration';
1311

1412
export { instrumentOutgoingRequests, defaultRequestInstrumentationOptions } from './request';

packages/tracing-internal/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export {
1717
browserTracingIntegration,
1818
startBrowserTracingNavigationSpan,
1919
startBrowserTracingPageLoadSpan,
20-
disableDefaultBrowserTracingNavigationSpan,
21-
disableDefaultBrowserTracingPageLoadSpan,
2220
BROWSER_TRACING_INTEGRATION_ID,
2321
instrumentOutgoingRequests,
2422
defaultRequestInstrumentationOptions,

0 commit comments

Comments
 (0)