@@ -166,14 +166,12 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
166166 }
167167
168168 /**
169- * For each header in the request, call the callback. Skips likely-invalid
170- * headers. Multi-valued headers are passed through. The loop exits early if
171- * the callback returns true.
169+ * Yield an object { key, value } for each header in the request. Skips
170+ * likely-invalid headers. Multi-valued headers are passed through.
172171 */
173- private forEachRequestHeader (
174- request : UndiciRequest ,
175- callback : ( key : string , value : string | string [ ] ) => boolean | undefined
176- ) : void {
172+ private * requestHeaders (
173+ request : UndiciRequest
174+ ) : Generator < { key : string ; value : string } , never , never > {
177175 if ( Array . isArray ( request . headers ) ) {
178176 // headers are an array [k1, v2, k2, v2] (undici v6+)
179177 for ( let i = 0 ; i < request . headers . length ; i += 2 ) {
@@ -182,9 +180,7 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
182180 // Shouldn't happen, but the types don't know that, and let's be safe
183181 continue ;
184182 }
185- if ( callback ( key , request . headers [ i + 1 ] ) ) {
186- break ;
187- }
183+ yield { key, value : request . headers [ i + 1 ] } ;
188184 }
189185 } else if ( typeof request . headers === 'string' ) {
190186 // headers are a raw string (undici v5)
@@ -199,11 +195,8 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
199195 continue ;
200196 }
201197 const key = line . substring ( 0 , colonIndex ) ;
202- const value = line . substring ( 0 , colonIndex + 1 ) ;
203-
204- if ( callback ( key , value ) ) {
205- break ;
206- }
198+ const value = line . substring ( colonIndex + 1 ) . trim ( ) ;
199+ yield { key, value } ;
207200 }
208201 }
209202 }
@@ -261,16 +254,15 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
261254 }
262255
263256 // Get user agent from headers
264- this . forEachRequestHeader ( request , ( key , value ) => {
257+ for ( const { key, value } of this . requestHeaders ( request ) ) {
265258 if ( key . toLowerCase ( ) === 'user-agent' ) {
266259 // user-agent should only appear once per the spec, but the library doesn't
267260 // prevent passing it multiple times, so we handle that to be safe.
268261 const userAgent = Array . isArray ( value ) ? value [ 0 ] : value ;
269262 attributes [ SemanticAttributes . USER_AGENT_ORIGINAL ] = userAgent ;
270- return true ; // no need to keep iterating
263+ break ;
271264 }
272- return false ;
273- } ) ;
265+ }
274266
275267 // Get attributes from the hook if present
276268 const hookAttributes = safeExecuteInTheMiddle (
@@ -363,15 +355,14 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
363355 config . headersToSpanAttributes . requestHeaders . map ( n => n . toLowerCase ( ) )
364356 ) ;
365357
366- this . forEachRequestHeader ( request , ( key , value ) => {
358+ for ( const { key, value } of this . requestHeaders ( request ) ) {
367359 const name = key . toLowerCase ( ) ;
368360 if ( headersToAttribs . has ( name ) ) {
369361 spanAttributes [ `http.request.header.${ name } ` ] = value
370362 . toString ( )
371363 . trim ( ) ;
372364 }
373- return false ; // keep iterating always, there may be more
374- } ) ;
365+ }
375366 }
376367
377368 span . setAttributes ( spanAttributes ) ;
0 commit comments