Skip to content

Commit fafce5b

Browse files
committed
add tracing enablement check to xhr and fetch callbacks
1 parent 436deb1 commit fafce5b

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

packages/tracing/src/browser/request.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { getCurrentHub } from '@sentry/hub';
12
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';
23

34
import { Span } from '../span';
4-
import { getActiveTransaction } from '../utils';
5+
import { getActiveTransaction, hasTracingEnabled } from '../utils';
56

67
export const DEFAULT_TRACING_ORIGINS = ['localhost', /^\//];
78

@@ -142,7 +143,13 @@ export function fetchCallback(
142143
shouldCreateSpan: (url: string) => boolean,
143144
spans: Record<string, Span>,
144145
): void {
145-
if (!handlerData.fetchData || !shouldCreateSpan(handlerData.fetchData.url)) {
146+
const currentClientOptions = getCurrentHub()
147+
.getClient()
148+
?.getOptions();
149+
if (
150+
!(currentClientOptions && hasTracingEnabled(currentClientOptions)) ||
151+
!(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))
152+
) {
146153
return;
147154
}
148155

@@ -209,19 +216,18 @@ export function xhrCallback(
209216
shouldCreateSpan: (url: string) => boolean,
210217
spans: Record<string, Span>,
211218
): void {
212-
if (!handlerData || !handlerData.xhr || !handlerData.xhr.__sentry_xhr__) {
219+
const currentClientOptions = getCurrentHub()
220+
.getClient()
221+
?.getOptions();
222+
if (
223+
!(currentClientOptions && hasTracingEnabled(currentClientOptions)) ||
224+
!(handlerData.xhr && handlerData.xhr.__sentry_xhr__ && shouldCreateSpan(handlerData.xhr.__sentry_xhr__.url)) ||
225+
handlerData.xhr.__sentry_own_request__
226+
) {
213227
return;
214228
}
215229

216230
const xhr = handlerData.xhr.__sentry_xhr__;
217-
if (!shouldCreateSpan(xhr.url)) {
218-
return;
219-
}
220-
221-
// We only capture complete, non-sentry requests
222-
if (handlerData.xhr.__sentry_own_request__) {
223-
return;
224-
}
225231

226232
// check first if the request has finished and is tracked by an existing span which should now end
227233
if (handlerData.endTimestamp && handlerData.xhr.__sentry_xhr_span_id__) {

packages/tracing/test/browser/request.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ beforeAll(() => {
2020
global.Request = {};
2121
});
2222

23+
const hasTracingEnabled = jest.spyOn(tracingUtils, 'hasTracingEnabled');
2324
const addInstrumentationHandler = jest.spyOn(utils, 'addInstrumentationHandler');
2425
const setRequestHeader = jest.fn();
2526

@@ -108,6 +109,30 @@ describe('callbacks', () => {
108109
expect(spans).toEqual({});
109110
});
110111

112+
it('does not add fetch request spans if tracing is disabled', () => {
113+
hasTracingEnabled.mockReturnValueOnce(false);
114+
const spans = {};
115+
116+
fetchCallback(fetchHandlerData, alwaysCreateSpan, spans);
117+
expect(spans).toEqual({});
118+
});
119+
120+
it('does not add fetch request headers if tracing is disabled', () => {
121+
hasTracingEnabled.mockReturnValueOnce(false);
122+
123+
// make a local copy so the global one doesn't get mutated
124+
const handlerData: FetchData = {
125+
args: ['http://dogs.are.great/', {}],
126+
fetchData: { url: 'http://dogs.are.great/', method: 'GET' },
127+
startTimestamp: 1353501072000,
128+
};
129+
130+
fetchCallback(handlerData, alwaysCreateSpan, {});
131+
132+
const headers = (handlerData.args[1].headers as Record<string, string>) || {};
133+
expect(headers['sentry-trace']).not.toBeDefined();
134+
});
135+
111136
it('creates and finishes fetch span on active transaction', () => {
112137
const spans = {};
113138

@@ -174,6 +199,22 @@ describe('callbacks', () => {
174199
expect(spans).toEqual({});
175200
});
176201

202+
it('does not add xhr request spans if tracing is disabled', () => {
203+
hasTracingEnabled.mockReturnValueOnce(false);
204+
const spans = {};
205+
206+
xhrCallback(xhrHandlerData, alwaysCreateSpan, spans);
207+
expect(spans).toEqual({});
208+
});
209+
210+
it('does not add xhr request headers if tracing is disabled', () => {
211+
hasTracingEnabled.mockReturnValueOnce(false);
212+
213+
xhrCallback(xhrHandlerData, alwaysCreateSpan, {});
214+
215+
expect(setRequestHeader).not.toHaveBeenCalled();
216+
});
217+
177218
it('adds sentry-trace header to XHR requests', () => {
178219
xhrCallback(xhrHandlerData, alwaysCreateSpan, {});
179220

0 commit comments

Comments
 (0)