Topic 3 of 5 · 4 min read
Distributed tracing with OpenTelemetry
Goal: Read a trace to find the span behind a latency spike and the place where context propagation broke.
A trace is the timeline of one request across every service it touches, made of spans that share a trace id; it shows where the time went and where the chain broke. Metrics tell you the checkout is slow. A trace tells you which call inside it is slow.
Check yourself · 1 of 3
What does a trace show that a single log line does not?
Why it matters
A request to an agent feature may hit an API, a database, a vector search, two model calls and three tools. Logs from each service show fragments. A trace joins them into one picture, with parent and child spans and the time each took.
How to read and build one
- Span. One unit of work with a name, start, duration, status and attributes. Spans form a tree: the root span is the incoming request.
- Context propagation. When service A calls service B, A sends the trace id and parent span id in a header, usually the W3C
traceparent. B continues the same trace. If a service, queue or background job does not forward it, the trace splits in two and the gap is invisible. - Instrument. OpenTelemetry (OTel) auto-instruments common HTTP clients, servers and databases. Add manual spans around your own work, such as a retrieval step.
- Sample. Keeping every trace is costly. Head sampling decides at the start (for example 10 percent). Tail sampling decides after the trace finishes and can keep all slow or failed ones. Choose on purpose.
- Model and tool calls. The OTel GenAI semantic conventions define standard attributes such as the model name and token counts. Use them so any backend understands your model spans. Do not store full prompts as attributes by default, since they may hold personal data.
Before and after
// Before: the queue hop loses the trace
await queue.send({ orderId });
// After: carry the context in the message
const carrier: Record<string, string> = {};
propagation.inject(context.active(), carrier);
await queue.send({ orderId, carrier });
Without the carrier, the worker starts a new trace and the slow step appears to belong to no request.
What goes wrong
- Broken propagation. Two short traces instead of one long one; the cause of the delay sits in the missing link.
- Reading the wrong span. A long parent span is not the culprit if a child span accounts for nearly all of it. Look at self time.
- Sampling away the failures. A 1 percent head sample can drop the rare slow request you wanted.
- High-cardinality span names. Put ids in attributes, not in the span name.
Examples
Finding the slow span
A trace for POST /chat takes 6.2 s. The children: auth 40 ms, vector.search 180 ms, llm.chat 5.8 s, render 20 ms. The slow step is the model call, not the database. Next check the token counts on that span: a 30,000-token prompt explains the time.
A trace that stops
The API span ends at queue.send, and a separate trace begins in the worker with a different trace id. The message carried no traceparent. The fix is to inject context into the message and extract it in the worker, as in the code above.
Key points
- A trace is a tree of spans sharing one trace id; the root span is the incoming request.
- Context propagation passes the trace id and parent span id, usually in the W3C traceparent header, across every hop.
- Queues, background jobs and custom HTTP clients often lose context, which splits one trace into two.
- Head sampling is cheap but may drop rare slow requests; tail sampling can keep slow and failed traces.
- Use the OTel GenAI conventions for model and tool spans, and avoid storing full prompts as attributes by default.
Common mistakes
- Assuming auto-instrumentation covers queues and custom clients; it often does not.
- Blaming the longest parent span instead of the child that takes most of its time.
- Setting a very low head sample rate and then being unable to find a rare failure.
- Putting user ids or prompts into span names, which breaks grouping.
Check yourself · 2 of 3
A request goes through a message queue and the worker's spans appear in a separate trace. What is the likely cause?
Check yourself · 3 of 3
You want to keep every slow or failed trace but only a small share of fast ones. Which sampling fits?
Practice items, not counted towards your mastery.
Start here
- OpenTelemetry documentation (opens in a new tab)DocsFree
The vendor-neutral standard for traces, metrics and logs, with concepts and language guides.
- Monitoring Distributed Systems (Google SRE Book) (opens in a new tab)BookFree
Defines the four golden signals and explains why to page on symptoms and use percentiles.
- Alerting on SLOs (Google SRE Workbook) (opens in a new tab)BookFree
Worked burn-rate alerts that turn an SLO into pages that matter.
More for this topic
- OpenTelemetry semantic conventions for generative AI (opens in a new tab)StandardFree
Standard span and metric attributes for model and tool calls.