Open In App

How to Configure AWS X-Ray Application Tracing?

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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": "*"

}

]

}

Screenshot-2024-09-14-183322

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
Screenshot-2024-09-24-154016

Now install xray

sudo yum install -y /home/ec2-user/xray.rpm
Screenshot-2024-09-24-1541

After the installation, you can start and enable the X-Ray daemon:

sudo systemctl start xray
sudo systemctl enable xray
Screenshot-2024-09-24-154409

Verify that the daemon is running using the following command:

sudo service xray status
Screenshot-2024-09-24-154442

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 -
Screenshot-2024-09-14-180746

Now Install node.js

sudo yum install -y nodejs
Screenshot-2024-09-14-180901

Verify installation:

After installation, verify that Node.js and npm were installed successfully by checking their versions:

node -v
npm -v
Screenshot-2024-09-14-181003

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
Screenshot-2024-09-14-181121

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
Screenshot-2024-09-14-181316

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
Screenshot-2024-09-14-181506

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'));

Screenshot-2024-09-14-181758

Step 7: Run the Application

Run the script using Node.js:

node app.js
Screenshot-2024-09-14-181948

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

Screenshot-2024-09-14-182116

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

Screenshot-2024-09-14-182324

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.
Screenshot-2024-09-14-182442

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.


Next Article
Article Tags :

Similar Reads