@@ -18,7 +18,9 @@ import {
18
18
context ,
19
19
propagation ,
20
20
Span as ISpan ,
21
- SpanKind , trace ,
21
+ SpanKind ,
22
+ trace ,
23
+ SpanOptions ,
22
24
} from '@opentelemetry/api' ;
23
25
import { NodeTracerProvider } from '@opentelemetry/node' ;
24
26
import {
@@ -39,7 +41,7 @@ import { DummyPropagation } from '../utils/DummyPropagation';
39
41
import { httpRequest } from '../utils/httpRequest' ;
40
42
import { ContextManager } from '@opentelemetry/api' ;
41
43
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks' ;
42
- import type { ClientRequest , IncomingMessage , ServerResponse } from 'http' ;
44
+ import type { ClientRequest , IncomingMessage , ServerResponse , RequestOptions } from 'http' ;
43
45
import { isWrapped } from '@opentelemetry/instrumentation' ;
44
46
import { getRPCMetadata , RPCType } from '@opentelemetry/core' ;
45
47
@@ -95,6 +97,13 @@ export const responseHookFunction = (
95
97
span . setAttribute ( 'custom response hook attribute' , 'response' ) ;
96
98
} ;
97
99
100
+ export const startSpanHookFunction = (
101
+ options : SpanOptions ,
102
+ request : IncomingMessage | RequestOptions
103
+ ) : void => {
104
+ options . attributes = Object . assign ( { } , options . attributes , { guid : request . headers ?. guid } ) ;
105
+ } ;
106
+
98
107
describe ( 'HttpInstrumentation' , ( ) => {
99
108
let contextManager : ContextManager ;
100
109
@@ -201,6 +210,7 @@ describe('HttpInstrumentation', () => {
201
210
applyCustomAttributesOnSpan : customAttributeFunction ,
202
211
requestHook : requestHookFunction ,
203
212
responseHook : responseHookFunction ,
213
+ startSpanHook : startSpanHookFunction ,
204
214
serverName,
205
215
} ) ;
206
216
instrumentation . enable ( ) ;
@@ -672,7 +682,8 @@ describe('HttpInstrumentation', () => {
672
682
673
683
it ( 'custom attributes should show up on client and server spans' , async ( ) => {
674
684
await httpRequest . get (
675
- `${ protocol } ://${ hostname } :${ serverPort } ${ pathname } `
685
+ `${ protocol } ://${ hostname } :${ serverPort } ${ pathname } ` ,
686
+ { headers : { guid : 'user_guid' } }
676
687
) ;
677
688
const spans = memoryExporter . getFinishedSpans ( ) ;
678
689
const [ incomingSpan , outgoingSpan ] = spans ;
@@ -685,6 +696,14 @@ describe('HttpInstrumentation', () => {
685
696
incomingSpan . attributes [ 'custom response hook attribute' ] ,
686
697
'response'
687
698
) ;
699
+ assert . strictEqual (
700
+ incomingSpan . attributes [ 'custom response hook attribute' ] ,
701
+ 'response'
702
+ ) ;
703
+ assert . strictEqual (
704
+ incomingSpan . attributes [ 'guid' ] ,
705
+ 'user_guid'
706
+ ) ;
688
707
assert . strictEqual (
689
708
incomingSpan . attributes [ 'span kind' ] ,
690
709
SpanKind . CLIENT
@@ -698,6 +717,10 @@ describe('HttpInstrumentation', () => {
698
717
outgoingSpan . attributes [ 'custom response hook attribute' ] ,
699
718
'response'
700
719
) ;
720
+ assert . strictEqual (
721
+ outgoingSpan . attributes [ 'guid' ] ,
722
+ 'user_guid'
723
+ ) ;
701
724
assert . strictEqual (
702
725
outgoingSpan . attributes [ 'span kind' ] ,
703
726
SpanKind . CLIENT
0 commit comments