Tracing with X-Ray and OpenTelemetry
Following one request across services: spans and context propagation, sampling, OpenTelemetry on AWS, X-Ray, Transaction Search and Application Signals.
CloudWatch Logs and Logs InsightsLambda functions and the execution modelSQS queues
Tracing with X-Ray and OpenTelemetry
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A trace is the tree of timed spans for one request. It shows where the time went and which hop failed.
- Tracing works only if every hop passes the context on. One uninstrumented service splits the trace.
- Instrument with OpenTelemetry. The X-Ray SDKs and daemon entered maintenance mode in February 2026.
- X-Ray remains the AWS backend for traces, viewed through CloudWatch with a map of service dependencies.
- Sampling keeps cost in check. The X-Ray default records the first request each second and 5 percent of the rest.
- Lambda active tracing and Application Signals give useful coverage with little code, and custom spans add the business detail.
Six services, one slow request
A checkout takes four seconds. The API, the order function, the payment service and the database each have logs and metrics, and each looks roughly fine. What you’re missing is the line connecting them for this request.
Distributed tracing draws that line. A span is one timed operation; a trace is the tree of spans for one request. It works like parcel tracking: every depot scans the same number, so you can see where the parcel sat.
Context propagation
The trick is that each caller sends its trace ID and current span ID along with the request, usually in a header, and the callee creates its spans as children. OpenTelemetry, the vendor-neutral standard, does this with the W3C traceparent header.
I ran a two-“service” script with the OpenTelemetry Python SDK 1.41.1, passing a plain dictionary as the headers:
headers = {}
propagate.inject(headers) # caller
ctx = propagate.extract(headers) # callee
with tracer.start_as_current_span("payment: charge", context=ctx): ...
header sent: {'traceparent': '00-f4a80749c625b961913d24b701c1dea4-170e9e3c344b5117-01'}
checkout: POST /orders 133 ms
payment: charge 121 ms
db: insert 120 ms
All spans share one trace ID, the middle part of the header. The final 01 says “this one is sampled”. The tree answers the question at a glance: of 133 ms, 120 are the database insert (here, a deliberate sleep). Remove the inject and extract and you get two unrelated traces.
Where AWS fits
AWS X-Ray stores and analyses traces and draws a trace map of who calls whom. Its own vocabulary is segments and subsegments, with indexed annotations for searching and unindexed metadata for detail. Its native header is X-Amzn-Trace-Id, and it also accepts W3C-style trace IDs.
The change to know: the X-Ray SDKs and daemon entered maintenance mode on 25 February 2026. AWS recommends instrumenting with OpenTelemetry, which AWS Distro for OpenTelemetry (ADOT) packages. X-Ray the service is not going away with them.
Spans can reach AWS through a collector or the CloudWatch agent, or directly to an OTLP endpoint (https://xray.<region>.amazonaws.com/v1/traces).
Around the raw traces, CloudWatch adds two layers. Transaction Search lets you query spans in detail. Application Signals instruments applications on EC2, ECS, EKS and Lambda, produces latency, error and fault metrics per service, and tracks service level objectives.
Managed services in the path
Some hops you don’t instrument. With active tracing on, Lambda records its own segments for sampled requests and honours the upstream sampling decision. Traces from upstream SQS producers are linked to the consumer function. Remember the permissions: the role must be allowed to send traces, or they vanish silently.
Sampling
Recording everything costs money and mostly stores identical successful requests. X-Ray’s default rule takes the first request each second (the reservoir) plus 5 percent of the rest. Rules can be edited centrally by route or service without redeploying.
The decision is normally made at the start and passed along, so a trace is whole or absent. That means the rare failure may not be sampled. Raise rates on the paths that matter, and put the trace ID in every log line so the two can be joined.
A sensible order of work
Turn on what the platform gives for free. Add OpenTelemetry auto-instrumentation for HTTP, AWS SDK and database calls. Then add a few custom spans and attributes for the business steps. Never put secrets or personal data in attributes.
Asynchronous hops
A queue breaks the simple parent and child picture. The producer finishes long before the consumer starts, and one batch may contain messages from many requests. OpenTelemetry handles this with links, which connect a span to other traces without claiming they caused it as a parent would. When reading such a trace, separate time spent waiting in the queue from time spent working.
About the video
The embed is an AWS re:Invent 2023 session on observability with AWS Distro for OpenTelemetry, 39 minutes. Its chapters define observability and telemetry signals, cover instrumentation practice and OpenTelemetry, then OpenTelemetry on AWS, collectors on EKS and serverless instrumentation. It predates the X-Ray SDK maintenance date, the OTLP endpoint changes and much of Application Signals. I judged it from chapters and description only, since transcripts couldn’t be fetched. English was judged from its English caption track and title. A 2026 X-Ray console demo is in deeper.
What was run: the script and its output are real, from the OpenTelemetry SDK with an in-memory exporter. Nothing was sent to AWS. X-Ray behaviour is from the AWS docs, checked 2026-11-29.
Where this fits
Metrics, logs and traces watch your application. The next lesson watches the account itself: CloudTrail and AWS Config.
Check yourself
A trace shows the API calling the queue producer, then stops. The consumer has traces of its own. What is wrong?
Context is not being carried across that boundary. The consumer must read the trace context sent with the message and continue it, or link to it. Check that both sides are instrumented and agree on the propagation format.
You sample 5 percent and cannot find the trace for the one failed order a customer reports. Why, and what could you change?
The decision was made when the request started, before anyone knew it would fail. Sample important routes at a higher rate, or collect all spans and decide later, and always log the trace ID so logs still help.
What should go in an X-Ray annotation and what in metadata?
Annotations are indexed, so use them for values you search or group by, such as a tenant or order type. Metadata is not indexed and suits larger detail you only read after finding the trace.
Should new code use the X-Ray SDK?
No. It is in maintenance mode, receiving security fixes only. Use OpenTelemetry, for example through AWS Distro for OpenTelemetry, and send traces to X-Ray.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.