@@ -26,18 +26,33 @@ import {
26
26
27
27
import { instrumentFetchRequest } from '../common/fetch' ;
28
28
import { addPerformanceInstrumentationHandler } from './instrument' ;
29
-
30
- export const DEFAULT_TRACE_PROPAGATION_TARGETS = [ 'localhost' , / ^ \/ (? ! \/ ) / ] ;
29
+ import { WINDOW } from './types' ;
31
30
32
31
/** Options for Request Instrumentation */
33
32
export interface RequestInstrumentationOptions {
34
33
/**
35
34
* List of strings and/or Regular Expressions used to determine which outgoing requests will have `sentry-trace` and `baggage`
36
35
* headers attached.
37
36
*
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'`.
39
54
*/
40
- tracePropagationTargets : Array < string | RegExp > ;
55
+ tracePropagationTargets ? : Array < string | RegExp > ;
41
56
42
57
/**
43
58
* Flag to disable patching all together for fetch requests.
@@ -73,7 +88,6 @@ export const defaultRequestInstrumentationOptions: RequestInstrumentationOptions
73
88
traceFetch : true ,
74
89
traceXHR : true ,
75
90
enableHTTPTimings : true ,
76
- tracePropagationTargets : DEFAULT_TRACE_PROPAGATION_TARGETS ,
77
91
} ;
78
92
79
93
/** Registers span creators for xhr and fetch requests */
@@ -208,10 +222,23 @@ function resourceTimingEntryToSpanData(resourceTiming: PerformanceResourceTiming
208
222
/**
209
223
* A function that determines whether to attach tracing headers to a request.
210
224
* 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.
212
226
*/
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
+ ) ;
215
242
}
216
243
217
244
/**
0 commit comments