Simple AWS: Using X-Ray for Observability in Event-Driven Architectures
How to Use AWS X-Ray to Monitor and Trace an Event-Driven Architecture on AWS

Guille Ojeda
December 22, 2022
Welcome to Simple AWS! A free newsletter that helps you build on AWS without being an expert. This is issue #6. Shall we?
Observability in Event-Driven Architectures Using AWS X-Ray
AWS Service: AWS X-Ray
tl;dr: An event-driven architecture is one where, instead of a service calling another one directly, it emits an event to which other services are subscribed. That's great for decoupling, but it makes it much more difficult to understand what's going on. AWS X-Ray gets data from every part of your architecture, aggregates it into graphs and shows you the big picture of what's happening.
Without X-Ray:
- You add logs to your Lambda functions. Then you add more logs just in case.
- When you have an issue, you open 10 tabs of CloudWatch Logs and try to figure out which log entries are related, based on timestamps and intuition.
- You finally figure it out (days later), and get a detective badge for the investigative work.
With X-Ray:
- You add AWS X-Ray to all your Lambda functions, DynamoDB tables, SNS topics, etc.
- When you have an issue, the X-Ray console gives you an overview of the whole system, and you can zoom in on a specific event and trace it all the way through your app.
- You don't get the detective badge, because figuring this out was actually pretty easy.

Actionable tips
I'll split the tips into two parts. The first one is how to set up X-Ray, which I'll do for a Node.js app. If you need help setting it up you can check the sample app, try the docs or send me an email to [email protected]
For Node.js, first install the AWS X-Ray SDK:
npm install aws-xray-sdk
Then structure your code like this:
// You'll always need this
const AWSXRay = require('aws-xray-sdk');
// This wraps the AWS SDK with X-Ray.
// Now you can use the AWS object to access the SDK for any other service
// like you usually do, but X-Ray will be monitoring it
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
// Same here, but for HTTP requests
AWSXRay.captureHTTPsGlobal(require('https'));
exports.handler = async (event) => {
// X-Ray gathers data from services in the form of segments
const segment = new AWSXRay.Segment('my-function-name');
try {
// Your awesome code goes here
} catch (error) {
console.error(error);
throw error;
} finally {
// Gotta close that segment!
segment.close();
}
};
That's the gist of it. Now X-Ray will collect data from your function. Remember to also set it up for DynamoDB tables and SNS topics by going to Advanced settings and enabling AWS X-Ray Tracing.
The second part is the tips and best practices:
- Enable X-Ray for all relevant components. Double check in case you forgot one.
- Use segments and subsegments to add context to trace data.
- Use custom attributes to add more context to trace data.
- Use captureFunc (captureAsyncFunc for async functions) to capture errors and add them to trace data.
- Enable sampling. You don't really need to trace all the requests, 10% is usually enough, and much cheaper.
Recommended tool
- The One Observability Workshop has a section about AWS X-Ray that you'll want to check out. It doesn't say much about setting X-Ray up (I've got you covered on that!), but it does showcase its uses once configured.
- The EKS Workshop has a section on using AWS X-Ray for monitoring.
- And if you need to do this outside AWS, Jaeger is an open-source observability tool.
Haiku(s)
Use X-Ray to trace
Events that go through your app
And debug issues.
X-Ray traces things
In event-driven systems
Built on serverless.
Monolith? Easy.
Distributed? Much harder.
Except with X-Ray.
Misc.
Here come the holidays! A time for joy, a time for friends and family, and a time for resting. I'll be keeping up with Simple AWS, so you'll still receive issues on Monday and Thursday. If you read them, I hope you enjoy them. But honestly, I hope you don't find the time to read them, because you're too busy celebrating the joys of life. That's my wish for you all.
Have a fantastic Christmas!
Thank you for reading! See ya on the next issue.