@@ -4,6 +4,7 @@ import { Event, EventProcessor, Integration, Severity, Span, SpanContext, Transa
44import {
55 addInstrumentationHandler ,
66 getGlobalObject ,
7+ isInstanceOf ,
78 isMatchingPattern ,
89 logger ,
910 safeJoin ,
@@ -1019,18 +1020,24 @@ function fetchCallback(handlerData: { [key: string]: any }): void {
10191020 if ( activity ) {
10201021 const span = activity . span ;
10211022 if ( span ) {
1023+ const request = ( handlerData . args [ 0 ] = handlerData . args [ 0 ] as string | Request ) ;
10221024 const options = ( handlerData . args [ 1 ] = ( handlerData . args [ 1 ] as { [ key : string ] : any } ) || { } ) ;
1023- if ( options . headers ) {
1024- if ( typeof options . headers . append === 'function' ) {
1025- options . headers . append ( 'sentry-trace' , span . toTraceparent ( ) ) ;
1026- } else if ( Array . isArray ( options . headers ) ) {
1027- options . headers = [ ...options . headers , [ 'sentry-trace' , span . toTraceparent ( ) ] ] ;
1025+ let headers = options . headers ;
1026+ if ( isInstanceOf ( request , Request ) ) {
1027+ headers = ( request as Request ) . headers ;
1028+ }
1029+ if ( headers ) {
1030+ if ( typeof headers . append === 'function' ) {
1031+ headers . append ( 'sentry-trace' , span . toTraceparent ( ) ) ;
1032+ } else if ( Array . isArray ( headers ) ) {
1033+ headers = [ ...headers , [ 'sentry-trace' , span . toTraceparent ( ) ] ] ;
10281034 } else {
1029- options . headers = { ... options . headers , 'sentry-trace' : span . toTraceparent ( ) } ;
1035+ headers = { ...headers , 'sentry-trace' : span . toTraceparent ( ) } ;
10301036 }
10311037 } else {
1032- options . headers = { 'sentry-trace' : span . toTraceparent ( ) } ;
1038+ headers = { 'sentry-trace' : span . toTraceparent ( ) } ;
10331039 }
1040+ options . headers = headers ;
10341041 }
10351042 }
10361043 }
0 commit comments