Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
}
like image 611
Justin Bell Avatar asked Aug 21 '18 18:08

Justin Bell


People also ask

Can you query an SQS queue?

It is not possible to 'search' messages in an Amazon SQS queue. A ReceiveMessage() call will retrieve one or more messages, but there is no ability to control which messages will be returned.

How do I read messages from SQS queue?

When you request messages from a queue, you can't specify which message to retrieve. Instead, you specify the maximum number of messages (up to 10) that you want to retrieve. From the Queues page, select a queue. From Queue Actions, select Send and receive messages.

How do I access my AWS SQS queue?

To access an Amazon SQS queue, you must add permissions to the SQS access policy, the IAM policy, or both. The specific permissions requirements differ depending on whether the SQS queue and IAM role are from the same account.

How do I read a SQS message in Lambda?

To configure your function to read from Amazon SQS in the Lambda console, create an SQS trigger. Open the Functions page of the Lambda console. Choose the name of a function. Under Function overview, choose Add trigger.


2 Answers

Even though SQS supports X-Ray tracing now, it doesn't propagate the trace to a lambda function. That's why with SQS->Lambda setup, Lambda always starts a new trace. There is an issue for it in the nodejs xray SDK https://github.com/aws/aws-xray-sdk-node/issues/208

like image 178
Mahendra R Avatar answered Sep 21 '22 16:09

Mahendra R


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/

like image 27
Mathew Werber Avatar answered Sep 22 '22 16:09

Mathew Werber