Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tracing): Favour client options tracePropagationTargets #8399

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/tracing-internal/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,19 @@ export class BrowserTracing implements Integration {

private _collectWebVitals: () => void;

private _hasSetTracePropagationTargets: boolean = false;

public constructor(_options?: Partial<BrowserTracingOptions>) {
addTracingExtensions();

if (__DEBUG_BUILD__) {
this._hasSetTracePropagationTargets = !!(
_options &&
// eslint-disable-next-line deprecation/deprecation
(_options.tracePropagationTargets || _options.tracingOrigins)
);
}

this.options = {
...DEFAULT_BROWSER_TRACING_OPTIONS,
..._options,
Expand Down Expand Up @@ -214,6 +224,9 @@ export class BrowserTracing implements Integration {
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
this._getCurrentHub = getCurrentHub;
const hub = getCurrentHub();
const client = hub.getClient();
const clientOptions = client && client.getOptions();

const {
routingInstrumentation: instrumentRouting,
Expand All @@ -222,11 +235,28 @@ export class BrowserTracing implements Integration {
markBackgroundTransactions,
traceFetch,
traceXHR,
tracePropagationTargets,
shouldCreateSpanForRequest,
_experiments,
} = this.options;

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

instrumentRouting(
(context: TransactionContext) => {
const transaction = this._createRouteTransaction(context);
Expand Down
59 changes: 59 additions & 0 deletions packages/tracing-internal/test/browser/browsertracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,65 @@ describe('BrowserTracing', () => {
tracePropagationTargets: ['something'],
});
});

it('uses `tracePropagationTargets` set by client over integration set targets', () => {
jest.clearAllMocks();
hub.getClient()!.getOptions().tracePropagationTargets = ['something-else'];
const sampleTracePropagationTargets = ['something'];
createBrowserTracing(true, {
routingInstrumentation: customInstrumentRouting,
tracePropagationTargets: sampleTracePropagationTargets,
});

expect(instrumentOutgoingRequestsMock).toHaveBeenCalledWith({
traceFetch: true,
traceXHR: true,
tracePropagationTargets: ['something-else'],
});
});

it.each([
[true, 'tracePropagationTargets', 'defined', { tracePropagationTargets: ['something'] }],
[false, 'tracePropagationTargets', 'undefined', { tracePropagationTargets: undefined }],
[true, 'tracingOrigins', 'defined', { tracingOrigins: ['something'] }],
[false, 'tracingOrigins', 'undefined', { tracingOrigins: undefined }],
[
true,
'tracePropagationTargets and tracingOrigins',
'defined',
{ tracePropagationTargets: ['something'], tracingOrigins: ['something-else'] },
],
[
false,
'tracePropagationTargets and tracingOrigins',
'undefined',
{ tracePropagationTargets: undefined, tracingOrigins: undefined },
],
[
true,
'tracePropagationTargets and tracingOrigins',
'defined and undefined',
{ tracePropagationTargets: ['something'], tracingOrigins: undefined },
],
[
true,
'tracePropagationTargets and tracingOrigins',
'undefined and defined',
{ tracePropagationTargets: undefined, tracingOrigins: ['something'] },
],
])(
'sets `_hasSetTracePropagationTargets` to %s if %s is %s',
(hasSet: boolean, _: string, __: string, options: Partial<BrowserTracingOptions>) => {
jest.clearAllMocks();
const inst = createBrowserTracing(true, {
routingInstrumentation: customInstrumentRouting,
...options,
});

// @ts-ignore accessing private property
expect(inst._hasSetTracePropagationTargets).toBe(hasSet);
},
);
});

describe('beforeNavigate', () => {
Expand Down