A developer has a distributed application where a producer service running on Amazon ECS Fargate sends tasks to an Amazon SQS queue. A consumer service, also running on Amazon ECS Fargate, polls the queue and processes the tasks. Both services use the AWS SDK and are configured with the AWS X-Ray SDK, with active tracing enabled where applicable and the AWS SDK clients properly patched.
When viewing the traces in the AWS X-Ray console, the developer observes two disconnected traces: one for the producer service sending the message, and another separate trace for the consumer service processing the task. The end-to-end transaction is not correlated.
Which action should the developer take to resolve this issue and trace the request end-to-end?
- AEnable active tracing on the Amazon SQS queue configuration via the AWS Management Console to allow the queue to act as a segment coordinator.
- Modify the consumer service code to extract the AWSTraceHeader from the SQS message system attributes, and use it to construct and set the parent segment context in the AWS X-Ray SDK.Answer
- CIncrease the visibility timeout of the Amazon SQS queue to ensure that the consumer service has enough time to write trace segments to the AWS X-Ray daemon before the message returns to the queue.
- DConfigure the AWS X-Ray SDK on the consumer service using hardcoded AWS IAM credentials with xray:PutTraceSegments permissions in the application source code to authorize the segment creation.
Answer
Modify the consumer service code to extract the AWSTraceHeader from the SQS message system attributes, and use it to construct and set the parent segment context in the AWS X-Ray SDK.
The correct answer is to modify the consumer service code to extract the AWSTraceHeader from the SQS message system attributes, and use it to construct and set the parent segment context. When using a self-managed worker (such as on ECS Fargate or EC2) to process SQS messages, the SDK does not automatically extract the tracing header. The developer must manually parse the AWSTraceHeader system attribute and initialize the segment context with it, which links the consumer's trace to the producer's trace.
Step-by-Step Solution
Key Concept
Manual trace context propagation across asynchronous boundaries with non-Lambda consumers