11import { EventProcessor , Hub , Integration , Span , SpanContext , SpanStatus } from '@sentry/types' ;
2- import {
3- addInstrumentationHandler ,
4- fill ,
5- getGlobalObject ,
6- isMatchingPattern ,
7- logger ,
8- supportsNativeFetch ,
9- } from '@sentry/utils' ;
2+ import { addInstrumentationHandler , getGlobalObject , isMatchingPattern , logger } from '@sentry/utils' ;
103
114/**
125 * Options for Tracing integration
@@ -163,7 +156,6 @@ export class Tracing implements Integration {
163156 callback : fetchCallback ,
164157 type : 'fetch' ,
165158 } ) ;
166- this . _traceFetch ( getCurrentHub ) ;
167159 }
168160
169161 // tslint:disable-next-line: no-non-null-assertion
@@ -183,61 +175,6 @@ export class Tracing implements Integration {
183175 }
184176 }
185177
186- /**
187- * JSDoc
188- */
189- private _traceFetch ( getCurrentHub : ( ) => Hub ) : void {
190- if ( ! supportsNativeFetch ( ) ) {
191- return ;
192- }
193-
194- // tslint:disable: only-arrow-functions
195- fill ( getGlobalObject < Window > ( ) , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
196- return function ( ...args : any [ ] ) : void {
197- // @ts -ignore
198- const hub = getCurrentHub ( ) ;
199- const self = hub . getIntegration ( Tracing ) ;
200- // tslint:disable-next-line: no-non-null-assertion
201- if ( self && self . _options ! . tracingOrigins ) {
202- const url = args [ 0 ] as string ;
203- const options = ( args [ 1 ] = ( args [ 1 ] as { [ key : string ] : any } ) || { } ) ;
204-
205- let isWhitelisted = false ;
206- // tslint:disable-next-line: no-non-null-assertion
207- self . _options ! . tracingOrigins . forEach ( ( whiteListUrl : string | RegExp ) => {
208- if ( ! isWhitelisted ) {
209- isWhitelisted = isMatchingPattern ( url , whiteListUrl ) ;
210- }
211- } ) ;
212-
213- if ( isMatchingPattern ( url , 'sentry_key' ) ) {
214- // If sentry_key is in the url, it's an internal store request to sentry
215- // we do not want to add the trace header to store requests
216- isWhitelisted = false ;
217- }
218-
219- if ( isWhitelisted ) {
220- if ( options . headers ) {
221- if ( Array . isArray ( options . headers ) ) {
222- options . headers = [ ...options . headers , ...Object . entries ( hub . traceHeaders ( ) ) ] ;
223- } else {
224- options . headers = {
225- ...options . headers ,
226- ...hub . traceHeaders ( ) ,
227- } ;
228- }
229- } else {
230- options . headers = hub . traceHeaders ( ) ;
231- }
232- }
233- }
234- // tslint:disable-next-line: no-unsafe-any
235- return originalFetch . apply ( getGlobalObject < Window > ( ) , args ) ;
236- } ;
237- } ) ;
238- // tslint:enable: only-arrow-functions
239- }
240-
241178 /**
242179 * Is tracing enabled
243180 */
@@ -480,11 +417,27 @@ function fetchCallback(handlerData: { [key: string]: any }): void {
480417 description : `${ handlerData . fetchData . method } ${ handlerData . fetchData . url } ` ,
481418 op : 'http' ,
482419 } ) ;
483- }
484420
485- // if (handlerData.error) {
486- // } else {
487- // }
421+ const activity = Tracing . _activities [ handlerData . fetchData . __activity ] ;
422+ if ( activity ) {
423+ const span = activity . span ;
424+ if ( span ) {
425+ const options = ( handlerData . args [ 1 ] = ( handlerData . args [ 1 ] as { [ key : string ] : any } ) || { } ) ;
426+ if ( options . headers ) {
427+ if ( Array . isArray ( options . headers ) ) {
428+ options . headers = [ ...options . headers , { 'sentry-trace' : span . toTraceparent ( ) } ] ;
429+ } else {
430+ options . headers = {
431+ ...options . headers ,
432+ 'sentry-trace' : span . toTraceparent ( ) ,
433+ } ;
434+ }
435+ } else {
436+ options . headers = { 'sentry-trace' : span . toTraceparent ( ) } ;
437+ }
438+ }
439+ }
440+ }
488441 // tslint:enable: no-unsafe-any
489442}
490443
0 commit comments