Skip to content

Commit ac435dd

Browse files
authored
feat(tracing): Favour client options tracePropagationTargets (#8399)
ref #8352 As we work toward adding tracing without performance support, this PR updates the `BrowserTracing` integration to use and favour the top level `tracePropagationTargets` option if it exists. This option was made top level in #8395 `tracePropagationTargets` is now part of the unified API for distributed tracing. It's also expected that electron/react native will behave the same way as well. This also leaves us the flexibility to extract tracing out of BrowserTracing, or create a new integration that just does tracing but no performance monitoring. We can make sure this migration is smooth and easy to understand with a good set of docs, which is what I will be working on next. In these docs changes, we'll be updating the automatic instrumentation sections, and formally documented `tracePropagationTargets` as a high level option.
1 parent 0c19446 commit ac435dd

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

Diff for: packages/tracing-internal/src/browser/browsertracing.ts

+31-1
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,
@@ -214,6 +224,9 @@ export class BrowserTracing implements Integration {
214224
*/
215225
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
216226
this._getCurrentHub = getCurrentHub;
227+
const hub = getCurrentHub();
228+
const client = hub.getClient();
229+
const clientOptions = client && client.getOptions();
217230

218231
const {
219232
routingInstrumentation: instrumentRouting,
@@ -222,11 +235,28 @@ export class BrowserTracing implements Integration {
222235
markBackgroundTransactions,
223236
traceFetch,
224237
traceXHR,
225-
tracePropagationTargets,
226238
shouldCreateSpanForRequest,
227239
_experiments,
228240
} = this.options;
229241

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+
}
259+
230260
instrumentRouting(
231261
(context: TransactionContext) => {
232262
const transaction = this._createRouteTransaction(context);

Diff for: packages/tracing-internal/test/browser/browsertracing.test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,65 @@ describe('BrowserTracing', () => {
250250
tracePropagationTargets: ['something'],
251251
});
252252
});
253+
254+
it('uses `tracePropagationTargets` set by client over integration set targets', () => {
255+
jest.clearAllMocks();
256+
hub.getClient()!.getOptions().tracePropagationTargets = ['something-else'];
257+
const sampleTracePropagationTargets = ['something'];
258+
createBrowserTracing(true, {
259+
routingInstrumentation: customInstrumentRouting,
260+
tracePropagationTargets: sampleTracePropagationTargets,
261+
});
262+
263+
expect(instrumentOutgoingRequestsMock).toHaveBeenCalledWith({
264+
traceFetch: true,
265+
traceXHR: true,
266+
tracePropagationTargets: ['something-else'],
267+
});
268+
});
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+
);
253312
});
254313

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

0 commit comments

Comments
 (0)