Skip to content

Commit f4f2809

Browse files
committed
feat(browser): Warn on duplicate browserTracingIntegration
1 parent f9383c7 commit f4f2809

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 9 additions & 0 deletions
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+
});
Lines changed: 22 additions & 0 deletions
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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ import {
99
startTrackingLongTasks,
1010
startTrackingWebVitals,
1111
} from '@sentry-internal/browser-utils';
12-
import type { Client, IntegrationFn, Span, StartSpanOptions, TransactionSource, WebFetchHeaders } from '@sentry/core';
12+
import {
13+
Client,
14+
IntegrationFn,
15+
Span,
16+
StartSpanOptions,
17+
TransactionSource,
18+
WebFetchHeaders,
19+
consoleSandbox,
20+
isBrowser,
21+
} from '@sentry/core';
1322
import {
1423
GLOBAL_OBJ,
1524
SEMANTIC_ATTRIBUTE_SENTRY_IDLE_SPAN_FINISH_REASON,
@@ -217,6 +226,8 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
217226
...defaultRequestInstrumentationOptions,
218227
};
219228

229+
let _hasBeenInitialized = false;
230+
220231
/**
221232
* The Browser Tracing integration automatically instruments browser pageload/navigation
222233
* actions as transactions, and captures requests, metrics and errors as spans.
@@ -227,6 +238,15 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
227238
* We explicitly export the proper type here, as this has to be extended in some cases.
228239
*/
229240
export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptions> = {}) => {
241+
if (_hasBeenInitialized && isBrowser()) {
242+
consoleSandbox(() => {
243+
// eslint-disable-next-line no-console
244+
console.warn('Multiple browserTracingIntegration instances are not supported.');
245+
});
246+
}
247+
248+
_hasBeenInitialized = true;
249+
230250
/**
231251
* This is just a small wrapper that makes `document` optional.
232252
* 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)