@@ -7,8 +7,12 @@ import { addBreadcrumb, getBreadcrumbLogLevelFromHttpStatusCode, getSanitizedUrl
77import { shouldPropagateTraceForUrl } from '@sentry/opentelemetry' ;
88import * as diagch from 'diagnostics_channel' ;
99import { NODE_MAJOR , NODE_MINOR } from '../../nodeVersion' ;
10+ import { mergeBaggageHeaders } from '../../utils/baggage' ;
1011import type { UndiciRequest , UndiciResponse } from './types' ;
1112
13+ const SENTRY_TRACE_HEADER = 'sentry-trace' ;
14+ const SENTRY_BAGGAGE_HEADER = 'baggage' ;
15+
1216export type SentryNodeFetchInstrumentationOptions = InstrumentationConfig & {
1317 /**
1418 * Whether breadcrumbs should be recorded for requests.
@@ -18,8 +22,7 @@ export type SentryNodeFetchInstrumentationOptions = InstrumentationConfig & {
1822 breadcrumbs ?: boolean ;
1923
2024 /**
21- * Do not capture breadcrumbs for outgoing fetch requests to URLs where the given callback returns `true`.
22- * For the scope of this instrumentation, this callback only controls breadcrumb creation.
25+ * Do not capture breadcrumbs or inject headers for outgoing fetch requests to URLs where the given callback returns `true`.
2326 * The same option can be passed to the top-level httpIntegration where it controls both, breadcrumb and
2427 * span creation.
2528 *
@@ -127,26 +130,47 @@ export class SentryNodeFetchInstrumentation extends InstrumentationBase<SentryNo
127130 return ;
128131 }
129132
133+ const { 'sentry-trace' : sentryTrace , baggage } = addedHeaders ;
134+
130135 // We do not want to overwrite existing headers here
131136 // If the core UndiciInstrumentation is registered, it will already have set the headers
132137 // We do not want to add any then
133138 if ( Array . isArray ( request . headers ) ) {
134139 const requestHeaders = request . headers ;
135- Object . entries ( addedHeaders )
136- . filter ( ( [ k ] ) => {
137- // If the header already exists, we do not want to set it again
138- return ! requestHeaders . includes ( k ) ;
139- } )
140- . forEach ( keyValuePair => requestHeaders . push ( ...keyValuePair ) ) ;
140+
141+ // We do not want to overwrite existing header here, if it was already set
142+ if ( sentryTrace && ! requestHeaders . includes ( SENTRY_TRACE_HEADER ) ) {
143+ requestHeaders . push ( SENTRY_TRACE_HEADER , sentryTrace ) ;
144+ }
145+
146+ // For baggage, we make sure to merge this into a possibly existing header
147+ const existingBaggagePos = requestHeaders . findIndex ( header => header === SENTRY_BAGGAGE_HEADER ) ;
148+ if ( baggage && existingBaggagePos === - 1 ) {
149+ requestHeaders . push ( SENTRY_BAGGAGE_HEADER , baggage ) ;
150+ } else if ( baggage ) {
151+ const existingBaggage = requestHeaders [ existingBaggagePos + 1 ] ;
152+ const merged = mergeBaggageHeaders ( existingBaggage , baggage ) ;
153+ if ( merged ) {
154+ requestHeaders [ existingBaggagePos + 1 ] = merged ;
155+ }
156+ }
141157 } else {
142158 const requestHeaders = request . headers ;
143- request . headers += Object . entries ( addedHeaders )
144- . filter ( ( [ k ] ) => {
145- // If the header already exists, we do not want to set it again
146- return ! requestHeaders . includes ( `${ k } :` ) ;
147- } )
148- . map ( ( [ k , v ] ) => `${ k } : ${ v } \r\n` )
149- . join ( '' ) ;
159+ // We do not want to overwrite existing header here, if it was already set
160+ if ( sentryTrace && ! requestHeaders . includes ( `${ SENTRY_TRACE_HEADER } :` ) ) {
161+ request . headers += `${ SENTRY_TRACE_HEADER } : ${ sentryTrace } \r\n` ;
162+ }
163+
164+ // For baggage, we make sure to merge this into a possibly existing header
165+ const existingBaggage = request . headers . match ( / b a g g a g e : ( .* ) \r \n / ) ?. [ 1 ] ;
166+ if ( baggage && ! existingBaggage ) {
167+ request . headers += `${ SENTRY_BAGGAGE_HEADER } : ${ baggage } \r\n` ;
168+ } else if ( baggage ) {
169+ const merged = mergeBaggageHeaders ( existingBaggage , baggage ) ;
170+ if ( merged ) {
171+ request . headers = request . headers . replace ( / b a g g a g e : ( .* ) \r \n / , `baggage: ${ merged } \r\n` ) ;
172+ }
173+ }
150174 }
151175 }
152176
0 commit comments