@@ -85,6 +85,63 @@ describe("SQSEventTraceExtractor", () => {
8585 expect ( traceContext ?. source ) . toBe ( "event" ) ;
8686 } ) ;
8787
88+ it ( "extracts trace context from _datadog binaryValue when raw message delivery is used" , ( ) => {
89+ mockSpanContext = {
90+ toTraceId : ( ) => "1234567890" ,
91+ toSpanId : ( ) => "0987654321" ,
92+ _sampling : {
93+ priority : "1" ,
94+ } ,
95+ } ;
96+ const tracerWrapper = new TracerWrapper ( ) ;
97+
98+ const ddHeaders = {
99+ "x-datadog-trace-id" : "1234567890" ,
100+ "x-datadog-parent-id" : "0987654321" ,
101+ "x-datadog-sampled" : "1" ,
102+ "x-datadog-sampling-priority" : "1" ,
103+ } ;
104+ const ddHeadersString = JSON . stringify ( ddHeaders ) ;
105+ const ddHeadersBase64 = Buffer . from ( ddHeadersString , "ascii" ) . toString ( "base64" ) ;
106+
107+ const payload : SQSEvent = {
108+ Records : [
109+ {
110+ messageId : "abc-123" ,
111+ receiptHandle : "MessageReceiptHandle" ,
112+ body : "Hello from SQS!" ,
113+ attributes : {
114+ ApproximateReceiveCount : "1" ,
115+ SentTimestamp : "1523232000000" ,
116+ SenderId : "123456789012" ,
117+ ApproximateFirstReceiveTimestamp : "1523232000001" ,
118+ } ,
119+ messageAttributes : {
120+ _datadog : {
121+ binaryValue : ddHeadersBase64 ,
122+ dataType : "Binary" ,
123+ } ,
124+ } ,
125+ md5OfBody : "x" ,
126+ eventSource : "aws:sqs" ,
127+ eventSourceARN : "arn:aws:sqs:us-east-1:123456789012:MyQueue" ,
128+ awsRegion : "us-east-1" ,
129+ } ,
130+ ] ,
131+ } ;
132+
133+ const extractor = new SQSEventTraceExtractor ( tracerWrapper ) ;
134+ const traceContext = extractor . extract ( payload ) ;
135+ expect ( traceContext ) . not . toBeNull ( ) ;
136+
137+ expect ( spyTracerWrapper ) . toHaveBeenCalledWith ( ddHeaders ) ;
138+
139+ expect ( traceContext ?. toTraceId ( ) ) . toBe ( "1234567890" ) ;
140+ expect ( traceContext ?. toSpanId ( ) ) . toBe ( "0987654321" ) ;
141+ expect ( traceContext ?. sampleMode ( ) ) . toBe ( "1" ) ;
142+ expect ( traceContext ?. source ) . toBe ( "event" ) ;
143+ } ) ;
144+
88145 it . each ( [
89146 [ "Records" , { } ] ,
90147 [ "Records first entry" , { Records : [ ] } ] ,
0 commit comments