@@ -10,25 +10,26 @@ export class SQSEventTraceExtractor implements EventTraceExtractor {
1010
1111 extract ( event : SQSEvent ) : SpanContextWrapper | null {
1212 try {
13- // Try to extract trace context from message attributes
14- const messageAttribute = event ?. Records ?. [ 0 ] ?. messageAttributes ?. _datadog ;
15- if ( messageAttribute ) {
16- let headers ;
17- if ( messageAttribute . stringValue !== undefined ) {
18- headers = JSON . parse ( messageAttribute . stringValue ) ;
19- } else if ( messageAttribute . binaryValue !== undefined && messageAttribute . dataType === "Binary" ) {
20- // Try decoding base64 values
21- const decodedValue = Buffer . from ( messageAttribute . binaryValue , "base64" ) . toString ( "ascii" ) ;
22- headers = JSON . parse ( decodedValue ) ;
13+ // First try to extract trace context from message attributes
14+ let headers = event ?. Records ?. [ 0 ] ?. messageAttributes ?. _datadog ?. stringValue ;
15+
16+ if ( ! headers ) {
17+ // Then try to get from binary value. This happens when SNS->SQS, but SNS has raw message delivery enabled.
18+ // In this case, SNS maps any messageAttributes to the SQS messageAttributes.
19+ // We can at least get trace context from SQS, but we won't be able to create the SNS inferred span.
20+ const encodedTraceContext = event ?. Records ?. [ 0 ] ?. messageAttributes ?. _datadog ?. binaryValue ;
21+ if ( encodedTraceContext ) {
22+ headers = Buffer . from ( encodedTraceContext , "base64" ) . toString ( "ascii" ) ;
2323 }
24+ }
2425
25- if ( headers ) {
26- const traceContext = extractTraceContext ( headers , this . tracerWrapper ) ;
27- if ( traceContext ) {
28- return traceContext ;
29- }
30- logDebug ( "Failed to extract trace context from SQS event" ) ;
26+ if ( headers ) {
27+ const parsedHeaders = JSON . parse ( headers ) ;
28+ const traceContext = extractTraceContext ( parsedHeaders , this . tracerWrapper ) ;
29+ if ( traceContext ) {
30+ return traceContext ;
3131 }
32+ logDebug ( "Failed to extract trace context from SQS event" ) ;
3233 }
3334
3435 // Else try to extract trace context from attributes.AWSTraceHeader
0 commit comments