How to trace a request through an SQS queue with AWS X-Ray

Justin Bell :

I'm trying to get a toy example up and running with an AWS Lambda function, f, that's triggered by a message on one SQS queue, sqs, publishes to another queue sqs', and then a worker, f' reads from sqs' and processes the message where the entire "request" is traced with X-Ray.

sqs -> f -> sqs' -> f'

Currently, I have the queues in place and the functions writing and receiving from the queue. I also have X-Ray tracing the request from the the first function f to the sqs queue.

My current challenge is: how do I propagate the trace to the final worker so I can see the entire process in x-ray.


Here are my current functions:

public class Hello implements RequestHandler<SQSEvent, Void> {
    String OUTPUT_QUEUE_URL = "...";

    private AmazonSQS sqs = AmazonSQSClientBuilder.standard()
        .withRequestHandlers(new TracingHandler(AWSXRay.getGlobalRecorder()))
        .build();

    public Void handleRequest(SQSEvent event, Context context)
    {
        for(SQSMessage msg : event.getRecords()){
            System.out.println(new String(msg.getBody()));
        }

        SendMessageRequest send_msg_request = new SendMessageRequest()
            .withQueueUrl(OUTPUT_QUEUE_URL)
            .withMessageBody("hello world")
            .withDelaySeconds(5);
        sqs.sendMessage(send_msg_request);
        return null;
    }
}

public class World implements RequestHandler<SQSEvent, Void>{
    public Void handleRequest(SQSEvent event, Context context)
    {
        for(SQSMessage msg : event.getRecords()){
            System.out.println(new String(msg.getBody()));
        }
        return null;
    }
}
Mathew Werber :

As of today, AWS X-Ray now has native support for Amazon SQS: https://aws.amazon.com/about-aws/whats-new/2019/08/aws-x-ray-now-supports-amazon-sqs/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=35989&siteId=1