On this page
On this page
- What Is X-Ray
- Core Terminology
- Service Map
- Sampling
- Instrumentation โ How to Enable X-Ray Per Service
- Lambda
- API Gateway
- EC2 / ECS (on instances)
- ECS / Fargate โ Sidecar Pattern
- Elastic Beanstalk
- Instrumentation Summary Table
- X-Ray Groups
- CloudWatch ServiceLens
- X-Ray vs CloudWatch Metrics vs CloudWatch Logs
- Common Gotchas
- Key Exam Scenarios
- CloudWatch Integration
- Automatically Published
- Requires Setup
X-Ray
X-Ray is AWS's distributed tracing service. It traces requests as they travel through your application โ from the frontend to a Lambda function, through an SQS queue, into a microservice, and out to a database โ and visualizes the entire path with timing information so you can see exactly where slowness or errors occur.
The Microservices Visibility Problem
In a monolith, when a request is slow, you look at the single application's logs. In a distributed system of microservices, a slow request might cross five services, three queues, and two databases. When a user reports that checkout takes 8 seconds, which of those hops is responsible? Without distributed tracing, this question is extremely hard to answer.
X-Ray solves this by propagating a trace ID through every service a request touches. Each service records a segment โ its portion of the work โ and X-Ray assembles these into a complete picture of the request's journey.
How Instrumentation Works
You add the X-Ray SDK to your application code and it automatically traces:
- Incoming HTTP requests
- Outbound calls to AWS services (S3, DynamoDB, SQS, SNS, etc.)
- Outbound HTTP calls to external services
- SQL queries (with supported drivers)
For Lambda, ECS, and API Gateway, X-Ray can be enabled with a configuration toggle and requires minimal or no code changes. The SDK handles trace ID generation and propagation automatically.
The Service Map
X-Ray generates a service map โ a visual graph showing every service that participated in handling requests, the connections between them, and their error rates and latency distributions. At a glance, you can see which service is red (high error rate) or slow (wide latency box) and drill into the individual traces for that service.
Sampling
X-Ray doesn't trace every single request by default โ for high-traffic services that would generate enormous volumes of data. Instead, it samples a percentage of requests. You can configure the sample rate (e.g., 5% of requests) and define rules to always trace requests that meet certain criteria (e.g., always trace requests that return a 5xx error).