Skip to content

Commit 4fef45f

Browse files
committed
feat(browser): Extract linked traces into linkedTracesIntegration
`browserTracingIntegration` registers it by default, so nothing changes for its users. `browserTracingCoreIntegration` users only bundle it when they add it.
1 parent 9eb974f commit 4fef45f

4 files changed

Lines changed: 72 additions & 10 deletions

File tree

‎packages/browser/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
} from '@sentry/browser-utils';
5252
export type { CachedRouteProvider, RouteProvider } from '@sentry/browser-utils';
5353
export { interactionsIntegration } from './integrations/interactions';
54+
export { linkedTracesIntegration } from './integrations/linkedTraces';
5455
export { longTasksIntegration } from './integrations/longTasks';
5556
export { xhrTracingIntegration } from './integrations/xhrTracing';
5657

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { IntegrationFn } from '@sentry/core';
2+
import { defineIntegration } from '@sentry/core';
3+
import { linkTraces } from '../tracing/linkedTraces';
4+
5+
interface LinkedTracesOptions {
6+
/**
7+
* Where the previous trace is stored: `'in-memory'` for single-page applications, or
8+
* `'session-storage'` for multi-page applications, which writes to the browser's `sessionStorage`.
9+
*
10+
* Default: 'in-memory'
11+
*/
12+
linkPreviousTrace?: 'in-memory' | 'session-storage';
13+
14+
/**
15+
* If true, subsequent traces are sampled consistently with the sampling decision of the initial trace.
16+
*
17+
* Default: false
18+
*/
19+
consistentTraceSampling?: boolean;
20+
}
21+
22+
const _linkedTracesIntegration = ((options: LinkedTracesOptions = {}) => {
23+
const { linkPreviousTrace = 'in-memory', consistentTraceSampling = false } = options;
24+
25+
return {
26+
name: 'LinkedTraces',
27+
setup(client) {
28+
linkTraces(client, { linkPreviousTrace, consistentTraceSampling });
29+
},
30+
};
31+
}) satisfies IntegrationFn;
32+
33+
/**
34+
* Links each new trace to the previous one, so you can navigate between traces in Sentry.
35+
*/
36+
export const linkedTracesIntegration = defineIntegration(_linkedTracesIntegration);

‎packages/browser/src/tracing/browserTracingIntegration.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { getHttpRequestData, WINDOW } from '../helpers';
4747
import type { WebVitalsOptions } from '../integrations/webVitals';
4848
import { webVitalsIntegration } from '../integrations/webVitals';
4949
import { registerBackgroundTabDetection } from './backgroundtab';
50-
import { linkTraces } from './linkedTraces';
50+
import { linkedTracesIntegration } from '../integrations/linkedTraces';
5151
import { xhrTracingIntegration } from '../integrations/xhrTracing';
5252
import { defaultRequestInstrumentationOptions, instrumentFetchRequests } from './request';
5353
import {
@@ -287,7 +287,14 @@ export interface BrowserTracingOptions {
287287
*/
288288
export type BrowserTracingCoreOptions = Omit<
289289
BrowserTracingOptions,
290-
'enableInp' | 'webVitals' | 'enableElementTiming' | 'enableLongTask' | 'enableLongAnimationFrame' | 'traceXHR'
290+
| 'enableInp'
291+
| 'webVitals'
292+
| 'enableElementTiming'
293+
| 'enableLongTask'
294+
| 'enableLongAnimationFrame'
295+
| 'traceXHR'
296+
| 'linkPreviousTrace'
297+
| 'consistentTraceSampling'
291298
>;
292299

293300
const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = {
@@ -298,8 +305,6 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingCoreOptions = {
298305
markBackgroundSpan: true,
299306
ignoreResourceSpans: [],
300307
detectRedirects: true,
301-
linkPreviousTrace: 'in-memory',
302-
consistentTraceSampling: false,
303308
enableReportPageLoaded: false,
304309
...defaultRequestInstrumentationOptions,
305310
};
@@ -334,6 +339,8 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
334339
enableHTTPTimings,
335340
onRequestSpanStart,
336341
onRequestSpanEnd,
342+
linkPreviousTrace = 'in-memory',
343+
consistentTraceSampling,
337344
} = options;
338345

339346
return _browserTracingIntegration(options, client => {
@@ -359,6 +366,10 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
359366
xhrTracingIntegration({ shouldCreateSpanForRequest, enableHTTPTimings, onRequestSpanStart, onRequestSpanEnd }),
360367
);
361368
}
369+
370+
if (linkPreviousTrace !== 'off') {
371+
client.addIntegration(linkedTracesIntegration({ linkPreviousTrace, consistentTraceSampling }));
372+
}
362373
});
363374
}) satisfies IntegrationFn;
364375

@@ -396,8 +407,6 @@ function _browserTracingIntegration(
396407
instrumentNavigation,
397408
instrumentBfcacheRestore,
398409
detectRedirects,
399-
linkPreviousTrace,
400-
consistentTraceSampling,
401410
enableReportPageLoaded,
402411
onRequestSpanStart,
403412
onRequestSpanEnd,
@@ -674,10 +683,6 @@ function _browserTracingIntegration(
674683

675684
let startingUrl: string | undefined = getLocationHref();
676685

677-
if (linkPreviousTrace !== 'off') {
678-
linkTraces(client, { linkPreviousTrace, consistentTraceSampling });
679-
}
680-
681686
if (WINDOW.location) {
682687
if (instrumentPageLoad) {
683688
startBrowserTracingPageLoadSpan(client, {

‎packages/browser/test/tracing/browserTracingIntegration.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ describe('browserTracingIntegration', () => {
262262
expect(!!client.getIntegrationByName('XhrTracing')).toBe(registered);
263263
});
264264

265+
it.each([
266+
['registers linkedTracesIntegration by default', {}, true],
267+
[
268+
'does not register linkedTracesIntegration when linkPreviousTrace is off',
269+
{ linkPreviousTrace: 'off' as const },
270+
false,
271+
],
272+
])('%s', (_name, options, registered) => {
273+
const client = new BrowserClient(
274+
getDefaultBrowserClientOptions({
275+
tracesSampleRate: 1,
276+
integrations: [browserTracingIntegration(options)],
277+
}),
278+
);
279+
setCurrentClient(client);
280+
client.init();
281+
282+
expect(!!client.getIntegrationByName('LinkedTraces')).toBe(registered);
283+
});
284+
265285
it('keeps the webVitalsIntegration the user supplies', () => {
266286
const userWebVitals = webVitalsModule.webVitalsIntegration({ softNavigations: false });
267287

0 commit comments

Comments
 (0)