Skip to content

Commit 0ebefdf

Browse files
fix(tracing): Use deprecated tracing origins option if set (#3331)
1 parent c3f54b5 commit 0ebefdf

File tree

3 files changed

+169
-13
lines changed

3 files changed

+169
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
- Change log output to show what paths are considered when collecting modules ([#3316](https://github.com/getsentry/sentry-react-native/pull/3316))
5757
- Ignore defaults when warning about duplicate definition of trace propagation targets ([#3327](https://github.com/getsentry/sentry-react-native/pull/3327))
58+
- Use deprecated `ReactNativeTracingOptions.tracingOrigins` if set in the options ([#3331](https://github.com/getsentry/sentry-react-native/pull/3331))
5859

5960
### Dependencies
6061

src/js/tracing/reactnativetracing.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export interface ReactNativeTracingOptions extends RequestInstrumentationOptions
9595
enableUserInteractionTracing: boolean;
9696
}
9797

98+
const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\/(?!\/)/];
99+
98100
const defaultReactNativeTracingOptions: ReactNativeTracingOptions = {
99101
...defaultRequestInstrumentationOptions,
100102
idleTimeout: 1000,
@@ -137,17 +139,19 @@ export class ReactNativeTracing implements Integration {
137139
private _appStartFinishTimestamp?: number;
138140
private _currentRoute?: string;
139141
private _hasSetTracePropagationTargets: boolean;
142+
private _hasSetTracingOrigins: boolean;
140143

141144
public constructor(options: Partial<ReactNativeTracingOptions> = {}) {
142-
this._hasSetTracePropagationTargets = false;
143-
144-
if (__DEV__) {
145-
this._hasSetTracePropagationTargets = !!(
146-
options &&
147-
// eslint-disable-next-line deprecation/deprecation
148-
(options.tracePropagationTargets || options.tracingOrigins)
149-
);
150-
}
145+
this._hasSetTracePropagationTargets = !!(
146+
options &&
147+
// eslint-disable-next-line deprecation/deprecation
148+
options.tracePropagationTargets
149+
);
150+
this._hasSetTracingOrigins = !!(
151+
options &&
152+
// eslint-disable-next-line deprecation/deprecation
153+
options.tracingOrigins
154+
);
151155

152156
this.options = {
153157
...defaultReactNativeTracingOptions,
@@ -203,8 +207,16 @@ export class ReactNativeTracing implements Integration {
203207
// ReactNativeTracing option `tracePropagationTargets` and then `tracingOrigins` (deprecated).
204208
//
205209
// If both 1 and either one of 2 or 3 are set (from above), we log out a warning.
206-
const tracePropagationTargets = clientOptionsTracePropagationTargets || thisOptionsTracePropagationTargets;
207-
if (__DEV__ && this._hasSetTracePropagationTargets && clientOptionsTracePropagationTargets) {
210+
const tracePropagationTargets =
211+
clientOptionsTracePropagationTargets ||
212+
(this._hasSetTracePropagationTargets && thisOptionsTracePropagationTargets) ||
213+
(this._hasSetTracingOrigins && tracingOrigins) ||
214+
DEFAULT_TRACE_PROPAGATION_TARGETS;
215+
if (
216+
__DEV__ &&
217+
(this._hasSetTracePropagationTargets || this._hasSetTracingOrigins) &&
218+
clientOptionsTracePropagationTargets
219+
) {
208220
logger.warn(
209221
'[ReactNativeTracing] The `tracePropagationTargets` option was set in the ReactNativeTracing integration and top level `Sentry.init`. The top level `Sentry.init` value is being used.',
210222
);
@@ -246,7 +258,6 @@ export class ReactNativeTracing implements Integration {
246258
instrumentOutgoingRequests({
247259
traceFetch,
248260
traceXHR,
249-
tracingOrigins,
250261
shouldCreateSpanForRequest,
251262
tracePropagationTargets,
252263
});

test/tracing/reactnativetracing.test.ts

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type { SpanStatusType, User } from '@sentry/browser';
3-
import { BrowserClient } from '@sentry/browser';
3+
import * as SentryBrowser from '@sentry/browser';
44
import type { IdleTransaction } from '@sentry/core';
55
import { addGlobalEventProcessor, Hub, Transaction } from '@sentry/core';
66

77
import type { NativeAppStartResponse } from '../../src/js/NativeRNSentry';
88
import { RoutingInstrumentation } from '../../src/js/tracing/routingInstrumentation';
99

10+
const BrowserClient = SentryBrowser.BrowserClient;
11+
1012
jest.mock('../../src/js/wrapper', () => {
1113
return {
1214
NATIVE: {
@@ -117,6 +119,148 @@ describe('ReactNativeTracing', () => {
117119
jest.clearAllMocks();
118120
});
119121

122+
describe('trace propagation targets', () => {
123+
it('uses tracingOrigins', () => {
124+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
125+
const mockedHub = {
126+
getClient: () => ({
127+
getOptions: () => ({}),
128+
}),
129+
};
130+
131+
const integration = new ReactNativeTracing({
132+
tracingOrigins: ['test1', 'test2'],
133+
});
134+
integration.setupOnce(
135+
() => {},
136+
() => mockedHub as unknown as Hub,
137+
);
138+
139+
expect(instrumentOutgoingRequests).toBeCalledWith(
140+
expect.objectContaining({
141+
tracePropagationTargets: ['test1', 'test2'],
142+
}),
143+
);
144+
});
145+
146+
it('uses tracePropagationTargets', () => {
147+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
148+
const mockedHub = {
149+
getClient: () => ({
150+
getOptions: () => ({}),
151+
}),
152+
};
153+
154+
const integration = new ReactNativeTracing({
155+
tracePropagationTargets: ['test1', 'test2'],
156+
});
157+
integration.setupOnce(
158+
() => {},
159+
() => mockedHub as unknown as Hub,
160+
);
161+
162+
expect(instrumentOutgoingRequests).toBeCalledWith(
163+
expect.objectContaining({
164+
tracePropagationTargets: ['test1', 'test2'],
165+
}),
166+
);
167+
});
168+
169+
it('uses tracePropagationTargets from client options', () => {
170+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
171+
const mockedHub = {
172+
getClient: () => ({
173+
getOptions: () => ({
174+
tracePropagationTargets: ['test1', 'test2'],
175+
}),
176+
}),
177+
};
178+
179+
const integration = new ReactNativeTracing({});
180+
integration.setupOnce(
181+
() => {},
182+
() => mockedHub as unknown as Hub,
183+
);
184+
185+
expect(instrumentOutgoingRequests).toBeCalledWith(
186+
expect.objectContaining({
187+
tracePropagationTargets: ['test1', 'test2'],
188+
}),
189+
);
190+
});
191+
192+
it('uses defaults', () => {
193+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
194+
const mockedHub = {
195+
getClient: () => ({
196+
getOptions: () => ({}),
197+
}),
198+
};
199+
200+
const integration = new ReactNativeTracing({});
201+
integration.setupOnce(
202+
() => {},
203+
() => mockedHub as unknown as Hub,
204+
);
205+
206+
expect(instrumentOutgoingRequests).toBeCalledWith(
207+
expect.objectContaining({
208+
tracePropagationTargets: ['localhost', /^\/(?!\/)/],
209+
}),
210+
);
211+
});
212+
213+
it('client tracePropagationTargets takes priority over integration options', () => {
214+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
215+
const mockedHub = {
216+
getClient: () => ({
217+
getOptions: () => ({
218+
tracePropagationTargets: ['test1', 'test2'],
219+
}),
220+
}),
221+
};
222+
223+
const integration = new ReactNativeTracing({
224+
tracePropagationTargets: ['test3', 'test4'],
225+
tracingOrigins: ['test5', 'test6'],
226+
});
227+
integration.setupOnce(
228+
() => {},
229+
() => mockedHub as unknown as Hub,
230+
);
231+
232+
expect(instrumentOutgoingRequests).toBeCalledWith(
233+
expect.objectContaining({
234+
tracePropagationTargets: ['test1', 'test2'],
235+
}),
236+
);
237+
});
238+
239+
it('integration tracePropagationTargets takes priority over tracingOrigins', () => {
240+
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
241+
const mockedHub = {
242+
getClient: () => ({
243+
getOptions: () => ({}),
244+
}),
245+
};
246+
247+
const integration = new ReactNativeTracing({
248+
tracePropagationTargets: ['test3', 'test4'],
249+
tracingOrigins: ['test5', 'test6'],
250+
});
251+
integration.setupOnce(
252+
() => {},
253+
() => mockedHub as unknown as Hub,
254+
);
255+
256+
expect(instrumentOutgoingRequests).toBeCalledWith(
257+
expect.objectContaining({
258+
tracePropagationTargets: ['test3', 'test4'],
259+
}),
260+
);
261+
});
262+
});
263+
120264
describe('App Start', () => {
121265
describe('Without routing instrumentation', () => {
122266
it('Starts route transaction (cold)', async () => {

0 commit comments

Comments
 (0)