What is AWS X-Ray?

Key takeaways:

  • AWS X-Ray traces requests across distributed applications for debugging and analysis.

  • It provides end-to-end visibility for web API calls, databases, and microservices.

  • X-Ray requires the application to be instrumented using the X-Ray SDK and agent.

  • Each request generates a unique trace ID, and segments track component interactions.

  • Data points in a trace represent components, including response time and interaction details.

  • The X-Ray agent forwards trace data to a tracing framework through an AWS SDK or API.

  • X-Ray integrates with other AWS services and HTTP servers for further analysis.

  • Trace maps and views visually represent requests and components in the application.

  • Developers can monitor requests and isolate performance issues using X-Ray’s detailed insights.

  • X-Ray simplifies debugging in microservices architectures by providing visual trace maps and tables.

AWS X-Ray helps trace requests across distributed applications, allowing developers to analyze and debug by tracking responses and calls made by different components. It provides an end-to-end view of web API calls, databases, and microservices.

To use X-Ray, the application must be instrumented to log all requests and events. This can be done by adding the X-Ray SDK and installing the X-Ray agent. Applications running on EC2 instances, ECS, custom Java applications, and more can be instrumented for tracing.

How does AWS X-Ray work?

When an instrumented application receives a request, it generates a trace and assigns it a unique trace ID. As the request interacts with different components, segments are created, linked to the trace ID, and store details about the quality of those interactions. Each segment contains comprehensive information about the component.

In this way, each trace consists of a collection of data points sharing the same trace ID. These data points represent the services and components the request passes through, including details like the response and duration of each interaction.

How X-Ray works
How X-Ray works

The data is then sent to the X-Ray agent, which forwards it to a tracing framework, either through an AWS SDK or API. The X-Ray agent can integrate with other AWS services, HTTP servers, queries, or methods to leverage the traced data further. X-Ray aggregates the trace data and creates detailed trace views and service maps.

To better understand X-Ray, consider a scenario where an API Gateway triggers a Lambda function upon receiving HTTP requests. If the application is instrumented for X-Ray monitoring, X-Ray will track the application’s activity. When the request reaches the API Gateway, X-Ray generates a trace with a unique trace ID. As the request triggers the Lambda function, X-Ray creates a segment for the function and associates it with the trace ID. By the end of the request, the trace contains detailed records of the components involved, whether the execution was successful or not.

A simplified explanation for X-Ray
A simplified explanation for X-Ray

Trace views

A trace view lists all the traces that the AWS Agent has received. Each trace contains the name of the request, status, duration, and response.

Trace maps

Trace maps plot the tracks of API requests and services connected through arrows in a sequence. They visually aid developers in tracking the application’s behavior and isolating faulty components. Additionally, each node in a trace map can be expanded to view detailed configurations, such as latency histograms, which help detect services or components slowing down the application.

Try it yourself

To understand how X-Ray traces applications, try out the example given below. The code widget contains the SAM template for the example we discussed above. It launches an API Gateway and a Lambda function that returns Hello World. As mentioned on line 13, the template enables X-Ray on both the API Gateway and the Lambda function, which allows us to keep the trace of the request we send to the API Gateway.

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
SAM template for the application

Click “Run” to open the terminal. The application requires an access key and a secret access key of your AWS account. Therefore, run the command given below o configure your credentials. Make sure to replace <ACCESS_KEY> and <SECRET_ACCESS_KEY> with your credentials.

aws configure set default.region us-east-1 && aws configure set aws_access_key_id <ACCESS_KEY> && aws configure set aws_secret_access_key <SECRET_ACCESS_KEY> && aws configure set default.output json

After configuring the credentials, run the command given below to build the deploy the application:

zip -r hello_world.zip hello_world/
sam build && sam deploy --stack-name test --resolve-s3 --capabilities CAPABILITY_IAM

Make sure you have the necessary permissions to create the required resources. Notice that the output of the SAM template returns the invoke URL of API Gateway. Copy the URL and paste it on the browser to send request API Gateway.

As we have X-Ray enabled on both of the services, X-Ray will trace the request. To view the trace, go to the X-Ray dashboard. Follow the steps below to open the trace map:

  • On the AWS Management Console, search for “X-Ray” and select “X-Ray” from the search results.

  • On the X-Ray dashboard, select “Trace Maps” to view the nodes for each service and how they are connected.

  • From the side bar menu select “Traces” to view the status, response and duration of each request in a tabular from.

Conclusion

In a microservices-based distributed architecture, it can become extremely difficult for developers to debug the application, especially if the application spans across on-premises and cloud services. AWS X-Ray is a specialized and managed service that helps developers track the application's behavior, analyze, and debug through visual aids as well as detailed tables.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What does AWS X-Ray do?

AWS X-Ray provides end-to-end tracing of requests in distributed applications, enabling developers to analyze and debug performance issues across various components and services.


What is X-Ray sampling in AWS?

X-Ray sampling in AWS is a process that selectively records a subset of requests to reduce data collection costs while still providing insights on application performance and behavior.


What is X-Ray report on Amazon?

An X-Ray report on Amazon provides detailed insights into the traces collected from an application, including metrics such as response times, error rates, and the performance of individual components, helping developers analyze and troubleshoot application behavior effectively.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved