Tracing Distributed Applications with AWS X-Ray and OpenTelemetry
In a distributed application, one user request passes through an API, several functions or services, queues and databases. Metrics and logs show each component separately; traces show the whole request path and where time and errors occur. This lesson explains X-Ray concepts, sampling, how instrumentation has moved to OpenTelemetry, and how tracing is configured for Lambda, API Gateway, ECS, EKS and EC2.
Learning outcomes
After completing this lesson, you will be able to:
- Describe traces, segments, subsegments, annotations, metadata and the trace map.
- Explain sampling rules and how they control cost and coverage.
- Explain the status of the X-Ray SDKs and daemon and the role of OpenTelemetry and ADOT.
- Describe how tracing is enabled and permitted for Lambda, API Gateway, Amazon ECS, Amazon EKS and Amazon EC2.
Tracing Concepts
AWS X-Ray collects data about requests as they travel through an application. A trace represents one request end to end and is identified by a trace ID that is propagated between services in a header (X-Amzn-Trace-Id, or the W3C traceparent header with OpenTelemetry). Each service that handles the request records a segment with timing, status and metadata, and calls to downstream resources, such as a DynamoDB query or an HTTP call, are recorded as subsegments. In OpenTelemetry terms, all of these are spans.
Segments can carry annotations, which are indexed key-value pairs that can be used in filter expressions to search for traces, such as annotation.customerTier = "gold", and metadata, which is any additional data that is stored but not indexed. The trace map (service map) shows the services and resources an application uses, with request rates, latency and error rates on each connection, so a failing or slow dependency is visible immediately. Groups define sets of traces by filter expression with their own maps and metrics, and X-Ray Insights detects anomalies in fault rates and creates notifications.
The following diagram shows one trace as a timeline of segments and subsegments.

Sampling
Recording every request would be costly and add overhead, so traces are sampled. A sampling rule has a reservoir, a fixed number of requests per second that are always traced, and a rate, the percentage of additional requests traced. The default rule traces the first request each second and 5% of the rest. Custom rules match on service name, HTTP method, URL path and other attributes, with priorities, so that, for example, all requests to a payment endpoint are traced while health checks are not. Rules are configured centrally in X-Ray and applied by instrumented services without redeployment. With Transaction Search, spans are sent to CloudWatch Logs in the aws/spans log group, which allows all spans to be captured and searched cost-effectively while a configurable percentage is indexed as trace summaries.
Instrumentation: From X-Ray SDKs to OpenTelemetry
Applications must be instrumented to produce trace data: incoming requests start or continue a trace, and outgoing calls through AWS SDKs, HTTP clients and database drivers create subsegments. Historically, this was done with the X-Ray SDKs and a local X-Ray daemon that relayed segments to the service over UDP port 2000. The X-Ray SDKs and daemon entered maintenance mode in February 2026, with end of support in February 2027. X-Ray itself continues, but new instrumentation should use OpenTelemetry, the open standard for traces, metrics and logs.
The AWS Distro for OpenTelemetry (ADOT) is AWS's supported distribution of OpenTelemetry SDKs, auto-instrumentation agents and the OpenTelemetry Collector, with exporters for X-Ray, CloudWatch and Amazon Managed Service for Prometheus. Auto-instrumentation agents for Java, Python, .NET and Node.js instrument common libraries without code changes. The CloudWatch agent can also receive OpenTelemetry data (OTLP) and forward it, and CloudWatch accepts OTLP directly.
Daemon and SDK in older scenarios
Exam scenarios may refer to the X-Ray daemon and SDK. They describe the mechanism correctly, but for new designs, prefer ADOT or the CloudWatch agent with OpenTelemetry, which are the supported path.
Instrument for tracing
Enabling Tracing per Service
Each compute and integration service enables tracing differently, and each needs permission to send trace data, normally through the AWS managed policy AWSXRayDaemonWriteAccess (xray:PutTraceSegments, xray:PutTelemetryRecords and sampling actions) on the role that sends the data.
AWS Lambda
Enabling active tracing on a function (or Tracing: Active in SAM) makes Lambda record a segment for each sampled invocation, including initialisation, and pass the trace context to the function. The execution role needs the X-Ray write permissions. For details inside the function, such as downstream calls, the function uses the ADOT Lambda layer or Powertools for AWS Lambda tracer. If the function is invoked by a traced service, such as API Gateway, it continues that trace.
Amazon API Gateway
Enabling X-Ray tracing on a REST API stage makes API Gateway sample requests, record a segment for its own processing and pass the trace header to integrations such as Lambda, so the trace map shows the API and its backends. Sampling rules apply to API requests. Trace context propagates to HTTP backends through the trace header.
Amazon ECS
Tasks run the ADOT Collector or the CloudWatch agent as a sidecar container in the task definition, and application containers send OTLP data to it on localhost. The task role needs X-Ray write permissions, because the collector runs inside the task. On Fargate, the sidecar pattern is the only option; on EC2, a daemon service per instance is an alternative.
Amazon EKS
The ADOT add-on or the Amazon CloudWatch Observability add-on deploys collectors, typically as a DaemonSet on each node, and can inject auto-instrumentation into pods through annotations. Collector permissions are granted to its service account with EKS Pod Identity or IRSA.
Amazon EC2 and on-premises
The CloudWatch agent or ADOT Collector runs on the instance and receives OTLP data from instrumented applications; the instance profile needs X-Ray write permissions. Elastic Beanstalk environments have an option to run the tracing daemon.
| Service | How tracing is enabled | Identity that needs X-Ray permissions |
|---|---|---|
| Lambda | Active tracing setting; ADOT layer for detail | Function execution role |
| API Gateway (REST) | Stage setting for X-Ray tracing | Service-managed |
| ECS | ADOT Collector or CloudWatch agent sidecar | Task role |
| EKS | ADOT or CloudWatch Observability add-on | Collector service account (Pod Identity or IRSA) |
| EC2 | CloudWatch agent or ADOT Collector on the instance | Instance profile |
Enable tracing everywhere
Finding a Slow Dependency
An order API built with API Gateway, Lambda, an ECS service and DynamoDB has intermittent p99 latency of four seconds, and logs show no errors.
-
Enable tracing
Enable X-Ray tracing on the API stage and active tracing on the functions, add the ADOT Lambda layer, and add an ADOT Collector sidecar to the ECS task definition with X-Ray permissions on the task role.
-
Sample usefully
Add a sampling rule that traces 20% of requests to the order path and excludes health checks.
-
Annotate
Add annotations for customer tier and order size so slow traces can be filtered.
-
Analyse
The trace map shows the ECS service's calls to a third-party tax API with high latency for large orders; the team adds a timeout, a cache and an alarm on that subsegment's latency.
An ECS Fargate service has an ADOT Collector sidecar, but no traces appear, and the collector logs show access denied errors. Which role needs permissions?
Show answer
The task role, because the collector runs inside the task and uses the task role's credentials to call xray:PutTraceSegments. The task execution role is used only for pulling images, secrets and logs.
Exam Tips
Remember for the exam
- Traces consist of segments and subsegments (spans); annotations are indexed for filtering, metadata is not.
- Sampling rules use a reservoir plus a rate (default: 1 request per second plus 5%); configure them centrally.
- X-Ray SDKs and daemon are in maintenance mode (end of support February 2027); use ADOT or the CloudWatch agent with OpenTelemetry.
- Lambda: active tracing plus execution role permissions; API Gateway: stage tracing; ECS: collector sidecar with task role permissions; EKS: ADOT or CloudWatch Observability add-on.
- The trace map shows latency and errors per dependency.