- Newest
- Most votes
- Most comments
Below are some important points to consider:
-
SNS and SQS support: Both SNS and SQS support trace context propagation, which means they can pass the tracing header to downstream services.
-
Linked traces: In your original setup (ECS -> SQS -> Lambda), you saw linked traces because both ECS and Lambda emit segment data. In the new setup (ECS -> SNS -> SQS -> Lambda), the introduction of SNS and SQS in the middle might be breaking the direct linkage in the trace map.
-
Trace continuity: Despite not appearing in the trace map, the trace context is still being propagated through SNS and SQS. This means that if you examine the individual traces, you should still see the correct trace IDs being passed along. However to ensure the propagation of trace IDs:
a. When publishing to SNS, include the trace header in both the MessageAttributes and the Message body:
const publishMessageTopic = async () => {
const sampled = segment.notTraced ? '0' : '1';
const traceHeader = `Root=${segment.trace_id};Parent=${segment.id};Sampled=${sampled}`;
const messageBody = JSON.stringify({
...body,
AWSTraceHeader: traceHeader
});
await snsClient.send(
new PublishCommand({
TopicArn: config.scanResultTopic,
Message: messageBody,
MessageAttributes: {
AWSTraceHeader: {
DataType: "String",
StringValue: traceHeader
}
}
})
);
};
b. Set up a subscription filter policy on your SNS to SQS subscription to include all messages:
{
"AWSTraceHeader": [{"exists": true}]
}
c. In your Lambda function, extract the trace header from the message body and set it manually:
const AWS = require('aws-sdk');
const AWSXRay = require('aws-xray-sdk-core');
exports.handler = async (event) => {
for (const record of event.Records) {
const body = JSON.parse(record.body);
if (body.AWSTraceHeader) {
AWSXRay.setSegment(new AWSXRay.Segment('lambda', body.AWSTraceHeader));
}
// Process the message...
}
};
Try following the above updated code.
[+] https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces
As a workaround, you can also use Step Functions: If possible, consider using AWS Step Functions to orchestrate your workflow. Step Functions maintains trace context across services, including SNS and SQS.
To address your specific question about the Stack Overflow post: There have been multiple updates with SNS supporting XRay now and can be referred at:
To improve your tracing visibility:
- Ensure that active tracing is enabled for your SNS topic.
- Check that your IAM permissions are correctly set up to allow SNS to send trace data to X-Ray.
- When analyzing traces, you may need to manually correlate the trace IDs between your ECS and Lambda traces, as they won't be visually linked in the X-Ray console.
Additionally please check the below resources which will help achieve the use-case:
[+] https://serverlessland.com/patterns/sns-sqs-lambda-xray [+] https://serverlessland.com/patterns/sns-sqs-lambda-xray
While this situation isn't ideal for visualization, rest assured that the trace context is still being propagated through your entire system. The limitation is in how X-Ray represents these traces in its console, not in the actual tracing mechanism itself.
Sources
Amazon S3 and AWS X-Ray - AWS X-Ray
AWS Lambda and AWS X-Ray - AWS X-Ray
Amazon SNS and AWS X-Ray - AWS X-Ray
but i don't see the lambda part in my x-ray graph, i see only client -> ECS service, but i'm expecting to see client -> ECS service -> myLmabdaFunction->..
Relevant content
- asked 4 years ago
- asked 3 years ago
- asked 3 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago

You probably need to enable active tracing on your SNS topic and also make sure your SNS client is instrumented. Please see this doc for details: https://docs.aws.amazon.com/xray/latest/devguide/xray-services-sns.html