Skip to content

Commit bd7205b

Browse files
authored
feat(browser): Warn on duplicate browserTracingIntegration (#16042)
Closes #16040 this logs a warning if a user adds multiple instances of `browserTracingIntegration`. If this is done, this can lead to potentially weird things (e.g. we add multiple handlers etc). This is especially relevant for react, as there are multiple different integrations there that users may add.
1 parent f9383c7 commit bd7205b

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

.size-limit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = [
4040
path: 'packages/browser/build/npm/esm/index.js',
4141
import: createImport('init', 'browserTracingIntegration'),
4242
gzip: true,
43-
limit: '38 KB',
43+
limit: '39 KB',
4444
},
4545
{
4646
name: '@sentry/browser (incl. Tracing, Replay)',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
integrations: [Sentry.browserTracingIntegration(), Sentry.browserTracingIntegration()],
8+
tracesSampleRate: 1,
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest('warns if multiple integrations are used', async ({ getLocalTestUrl, page }) => {
7+
if (shouldSkipTracingTest()) {
8+
sentryTest.skip();
9+
}
10+
11+
const msgs: string[] = [];
12+
13+
page.on('console', msg => {
14+
msgs.push(msg.text());
15+
});
16+
17+
const url = await getLocalTestUrl({ testDir: __dirname });
18+
19+
await page.goto(url);
20+
21+
expect(msgs).toEqual(['Multiple browserTracingIntegration instances are not supported.']);
22+
});

packages/browser/src/tracing/browserTracingIntegration.ts

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
startTrackingWebVitals,
1111
} from '@sentry-internal/browser-utils';
1212
import type { Client, IntegrationFn, Span, StartSpanOptions, TransactionSource, WebFetchHeaders } from '@sentry/core';
13+
import { consoleSandbox } from '@sentry/core';
1314
import {
1415
GLOBAL_OBJ,
1516
SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON,
@@ -217,6 +218,8 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
217218
...defaultRequestInstrumentationOptions,
218219
};
219220

221+
let _hasBeenInitialized = false;
222+
220223
/**
221224
* The Browser Tracing integration automatically instruments browser pageload/navigation
222225
* actions as transactions, and captures requests, metrics and errors as spans.
@@ -227,6 +230,15 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
227230
* We explicitly export the proper type here, as this has to be extended in some cases.
228231
*/
229232
export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptions> = {}) => {
233+
if (_hasBeenInitialized) {
234+
consoleSandbox(() => {
235+
// eslint-disable-next-line no-console
236+
console.warn('Multiple browserTracingIntegration instances are not supported.');
237+
});
238+
}
239+
240+
_hasBeenInitialized = true;
241+
230242
/**
231243
* This is just a small wrapper that makes `document` optional.
232244
* We want to be extra-safe and always check that this exists, to ensure weird environments do not blow up.

0 commit comments

Comments
 (0)