Skip to content

Commit c5de662

Browse files
committed
fix(tracing): Make shouldAttachHeaders not fall back to default values
1 parent 5be5a04 commit c5de662

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

packages/tracing/src/browser/request.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export interface RequestInstrumentationOptions {
2121
* Use `shouldCreateSpanForRequest` to control span creation and `tracePropagationTargets` to control
2222
* trace header attachment.
2323
*/
24-
tracingOrigins: Array<string | RegExp>;
24+
tracingOrigins?: Array<string | RegExp>;
2525

2626
/**
2727
* List of strings and/or regexes used to determine which outgoing requests will have `sentry-trace` and `baggage`
2828
* headers attached.
2929
*
3030
* Default: ['localhost', /^\//] {@see DEFAULT_TRACE_PROPAGATION_TARGETS}
3131
*/
32-
tracePropagationTargets: Array<string | RegExp>;
32+
tracePropagationTargets?: Array<string | RegExp>;
3333

3434
/**
3535
* Flag to disable patching all together for fetch requests.
@@ -107,8 +107,6 @@ type PolymorphicRequestHeaders =
107107
export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions = {
108108
traceFetch: true,
109109
traceXHR: true,
110-
tracingOrigins: DEFAULT_TRACING_ORIGINS,
111-
tracePropagationTargets: DEFAULT_TRACE_PROPAGATION_TARGETS,
112110
};
113111

114112
/** Registers span creators for xhr and fetch requests */
@@ -122,8 +120,7 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
122120
const shouldCreateSpan =
123121
typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : (_: string) => true;
124122

125-
const shouldAttachHeaders = (url: string): boolean =>
126-
stringMatchesSomePattern(url, tracingOrigins) || stringMatchesSomePattern(url, tracePropagationTargets);
123+
const shouldAttachHeaders = makeShouldAttachHeaders(tracingOrigins, tracePropagationTargets);
127124

128125
const spans: Record<string, Span> = {};
129126

@@ -140,6 +137,23 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
140137
}
141138
}
142139

140+
/**
141+
* Creates a function that determines whether to attach tracing headers to a request.
142+
* This was extracted from `instrumentOutgoingRequests` to make it easier to test shouldAttachHeaders.
143+
* TODO (v8): Remove `tracingOrigins` which should drastically simplify this function.
144+
*/
145+
export function makeShouldAttachHeaders(
146+
tracePropagationTargets: (string | RegExp)[] | undefined,
147+
tracingOrigins: (string | RegExp)[] | undefined,
148+
) {
149+
return (url: string): boolean => {
150+
if (tracePropagationTargets || tracingOrigins) {
151+
return stringMatchesSomePattern(url, tracePropagationTargets || tracingOrigins);
152+
}
153+
return stringMatchesSomePattern(url, DEFAULT_TRACE_PROPAGATION_TARGETS);
154+
};
155+
}
156+
143157
/**
144158
* Create and track fetch request spans
145159
*/

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import { Hub, makeMain } from '@sentry/core';
33
import * as utils from '@sentry/utils';
44

55
import { Span, spanStatusfromHttpCode, Transaction } from '../../src';
6-
import { fetchCallback, FetchData, instrumentOutgoingRequests, xhrCallback, XHRData } from '../../src/browser/request';
6+
import {
7+
fetchCallback,
8+
FetchData,
9+
instrumentOutgoingRequests,
10+
makeShouldAttachHeaders,
11+
xhrCallback,
12+
XHRData,
13+
} from '../../src/browser/request';
714
import { addExtensionMethods } from '../../src/hubextensions';
815
import * as tracingUtils from '../../src/utils';
916
import { getDefaultBrowserClientOptions } from '../testutils';
@@ -383,3 +390,52 @@ describe('callbacks', () => {
383390
});
384391
});
385392
});
393+
394+
// TODO (v8): Adapt these tests once we remove `tracingOrigins`
395+
describe('[pre-v8]: shouldAttachHeaders', () => {
396+
describe('prefer `tracePropagationTargets` over `tracingOrigins`', () => {
397+
it('should return `true` if the url matches the new tracePropagationTargets', () => {
398+
const shouldAttachHeaders = makeShouldAttachHeaders(['example.com'], undefined);
399+
expect(shouldAttachHeaders('http://example.com')).toBe(true);
400+
});
401+
it('should return `false` if tracePropagationTargets array is empty', () => {
402+
const shouldAttachHeaders = makeShouldAttachHeaders([], ['localhost']);
403+
expect(shouldAttachHeaders('http://localhost:3000/test')).toBe(false);
404+
});
405+
it("should return `false` if tracePropagationTargets array doesn't match", () => {
406+
const shouldAttachHeaders = makeShouldAttachHeaders(['example.com'], ['localhost']);
407+
expect(shouldAttachHeaders('http://localhost:3000/test')).toBe(false);
408+
});
409+
});
410+
411+
describe('tracingOrigins backwards compatibility (tracePropagationTargets not defined)', () => {
412+
it('should return `true` if the url matches tracingOrigns', () => {
413+
const shouldAttachHeaders = makeShouldAttachHeaders(undefined, ['example.com']);
414+
expect(shouldAttachHeaders('http://example.com')).toBe(true);
415+
});
416+
it('should return `false` if tracePropagationTargets array is empty', () => {
417+
const shouldAttachHeaders = makeShouldAttachHeaders(undefined, []);
418+
expect(shouldAttachHeaders('http://localhost:3000/test')).toBe(false);
419+
});
420+
it("should return `false` if tracePropagationTargets array doesn't match", () => {
421+
const shouldAttachHeaders = makeShouldAttachHeaders(undefined, ['example.com']);
422+
expect(shouldAttachHeaders('http://localhost:3000/test')).toBe(false);
423+
});
424+
});
425+
426+
describe('should fall back to defaults if no options are specified', () => {
427+
it.each([
428+
'/api/test',
429+
'http://localhost:3000/test',
430+
'http://somewhere.com/test/localhost/123',
431+
'http://somewhere.com/test?url=localhost:3000&test=123',
432+
])('return `true` for urls matching defaults (%s)', url => {
433+
const shouldAttachHeaders = makeShouldAttachHeaders(undefined, undefined);
434+
expect(shouldAttachHeaders(url)).toBe(true);
435+
});
436+
it.each(['notmydoman/api/test', 'example.com'])('return `false` for urls not matching defaults (%s)', url => {
437+
const shouldAttachHeaders = makeShouldAttachHeaders(undefined, undefined);
438+
expect(shouldAttachHeaders(url)).toBe(false);
439+
});
440+
});
441+
});

0 commit comments

Comments
 (0)