You are on page 1of 7

How to Implement AWS X-Ray Tracing

Ryan Bui Follow


Jan 12 · 4 min read

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:

1 // AWS X-Ray is capturing all AWS calls


2 const AWSXRay = require("aws-xray-sdk-core");
3 const AWS = AWSXRay.captureAWS(require("aws-sdk"));
4 const db = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true });
5
6 // This function is just updating a DB table. Replace it with what you need.
7 module.exports = (data) => {
8 const tableName = data.tableName;
9
10 const params = {
11 TableName: `${process.env.STAGE}-${tableName}`,
12 Key: data.key,
13 // rest of params
14 };
15
16 return db.update(params).promise();
17 };

db.js hosted with ❤ by GitHub view raw

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:

1 // AWS X-Ray is capturing all AWS calls


2 const AWSXRay = require("aws-xray-sdk-core");
3 const cuid = require("cuid");
4 const updateActivity = require("./db");
5
6 module.exports.lambda = async (event, context) => {
7 /**
8 * set up X-ray traces
9 */
10 const correlationID = cuid();
11 const segment = AWSXRay.getSegment();
12 const subSegment = segment.addNewSubsegment("Correlation ID");
13 subSegment.addAnnotation("correlationID", correlationID);
14 subSegment.close();
15
16 // some dummy data
17 const userID = "123";
18 const activityID = "111";
19 const reminders = [
20 { day: "monday", value: "true" },
21 { day: "friday", value: "false" },
22 ];
23
24 // update dynamoDB table
25 const result = await Promise.allSettled([
26 updateActivity(
27 {
28 key: {
29 userID,
30 activityID,
31 },
32 userID,
33 tableName: "activities",
34 },
35 reminders
36 ),
37 ]);
38 };

index.js hosted with ❤ by GitHub view raw

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!
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!

AWS Lambda Tutorial Awsxray Bugs

About Help Legal

Get the Medium app

You might also like