Tracing Requests Through Distributed Applications with AWS X-Ray
This lesson explains how AWS X-Ray traces requests as they travel through your services, helping you find where latency and errors originate in a distributed application.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how traces, segments, subsegments, annotations and metadata record a request.
- Explain how sampling rules control how many requests are traced.
- Identify the component that sends trace data on each compute service, such as the X-Ray daemon or the ADOT collector.
- Use filter expressions and the trace map to find slow or failing requests.
Traces, segments and annotations
X-Ray follows each request through a distributed application. When a request enters the first instrumented service, it receives a trace ID, which is passed to every downstream service in the X-Amzn-Trace-Id header. Everything recorded for that request shares the ID and together forms one trace.
Each service that handles the request records a segment, containing its name, start and end times, status and any error. Within a segment, subsegments record the work the service does in more detail, especially calls to other services, such as a DynamoDB query or an HTTP request to a payment API, so the trace shows exactly how long each call took. Developers can add custom subsegments around sections of their own code.
Segments can carry two kinds of extra data. Annotations are key-value pairs that X-Ray indexes, so traces can be searched with filter expressions, such as all traces where customer_tier is gold. Metadata can hold any value, including whole objects, but is not indexed, so it is for detail to read once a trace has been found. From all traces, X-Ray builds a service map that shows each service and its connections, with request rates, latency and error rates. The following table summarises the concepts.
| Concept | Meaning |
|---|---|
| Trace | One request followed from end to end |
| Segment | The work done by one service for that request |
| Subsegment | A downstream call, such as a DynamoDB query or HTTP request |
| Annotation | An indexed key-value pair you can filter and search on |
| Metadata | Extra detail that is not indexed |
| Service map | A diagram of dependencies with latency and error rates |
Sampling
Sampling rules decide how many requests are traced. By default X-Ray records the first request each second plus 5% of additional requests, keeping cost down while staying representative.
1per second
Requests always traced by the default rule
5%
Of additional requests traced by the default rule
Sending trace data
Applications do not send trace data straight from their own code to the X-Ray API on every request. A component close to the application collects the segments and sends them in batches, and how that component is provided depends on the compute service.
In Lambda, turning on active tracing is enough to record a segment for every sampled invocation, because Lambda runs the collector itself; the execution role needs permission to send trace data. To record subsegments for the function's own calls, the code is also instrumented. API Gateway, Step Functions and several other services can add their own segments when tracing is enabled on them.
On EC2 and ECS, the application sends segments over UDP port 2000 to a collector running alongside it, either the X-Ray daemon or the AWS Distro for OpenTelemetry (ADOT) collector, which then sends them to X-Ray. On EC2 the collector runs on the instance; on ECS it usually runs as a sidecar container in the same task. The following table summarises how tracing is enabled.
| Compute | How to enable tracing |
|---|---|
| Lambda | Turn on active tracing |
| EC2, ECS | Run the X-Ray daemon (UDP port 2000) or the AWS Distro for OpenTelemetry collector, which AWS recommends for new apps |
The X-Ray SDKs and daemon entered maintenance mode in February 2026 and reach end of support in February 2027. They still work and remain common in exam questions, but new instrumentation should use OpenTelemetry, through the AWS Distro for OpenTelemetry or CloudWatch Application Signals. The following list describes two further requirements.
- Whatever sends the data needs
xray:PutTraceSegmentsandxray:PutTelemetryRecords. - Instrumenting AWS SDK clients and HTTP libraries records subsegments for calls to DynamoDB, S3 and other services automatically.
Example. patch_all() traces every boto3 and requests call. In Lambda the function's segment is created by the service, so annotations go on a subsegment you create.
from aws_xray_sdk.core import patch_all, xray_recorder
patch_all()
def handler(event, context):
with xray_recorder.in_subsegment("charge-card") as sub:
sub.put_annotation("customer_tier", event["tier"]) # indexed, usable in filter expressions
sub.put_metadata("request", event) # not indexed, for debugging detail
return charge(event)Analysing traces
Filter expressions find traces by annotation, response time and more. Slow requests from gold customers:
annotation.customer_tier = "gold" AND responsetime > 2Trace groups and X-Ray Insights detect anomalies in error and latency rates.
No traces from a service
A team enables X-Ray but sees no traces from its ECS service, while its Lambda functions appear correctly.
| Check | Finding |
|---|---|
| Is a collector running? | Lambda has one built in; ECS needs the X-Ray daemon or ADOT collector as a sidecar container |
| Can the task send traces? | The task role needs xray:PutTraceSegments and xray:PutTelemetryRecords |
| Is the code instrumented? | The SDK or OpenTelemetry must wrap incoming requests and AWS clients |
| Is sampling hiding them? | Default sampling records the first request each second plus 5% |
Adding the sidecar and the task role permissions makes the service appear on the trace map, linked to the Lambda functions it calls.
What is the difference between annotations and metadata in an X-Ray segment?
Show answer
Annotations are indexed key-value pairs that can be used in filter expressions to find traces. Metadata is not indexed and only adds detail to a trace you are already looking at.
Exam Tips
Remember for the exam
- To filter traces by user ID, record it as an annotation, not metadata.
- If there are no traces from EC2, check that the daemon is running and the instance role has X-Ray permissions.
- To find traces slower than two seconds, use the filter expression
responsetime > 2. - To record DynamoDB calls as subsegments automatically, instrument the AWS SDK client.