Skip to content
mentoor

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?

Choose one answer

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

  1. Span. One unit of work with a name, start, duration, status and attributes. Spans form a tree: the root span is the incoming request.
  2. 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.
  3. Instrument. OpenTelemetry (OTel) auto-instruments common HTTP clients, servers and databases. Add manual spans around your own work, such as a retrieval step.
  4. 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.
  5. 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?

Choose one answer

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?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic