How to Configure AWS X-Ray Application Tracing?
Last Updated :
09 Oct, 2024
AWS X-Ray is a service that helps a developer trace and analyze distributed applications in the cloud, when dealing with a significant level of complexity, in terms of how different components interrelate, especially since the inception of microservices and serverless architecture, knowledge is key. X-Ray provides the ability to diagram those interactions by pulling data from sources like Amazon EC2, AWS Lambda, and Amazon ECS. With the configuration of AWS X-Ray, a developer can monitor performance bottlenecks, pinpoint errors, and fix distributed systems.
This article covers the configuration of AWS X-Ray for application tracing, basic concepts, detailed step-by-step instructions, and examples of use.
Primary Terminologies
- Trace: A trace is the path that a single request follows as it travels through the components of an application. It is comprised of segments that detail the interactions of the request with various services.
- Segment: A trace contains one or more segments. A segment provides details about the work done by a service, such as the time spent, resources accessed, and any errors encountered.
- Subsegment: Subsegments are parts of a segment that dive deeper into specific operations, like database queries, API calls, or requests to other services. They offer a finer level of insight into application performance.
- Annotations: These are key-value pairs that can be added to segments to provide more context to the trace, such as user IDs or order numbers. They help when filtering and querying specific traces.
- Service Map: The service map is a graphical representation of your application, showing how different services interact and where errors or performance bottlenecks occur
X-Ray Traces
X-Ray Traces are a complete record of what happened to a single request as it has been handled by the various services that comprise an application. Each trace collects detailed information on all the segments involved with the request and these traces show how a request flows through an application.
Key Components of a Trace:
- Segments: A trace is composed of one or more segments in which:. Each segment corresponds to a unit of work within a service. For example, if a request hits your web service, a segment is created for the web server doing work on handling that request (API request, database query, etc.).
- Sub-segments: Segments can further be divided into sub-segments which detail the fine-grained operation, say one external API call or one database query of a segment. This helps in the exact locating of the performance bottleneck within any particular operation.
X-Ray Service Map
The X-Ray Service Map graphs your application for you using AWS X-Ray, in order for you to better visualize how different services interact. It aggregates the trace data of lots of requests into one view, so you get a big-picture view of what's going on for each of the services within your application.
Key components of a service map:
- Nodes: Each node or service represents something in your application, like API Gateway, Lambda, EC2, S3. The node includes key performance metrics like average latency, error rates, and quantity of requests served.
- Edges: The edges represent the lines between nodes, which indicate the communication between services. They express how the different components are communicating with respect to the flow of requests and may also give an indication of how much traffic is sent or if there are any errors in the communication.
- Error Markers: AWS X-Ray uses color or symbols to highlight nodes or edges on a trace that establishes errors. For example, red could indicate a high number of errors, and yellow for performance issues.
Traces
A trace in AWS X-Ray is essentially a representation of the complete journey that a request takes behind the scenes of an application's infrastructure. A trace gives utmost visibility into how a single request travels through different services and resources within your application. This becomes even more important in microservices and serverless architectures because one request crosses many services, and debugging becomes very hard to accomplish without proper visibility.
How X-Ray collects trace data
- Daemon Process: An X-Ray daemon or agent on the services collects the trace data and sends that to the X-Ray service. Applying this, the daemon runs on the EC2 instances, ECS tasks, or forms a part of Lambda functions.
- Trace Header Propagation: Each service in the chain of execution propagates an X-Ray trace header. This will ensure that all the components which have contributed to servicing a single request come into this trace.
- Sampling: Not every request needs or should be traced for reasons of performance and cost. AWS X-Ray uses sampling rules to determine the frequency of tracing requests. This will ensure that you will be able to get useful trace data without overwhelming your application and/or racking up large expenses.
Step-by-Step Process to Configure AWS X-Ray for Application Tracing
Step 1: Set Up IAM Permissions for X-Ray
First, you need to configure the appropriate IAM role and attach necessary policies to the services that will interact with X-Ray (e.g., Lambda, EC2, ECS). The following IAM policy grants permissions to write traces and telemetry data to X-Ray:
- Attach below IAM policy to your service role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords",
"xray:GetSamplingRules",
"xray:GetSamplingTargets"
],
"Resource": "*"
}
]
}
Step 2: Start and Run the AWS X-Ray Daemon
To collect trace data, you must install and run the X-Ray daemon. The daemon buffers trace data segments and sends them to the X-Ray API. Here’s how you can install it on an Amazon Linux instance:
curl https://s3.us-east-2.amazonaws.com/aws-xray-assets.us-east-2
/xray-daemon/aws-xray-daemon-3.x.rpm -o /home/ec2-user/xray.rpm
Now install xray
sudo yum install -y /home/ec2-user/xray.rpm
After the installation, you can start and enable the X-Ray daemon:
sudo systemctl start xray
sudo systemctl enable xray
Verify that the daemon is running using the following command:
sudo service xray status
Step 3: Install Node.js and npm
Install Node.js:
Install Node.js by using following commands
Install Node.js and npm
curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
Now Install node.js
sudo yum install -y nodejs
Verify installation:
After installation, verify that Node.js and npm were installed successfully by checking their versions:
node -v
npm -v
Step 4: Install AWS X-Ray SDK
Now that Node.js and npm are installed, you can install the AWS X-Ray SDK:
npm install aws-xray-sdk
Step 5: Set Up the Project
Create a directory for your Node.js project:
mkdir myapp
cd myapp
Initialize a Node.js project (this creates a package.json file):
npm init -y
Install Express and AWS X-Ray SDK
You need to install both express and aws-xray-sdk:
npm install express aws-xray-sdk aws-sdk
Step 6: Create app.js file
Here is the app.js file
const AWSXRay = require('aws-xray-sdk');
const express = require('express');
const app = express();
// Capture AWS SDK and HTTP calls
AWSXRay.captureAWS(require('aws-sdk'));
AWSXRay.captureHTTPsGlobal(require('http'));
app.use(AWSXRay.express.openSegment('MyApp')); // Start tracing
app.get('/', (req, res) => res.send('Hello World!'));
app.use(AWSXRay.express.closeSegment()); // End tracing
app.listen(3000, () => console.log('Server is running on port 3000'));
Step 7: Run the Application
Run the script using Node.js:
node app.js
Step 8: Access the Application
Open your browser and navigate to your EC2 instance’s public IP with port 3000:
http://<EC2-Instance-Public-IP>:3000
You should see the response of Hello World
Step 9: Monitor Traces in AWS X-Ray
Once the application is running and handling requests, you can monitor trace data in the AWS X-Ray console. The traces will show how each HTTP request interacts with the services in your app.
Navigate to X-Ray
View Traces
- In the AWS X-Ray Console, navigate to the "Traces" section.
- You should see a list of traces for your application. If you’ve been generating traffic to your application, traces should appear here.
With these steps, you can successfully run the script and start tracing with AWS X-Ray.
Conclusion
In conclusion, the above article walked through an end-to-end process of setting up AWS X-Ray for application tracing. We began by examining all of the most important concepts and terminologies underpinning AWS X-Ray and how it gets put to work monitoring application performance. The guide showed the process beginning with the installation of a daemon of X-Ray, setting up an AWS X-Ray SDK in a Node.js application, and ensuring good trace collection. It also gave troubleshooting hints related to port conflicts and missing traces. Such guidance will make implementation of distributed tracing much easier, giving you a deeper understanding of application performance and increasing your ability to diagnose and remediate performance problems. At the same time, it doesn't only monitor but optimizes the application in such a way that it becomes reliable to the users.
Similar Reads
How To Setup AWS Xray Tracing Setup Or Django Application ?
AWS X-Ray provides powerful tracing capabilities, allowing you to gain insights into your applicationâs performance, identify bottlenecks in your app, and troubleshoot issues effectively so that you can optimize your application performance.This article will take you on a step-by-step guide to set u
5 min read
How to Configure AWS Athena for Query Analysis?
AWS Athena is an interactive serverless query service. With this powerful tool, you can analyze data directly on Amazon S3 using standard SQL, it eliminates the need for complex ETL processes, allowing you to run SQL queries on data stored in S3 and gain insights without moving the data to another p
6 min read
How To Deploy Python Application In AWS?
In this article, we will explore how one as a Python developer can deploy the application by harnessing the capabilities of AWS. AWS, a leading cloud computing platform, offers a wide range of services to help developers build, deploy, and manage applications at scale EC2. It provides scalable compu
4 min read
6 Strategies for Migrating Applications to AWS Cloud
Migrating applications to the AWS Cloud can unlock significant benefits from enhanced scalability and cost savings to improved security and operational efficiency. However a successful migration requires a clear strategy to minimize risks and maximize results. Here we explore six key strategies for
11 min read
AWS Application Load Balancer Using Terraform
In contemporary cloud infrastructure setups, managing and distributing incoming traffic effectively across various instances is central to ensuring the high accessibility and scalability of applications. Among the bunch of services given by AWS (Amazon Web Services), Elastic Load Balancing (ELB) sta
10 min read
How to Configure AWS Lambda?
AWS Lambda is a responsive cloud service that examines the steps followed within any application and responds to them by compiling the codes that have been defined by the users, also known as functions. The service automatically computes and manages the resources across multiple availability zones a
4 min read
How To Deploy Node Js Application In AWS Lightsail ?
Lightsail can be defined as a simple, easy-to-use, and user-friendly service offered by Amazon Web Services (AWS). The main goal of Lightsail is to provide an easy way for individuals, startups, and small businesses to launch and manage virtual private servers (VPS) and other cloud services without
6 min read
Launching an Application on AWS Beanstalk
AWS Beanstalk is an application that makes your task of hosting your web application and you do not have to create or configure any kind of webservers by yourself. All the in fractures like virtual machines, load balancer, etc all these things are provided by Elastic beanstalk and also provide the O
2 min read
How To Integrate AWS Auto Scaling With Application Load Balancer?
On learning how to bring efficiency to your AWS infrastructure will enhance the workflow and cost management. In this article, we will guide you on integrating AWS Auto Scaling and Application Load Balancer. Exploring the seamless setup process, empowering your applications to effortlessly scale bas
7 min read
AWS Application Cost Profiler
AWS is another name for Amazon Web Services. It is a cloud service platform that provides a variety of services such as databases, storage, and on-demand computing capabilities. AWS offers around 200 featured Services. Although an AWS service may be functionally restricted on its own, AWS services m
5 min read