@@ -26,18 +26,33 @@ import {
2626
2727import { instrumentFetchRequest } from '../common/fetch' ;
2828import { addPerformanceInstrumentationHandler } from './instrument' ;
29-
30- export const DEFAULT_TRACE_PROPAGATION_TARGETS = [ 'localhost' , / ^ \/ (? ! \/ ) / ] ;
29+ import { WINDOW } from './types' ;
3130
3231/** Options for Request Instrumentation */
3332export 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/**
0 commit comments