Professional Documents
Culture Documents
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.
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:
And that’s essentially it! We have our basic x-ray tracing set up.
We can generate correlation IDs using cuid . However we still need a way to display this
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:
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!