How to create single xray trace for WebAPI Requests to SQS message to Lambda function

379 views Asked by At

I would like to create a single trace such that a trace from a web api request which publishes an SQS message is connected to a trace which is created from a lambda function invocation which is from a trigger on that sqs. For example, client --> webapi --> sqs --> lambda

Is this possible? Any help is appreciated.

I realize that creating trace from client --> webapi is the easy part. I am making the assumption that this is possible, but perhaps it is not.

This is perhaps an advanced use case. Is is possible and necessary for me to manually stitch the segments together somehow within the lambda?

Here is what it looks like w/ contexts separated (sqs message is terminal node) enter image description here

3

There are 3 answers

0
Judy007 On

Currently, xray segments in lambda are immutable and therefore its not possible to connect the different segments together. However, the AWS team is considering this feature as described here https://github.com/aws/aws-xray-sdk-node/issues/208

0
saart On

Disclosure: I work for Lumigo, a company that does exactly that.

You can consider using 3rd parties that provide a more robust solution than X-ray, and specifically handles this type of connection.

To To answer your question, as a workaround, you can manually connect the flows via a special key (MessageId) that exists both in the response from the SQS and in the triggered event. Exact location changes by the API, but generally, it can be found under SendMessageResponse/SendMessageResult/MessageId and Records/MessageId.

0
Sam Martin On

If you're working with Python you can do it with aws-xray-lambda-segment-shim.
If you're working with NodeJS you can follow this guide on dev.to.
If you're working with .NET there are some examples on this GitHub issue.

Basically it involves creating a new AWS-Xray segment inside the Lambda Function and using the incoming TraceID from the SQS message. This will result in two Segments for your lambda invocation. One which Lambda itself creates, and one which you create to extend the existing trace. Whether that's acceptable or not for your use case is something you'll have to decide for yourself!