Skip to content

Commit c546267

Browse files
author
Luca Forstner
committed
feat: Update default trace propagation targets logic
1 parent 0e7ab1e commit c546267

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

packages/browser/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export {
6262
browserTracingIntegration,
6363
startBrowserTracingNavigationSpan,
6464
startBrowserTracingPageLoadSpan,
65-
DEFAULT_TRACE_PROPAGATION_TARGETS,
6665
} from '@sentry-internal/tracing';
6766
export type { RequestInstrumentationOptions } from '@sentry-internal/tracing';
6867
export {

packages/nextjs/src/client/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
22
import type { BrowserOptions } from '@sentry/react';
33
import {
4-
DEFAULT_TRACE_PROPAGATION_TARGETS,
54
getCurrentScope,
65
getDefaultIntegrations as getReactDefaultIntegrations,
76
init as reactInit,
@@ -29,17 +28,6 @@ declare const __SENTRY_TRACING__: boolean;
2928
export function init(options: BrowserOptions): void {
3029
const opts = {
3130
environment: getVercelEnv(true) || process.env.NODE_ENV,
32-
33-
tracePropagationTargets:
34-
process.env.NODE_ENV === 'development'
35-
? [
36-
// Will match any URL that contains "localhost" but not "webpack.hot-update.json" - The webpack dev-server
37-
// has cors and it doesn't like extra headers when it's accessed from a different URL.
38-
// TODO(v8): Ideally we rework our tracePropagationTargets logic so this hack won't be necessary anymore (see issue #9764)
39-
/^(?=.*localhost)(?!.*webpack\.hot-update\.json).*/,
40-
/^\/(?!\/)/,
41-
]
42-
: [...DEFAULT_TRACE_PROPAGATION_TARGETS, /^(api\/)/],
4331
defaultIntegrations: getDefaultIntegrations(options),
4432
...options,
4533
} satisfies BrowserOptions;

packages/tracing-internal/src/browser/request.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,33 @@ import {
2626

2727
import { instrumentFetchRequest } from '../common/fetch';
2828
import { addPerformanceInstrumentationHandler } from './instrument';
29-
30-
export const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\/(?!\/)/];
29+
import { WINDOW } from './types';
3130

3231
/** Options for Request Instrumentation */
3332
export interface RequestInstrumentationOptions {
3433
/**
3534
* List of strings and/or Regular Expressions used to determine which outgoing requests will have `sentry-trace` and `baggage`
3635
* headers attached.
3736
*
38-
* Default: ['localhost', /^\//]
37+
* By default, if this option is not provided, tracing headers will be attached to all outgoing requests to the same origin as the current page.
38+
*
39+
* NOTE: Carelessly setting this option may result into CORS errors.
40+
*
41+
* If you provide a `tracePropagationTargets` array, the entries you provide will be matched against two values:
42+
* - The entire URL of the outgoing request.
43+
* - The pathname of the outgoing request (only if it is a same-origin request)
44+
*
45+
* If any of the two match any of the provided values, tracing headers will be attached to the outgoing request.
46+
*
47+
* Examples:
48+
* - `tracePropagationTargets: [/^\/api/]` and request to `https://same-origin.com/api/posts`:
49+
* - Tracing headers will be attached because the request is sent to the same origin and the regex matches the pathname "/api/posts".
50+
* - `tracePropagationTargets: [/^\/api/]` and request to `https://different-origin.com/api/posts`:
51+
* - Tracing headers will not be attached because the pathname will only be compared when the request target lives on the same origin.
52+
* - `tracePropagationTargets: [/^\/api/, 'https://external-api.com']` and request to `https://external-api.com/v1/data`:
53+
* - Tracing headers will be attached because the request URL matches the string `'https://external-api.com'`.
3954
*/
40-
tracePropagationTargets: Array<string | RegExp>;
55+
tracePropagationTargets?: Array<string | RegExp>;
4156

4257
/**
4358
* Flag to disable patching all together for fetch requests.
@@ -73,7 +88,6 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
7388
traceFetch: true,
7489
traceXHR: true,
7590
enableHTTPTimings: true,
76-
tracePropagationTargets: DEFAULT_TRACE_PROPAGATION_TARGETS,
7791
};
7892

7993
/** Registers span creators for xhr and fetch requests */
@@ -208,10 +222,23 @@ function resourceTimingEntryToSpanData(resourceTiming: PerformanceResourceTiming
208222
/**
209223
* A function that determines whether to attach tracing headers to a request.
210224
* This was extracted from `instrumentOutgoingRequests` to make it easier to test shouldAttachHeaders.
211-
* We only export this fuction for testing purposes.
225+
* We only export this function for testing purposes.
212226
*/
213-
export function shouldAttachHeaders(url: string, tracePropagationTargets: (string | RegExp)[] | undefined): boolean {
214-
return stringMatchesSomePattern(url, tracePropagationTargets || DEFAULT_TRACE_PROPAGATION_TARGETS);
227+
export function shouldAttachHeaders(
228+
targetUrl: string,
229+
tracePropagationTargets: (string | RegExp)[] | undefined,
230+
): boolean {
231+
const resolvedUrl = new URL(targetUrl, WINDOW.location.origin);
232+
const isSameOriginRequest = resolvedUrl.origin === WINDOW.location.origin;
233+
234+
if (!tracePropagationTargets) {
235+
return isSameOriginRequest;
236+
}
237+
238+
return (
239+
stringMatchesSomePattern(resolvedUrl.toString(), tracePropagationTargets) ||
240+
(isSameOriginRequest && stringMatchesSomePattern(resolvedUrl.pathname, tracePropagationTargets))
241+
);
215242
}
216243

217244
/**

packages/tracing-internal/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,3 @@ export { addTracingHeadersToFetchRequest, instrumentFetchRequest } from './commo
3232
export type { RequestInstrumentationOptions } from './browser';
3333

3434
export { addExtensionMethods } from './extensions';
35-
36-
export { DEFAULT_TRACE_PROPAGATION_TARGETS } from './browser/request';

packages/types/src/options.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,26 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
223223
denyUrls?: Array<string | RegExp>;
224224

225225
/**
226-
* List of strings/regex controlling to which outgoing requests
227-
* the SDK will attach tracing headers.
226+
* List of strings and/or Regular Expressions used to determine which outgoing requests will have `sentry-trace` and `baggage`
227+
* headers attached.
228228
*
229-
* By default the SDK will attach those headers to all requests to localhost
230-
* and same origin. If this option is provided, the SDK will match the
231-
* request URL of outgoing requests against the items in this
232-
* array, and only attach tracing headers if a match was found.
229+
* By default, if this option is not provided, tracing headers will be attached to all outgoing requests to the same origin as the current page.
233230
*
234-
* @example
235-
* ```js
236-
* Sentry.init({
237-
* tracePropagationTargets: ['api.site.com'],
238-
* });
239-
* ```
231+
* NOTE: Carelessly setting this option may result into CORS errors.
240232
*
241-
* Default: ['localhost', /^\//] {@see DEFAULT_TRACE_PROPAGATION_TARGETS}
233+
* If you provide a `tracePropagationTargets` array, the entries you provide will be matched against two values:
234+
* - The entire URL of the outgoing request.
235+
* - The pathname of the outgoing request (only if it is a same-origin request)
236+
*
237+
* If any of the two match any of the provided values, tracing headers will be attached to the outgoing request.
238+
*
239+
* Examples:
240+
* - `tracePropagationTargets: [/^\/api/]` and request to `https://same-origin.com/api/posts`:
241+
* - Tracing headers will be attached because the request is sent to the same origin and the regex matches the pathname "/api/posts".
242+
* - `tracePropagationTargets: [/^\/api/]` and request to `https://different-origin.com/api/posts`:
243+
* - Tracing headers will not be attached because the pathname will only be compared when the request target lives on the same origin.
244+
* - `tracePropagationTargets: [/^\/api/, 'https://external-api.com']` and request to `https://external-api.com/v1/data`:
245+
* - Tracing headers will be attached because the request URL matches the string `'https://external-api.com'`.
242246
*/
243247
tracePropagationTargets?: TracePropagationTargets;
244248

0 commit comments

Comments
 (0)