Skip to content

Commit b098188

Browse files
committed
Add warning if both options are set
1 parent cd93c1c commit b098188

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,19 @@ export class BrowserTracing implements Integration {
177177

178178
private _collectWebVitals: () => void;
179179

180+
private _hasSetTracePropagationTargets: boolean = false;
181+
180182
public constructor(_options?: Partial<BrowserTracingOptions>) {
181183
addTracingExtensions();
182184

185+
if (__DEBUG_BUILD__) {
186+
this._hasSetTracePropagationTargets = !!(
187+
_options &&
188+
// eslint-disable-next-line deprecation/deprecation
189+
(_options.tracePropagationTargets || _options.tracingOrigins)
190+
);
191+
}
192+
183193
this.options = {
184194
...DEFAULT_BROWSER_TRACING_OPTIONS,
185195
..._options,
@@ -229,8 +239,23 @@ export class BrowserTracing implements Integration {
229239
_experiments,
230240
} = this.options;
231241

232-
const tracePropagationTargets =
233-
(clientOptions && clientOptions.tracePropagationTargets) || this.options.tracePropagationTargets;
242+
const clientOptionsTracePropagationTargets = clientOptions && clientOptions.tracePropagationTargets;
243+
// There are three ways to configure tracePropagationTargets:
244+
// 1. via top level client option `tracePropagationTargets`
245+
// 2. via BrowserTracing option `tracePropagationTargets`
246+
// 3. via BrowserTracing option `tracingOrigins` (deprecated)
247+
//
248+
// To avoid confusion, favour top level client option `tracePropagationTargets`, and fallback to
249+
// BrowserTracing option `tracePropagationTargets` and then `tracingOrigins` (deprecated).
250+
// This is done as it minimizes bundle size (we don't have to have undefined checks).
251+
//
252+
// If both 1 and either one of 2 or 3 are set (from above), we log out a warning.
253+
const tracePropagationTargets = clientOptionsTracePropagationTargets || this.options.tracePropagationTargets;
254+
if (__DEBUG_BUILD__ && this._hasSetTracePropagationTargets && clientOptionsTracePropagationTargets) {
255+
logger.warn(
256+
'[Tracing] The `tracePropagationTargets` option was set in the BrowserTracing integration and top level `Sentry.init`. The top level `Sentry.init` value is being used.',
257+
);
258+
}
234259

235260
instrumentRouting(
236261
(context: TransactionContext) => {

packages/tracing-internal/test/browser/browsertracing.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,49 @@ describe('BrowserTracing', () => {
266266
tracePropagationTargets: ['something-else'],
267267
});
268268
});
269+
270+
it.each([
271+
[true, 'tracePropagationTargets', 'defined', { tracePropagationTargets: ['something'] }],
272+
[false, 'tracePropagationTargets', 'undefined', { tracePropagationTargets: undefined }],
273+
[true, 'tracingOrigins', 'defined', { tracingOrigins: ['something'] }],
274+
[false, 'tracingOrigins', 'undefined', { tracingOrigins: undefined }],
275+
[
276+
true,
277+
'tracePropagationTargets and tracingOrigins',
278+
'defined',
279+
{ tracePropagationTargets: ['something'], tracingOrigins: ['something-else'] },
280+
],
281+
[
282+
false,
283+
'tracePropagationTargets and tracingOrigins',
284+
'undefined',
285+
{ tracePropagationTargets: undefined, tracingOrigins: undefined },
286+
],
287+
[
288+
true,
289+
'tracePropagationTargets and tracingOrigins',
290+
'defined and undefined',
291+
{ tracePropagationTargets: ['something'], tracingOrigins: undefined },
292+
],
293+
[
294+
true,
295+
'tracePropagationTargets and tracingOrigins',
296+
'undefined and defined',
297+
{ tracePropagationTargets: undefined, tracingOrigins: ['something'] },
298+
],
299+
])(
300+
'sets `_hasSetTracePropagationTargets` to %s if %s is %s',
301+
(hasSet: boolean, _: string, __: string, options: Partial<BrowserTracingOptions>) => {
302+
jest.clearAllMocks();
303+
const inst = createBrowserTracing(true, {
304+
routingInstrumentation: customInstrumentRouting,
305+
...options,
306+
});
307+
308+
// @ts-ignore accessing private property
309+
expect(inst._hasSetTracePropagationTargets).toBe(hasSet);
310+
},
311+
);
269312
});
270313

271314
describe('beforeNavigate', () => {

0 commit comments

Comments
 (0)