@@ -7,8 +7,12 @@ import { addBreadcrumb, getBreadcrumbLogLevelFromHttpStatusCode, getSanitizedUrl
7
7
import { shouldPropagateTraceForUrl } from '@sentry/opentelemetry' ;
8
8
import * as diagch from 'diagnostics_channel' ;
9
9
import { NODE_MAJOR , NODE_MINOR } from '../../nodeVersion' ;
10
+ import { mergeBaggageHeaders } from '../../utils/baggage' ;
10
11
import type { UndiciRequest , UndiciResponse } from './types' ;
11
12
13
+ const SENTRY_TRACE_HEADER = 'sentry-trace' ;
14
+ const SENTRY_BAGGAGE_HEADER = 'baggage' ;
15
+
12
16
export type SentryNodeFetchInstrumentationOptions = InstrumentationConfig & {
13
17
/**
14
18
* Whether breadcrumbs should be recorded for requests.
@@ -18,8 +22,7 @@ export type SentryNodeFetchInstrumentationOptions = InstrumentationConfig & {
18
22
breadcrumbs ?: boolean ;
19
23
20
24
/**
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`.
23
26
* The same option can be passed to the top-level httpIntegration where it controls both, breadcrumb and
24
27
* span creation.
25
28
*
@@ -127,26 +130,47 @@ export class SentryNodeFetchInstrumentation extends InstrumentationBase<SentryNo
127
130
return ;
128
131
}
129
132
133
+ const { 'sentry-trace' : sentryTrace , baggage } = addedHeaders ;
134
+
130
135
// We do not want to overwrite existing headers here
131
136
// If the core UndiciInstrumentation is registered, it will already have set the headers
132
137
// We do not want to add any then
133
138
if ( Array . isArray ( request . headers ) ) {
134
139
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
+ }
141
157
} else {
142
158
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
+ }
150
174
}
151
175
}
152
176
0 commit comments