DEV Community

Kay for AWS Community Builders

Posted on

X-Ray tracing from SQS to Lambda

SQS supports X-Ray tracing but it does not propagate the trace to Lambda function. Lambda always starts a new trace with an immutable facade segment.

This is a known issue, see:


There were some workaround discussions in the above issues.

The following is a solution I implemented for a demo:

  • Create a new segment to replace the facade segment created by Lambda, and
  • assign with the retrieved trace ID and parent ID from the trace header of the SQS segment.

Example in typescript:

import { Handler, SQSEvent, SQSRecord } from "aws-lambda"
import { Segment, setSegment, utils } from "aws-xray-sdk"

// Create a new Segment
const traceHeaderStr = sqsRecord.attributes.AWSTraceHeader
const traceData = utils.processTraceData(traceHeaderStr)
const sqsSegmentEndTime = Number(sqsRecord.attributes.ApproximateFirstReceiveTimestamp) / 1000

const lambdaSegment = new Segment(
lambdaSegment.origin = "AWS::Lambda::Function"
lambdaSegment.start_time = lambdaExecStartTime - (lambdaExecStartTime - sqsSegmentEndTime)
    function_arn: functionArn,
    region: sqsRecord.awsRegion,
    request_id: awsRequestId

// Set it as the current Segment

// Do something

// Close the segment
Enter fullscreen mode Exit fullscreen mode

From Trace Map console:

Alt Text

Example code can be found here on GitHub kyhau/aws-tools/b/X-Ray/xray-sqs-to-lambda/handler.ts.

Top comments (2)

alexey2baranov profile image

hope this work. i will try to use tomorrow.
btw: why don't you use new Date() for Lambda start Time. You can make it optional at least