File tree 2 files changed +41
-1
lines changed
plugins/node/instrumentation-undici
2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -205,7 +205,9 @@ export class UndiciInstrumentation extends InstrumentationBase {
205
205
const idx = request . headers . findIndex (
206
206
h => h . toLowerCase ( ) === 'user-agent'
207
207
) ;
208
- userAgent = request . headers [ idx + 1 ] ;
208
+ if ( idx >= 0 ) {
209
+ userAgent = request . headers [ idx + 1 ] ;
210
+ }
209
211
} else if ( typeof request . headers === 'string' ) {
210
212
const headers = request . headers . split ( '\r\n' ) ;
211
213
const uaHeader = headers . find ( h =>
Original file line number Diff line number Diff line change @@ -768,5 +768,43 @@ describe('UndiciInstrumentation `undici` tests', function () {
768
768
} ,
769
769
} ) ;
770
770
} ) ;
771
+
772
+ it ( 'should not report an user-agent if it was not defined' , async function ( ) {
773
+ let spans = memoryExporter . getFinishedSpans ( ) ;
774
+ assert . strictEqual ( spans . length , 0 ) ;
775
+
776
+ // Do some requests
777
+ const headers = {
778
+ 'foo-client' : 'bar' ,
779
+ } ;
780
+
781
+ const queryRequestUrl = `${ protocol } ://${ hostname } :${ mockServer . port } /?query=test` ;
782
+ const queryResponse = await undici . request ( queryRequestUrl , { headers } ) ;
783
+ await consumeResponseBody ( queryResponse . body ) ;
784
+
785
+ assert . ok (
786
+ queryResponse . headers [ 'propagation-error' ] == null ,
787
+ 'propagation is set for instrumented requests'
788
+ ) ;
789
+
790
+ spans = memoryExporter . getFinishedSpans ( ) ;
791
+ const span = spans [ 0 ] ;
792
+ assert . ok ( span , 'a span is present' ) ;
793
+ assert . strictEqual ( spans . length , 1 ) ;
794
+ assertSpan ( span , {
795
+ hostname : 'localhost' ,
796
+ httpStatusCode : queryResponse . statusCode ,
797
+ httpMethod : 'GET' ,
798
+ path : '/' ,
799
+ query : '?query=test' ,
800
+ reqHeaders : headers ,
801
+ resHeaders : queryResponse . headers ,
802
+ } ) ;
803
+ assert . strictEqual (
804
+ span . attributes [ 'user_agent.original' ] ,
805
+ undefined ,
806
+ 'user-agent is undefined'
807
+ ) ;
808
+ } ) ;
771
809
} ) ;
772
810
} ) ;
You can’t perform that action at this time.
0 commit comments