@@ -6,13 +6,15 @@ import {
66 diag ,
77 Context as OtelContext ,
88 trace ,
9+ context ,
910 propagation ,
1011 Span ,
1112 Tracer ,
1213 AttributeValue ,
1314 TextMapSetter ,
14- defaultTextMapSetter ,
1515 ROOT_CONTEXT ,
16+ INVALID_SPAN_CONTEXT ,
17+ ContextAPI ,
1618} from '@opentelemetry/api' ;
1719import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node' ;
1820import { Instrumentation } from '@opentelemetry/instrumentation' ;
@@ -21,7 +23,12 @@ import { AwsLambdaInstrumentation, AwsLambdaInstrumentationConfig } from '@opent
2123import { expect } from 'expect' ;
2224import { AWS_ATTRIBUTE_KEYS } from '../../src/aws-attribute-keys' ;
2325import { RequestMetadata , ServiceExtension } from '../../src/third-party/otel/aws/services/ServiceExtension' ;
24- import { applyInstrumentationPatches , customExtractor , headerGetter } from './../../src/patches/instrumentation-patch' ;
26+ import {
27+ applyInstrumentationPatches ,
28+ AWSXRAY_TRACE_ID_HEADER_CAPITALIZED ,
29+ customExtractor ,
30+ headerGetter ,
31+ } from './../../src/patches/instrumentation-patch' ;
2532import * as sinon from 'sinon' ;
2633import { AWSXRAY_TRACE_ID_HEADER , AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray' ;
2734import { Context } from 'aws-lambda' ;
@@ -560,12 +567,16 @@ describe('InstrumentationPatchTest', () => {
560567 } ,
561568 } ;
562569
570+ afterEach ( ( ) => {
571+ sinon . restore ( ) ;
572+ } ) ;
573+
563574 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
564575 // @ts -ignore
565576 await send ( { } , null ) ;
566577 } ) ;
567578
568- it ( 'propagator injects with valid context ' , async ( ) => {
579+ it ( 'Updates trace header casing when AWSXRayPropagator injects trace header successfully ' , async ( ) => {
569580 sinon
570581 . stub ( AWSXRayPropagator . prototype , 'inject' )
571582 . callsFake ( ( context : OtelContext , carrier : unknown , setter : TextMapSetter ) => {
@@ -574,31 +585,29 @@ describe('InstrumentationPatchTest', () => {
574585 } ) ;
575586 await mockedMiddlewareStackInternal [ 0 ] [ 0 ] ( ( arg : any ) => Promise . resolve ( ) , null ) ( middlewareArgsHeader ) ;
576587
577- sinon . restore ( ) ;
578-
579588 expect ( middlewareArgsHeader . request . headers [ 'isCarrierModified' ] ) . toEqual ( 'carrierIsModified' ) ;
580589 expect ( middlewareArgsHeader . request . headers ) . not . toHaveProperty ( AWSXRAY_TRACE_ID_HEADER ) ;
581- expect ( middlewareArgsHeader . request . headers ) . toHaveProperty ( 'X-Amzn-Trace-Id' ) ;
582- expect ( middlewareArgsHeader . request . headers [ 'X-Amzn-Trace-Id' ] ) . toEqual ( testXrayTraceHeader ) ;
590+ expect ( middlewareArgsHeader . request . headers ) . toHaveProperty ( AWSXRAY_TRACE_ID_HEADER_CAPITALIZED ) ;
591+ expect ( middlewareArgsHeader . request . headers [ AWSXRAY_TRACE_ID_HEADER_CAPITALIZED ] ) . toEqual ( testXrayTraceHeader ) ;
583592
584593 expect ( mockedMiddlewareStackInternal [ 0 ] [ 1 ] . name ) . toEqual ( '_adotInjectXrayContextMiddleware' ) ;
585594 } ) ;
586595
587- it ( 'propagator does not inject with invalid context' , async ( ) => {
588- const invalidContext = trace . setSpanContext ( ROOT_CONTEXT , {
589- traceId : 'invalid-trace-id' ,
590- spanId : 'invalid-span' ,
591- traceFlags : 0 ,
592- isRemote : false ,
593- } ) ;
596+ it ( 'Does not set trace header when AWSXRayPropagator does not inject trace header' , async ( ) => {
597+ const invalidContext : OtelContext = {
598+ getValue : ( key : symbol ) => ( {
599+ spanContext : ( ) => INVALID_SPAN_CONTEXT ,
600+ } ) ,
601+ setValue : ( key : symbol , value : unknown ) => invalidContext ,
602+ deleteValue : ( key : symbol ) => invalidContext ,
603+ } ;
594604
595- const propagator = new AWSXRayPropagator ( ) ;
605+ sinon . stub ( context , 'active' ) . returns ( invalidContext ) ;
596606
597- propagator . inject ( invalidContext , middlewareArgsHeader . request . headers , defaultTextMapSetter ) ;
598- sinon . restore ( ) ;
607+ await mockedMiddlewareStackInternal [ 0 ] [ 0 ] ( ( arg : any ) => Promise . resolve ( ) , null ) ( middlewareArgsHeader ) ;
599608
600609 expect ( middlewareArgsHeader . request . headers ) . not . toHaveProperty ( AWSXRAY_TRACE_ID_HEADER ) ;
601- expect ( middlewareArgsHeader . request . headers ) . not . toHaveProperty ( 'X-Amzn-Trace-Id' ) ;
610+ expect ( middlewareArgsHeader . request . headers ) . not . toHaveProperty ( AWSXRAY_TRACE_ID_HEADER_CAPITALIZED ) ;
602611
603612 expect ( mockedMiddlewareStackInternal [ 0 ] [ 1 ] . name ) . toEqual ( '_adotInjectXrayContextMiddleware' ) ;
604613 } ) ;
0 commit comments