How to Implement AWS X-Ray Tracing

Ryan Bui
TribalScale
Published in
4 min readJan 12, 2021

So you have an app and you want to trace errors easily. Well, you’ve come to the right place. AWS X-Ray tracing is a user-friendly and detailed service that allows users to trace their lambda functions and generate statistics such as response time, annotations, and response codes.

Let’s see how we can use it.

Setting up X-Ray

Many aspects of this tutorial are taken from this tutorial created by AWS.
The first thing we need to do is actually install the package for x-ray:

npm i aws-xray-sdk-core

Now that we have the package, we can use it in our function we want to trace:

Here we are just capturing AWS using AWSXRay by doing…

const AWS = AWSXRay.captureAWS(require("aws-sdk"));

And that’s essentially it! We have our basic x-ray tracing set up.

Adding more advanced tracing

Now that we have our basic tracing set up we should add some more advanced tracing using correlation IDs. Correlation IDs allow us to trace an entire call that may go through several different lambda functions. This makes in-depth tracing much easier as we can essentially pull the thread using these correlation IDs.

We can generate correlation IDs using cuid. However we still need a way to display this correlationID in our X-Ray traces. In this situation, we can use segments.

Segments are sections in X-Ray traces that can contain information such as annotations and exceptions. By creating a segment and additionally a subsegment, we can add annotations to subsegments to store specific information. Below is an example of using subsegments to store our correlationID. This function is using our previous function (displayed above) to update a dynamoDB table:

Now when we execute our function we see can see our x-ray traces along with our annotation.

Our basic X-Ray traces will be presented in the Service Map and will look like this:

When we click on our Lambda function, the following is shown:

As you can see, these traces reveal the time it took to respond after the test, and will show how long each part takes to respond.

We can see the new correlationID subsegment, and if we click on it we can see some information:

With this correlationID as an annotation, we can now filter our traces by the correlationID, which can help us trace any specific call for errors.

Now that we have the correlationID that cuid generated, we can actually filter our traces using this annotation. By going to the service map and typing annotation.correlationID = "ckj075051000108l208o6epqh" in the search bar, we can filter all traces with that specific correlationID:

Summary

And that’s AWS X-Ray in a nutshell! We’ve learned how useful X-Ray traces can be for finding and solving bugs, and we’ve seen how we can set it up and implement more detailed traces using annotations and correlationIDs. We did this by:

  1. Setting up X-Ray tracing and capturing AWS using AWSXRay
  2. Creating correlationIDs using cuid
  3. Adding annotations to subsegments and setting that annotation to the correlationID

Special thanks to Serguey Arellano Martínez for co-authoring this article. Check out his tutorials on:
How to Configure AWS on Route 53 & How to Proxy an S3 Static Website

Happy hacking!

Have any questions about our development capabilities? Click here to chat with one of our experts.

Ryan is a Javascript Developer who has worked closely with TDD and BDD. He enjoys playing video games and guitar.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--