In this lesson · topic 5 of 7
Topic 5
Traces that cross a queue
7 min read
Everything so far assumed calls that wait for an answer. Real systems also hand work to a queue and move on: send the email, resize the photo, update the search index. A trace has to survive that handoff or the most interesting half of the request goes missing.
Context rides in the message
An HTTP client puts the trace context in a header. A producer does the same with a message, using whatever metadata slot the broker offers, such as Kafka record headers or AMQP properties. The consumer reads it when the message arrives and starts its own span. The mechanism is identical to HTTP propagation. Only the carrier changed.
Suppose a checkout request takes 80 ms and publishes an order event in its last 5 ms. The consumer picks it up 4.2 seconds later and works for 300 ms. The trace now has a 5 ms publish span, a 4.2 second gap, and a 300 ms process span. The user waited 80 ms. The workflow took about 4.5 seconds. Both are true, and they answer different questions, so tools should not fold the gap into the request’s critical path.
Parent or link
A span has exactly one parent. That works for one message, but fails the moment a consumer takes a batch.
Say a worker polls 50 messages that came from 50 different requests and handles them in one database transaction. It cannot be the child of 50 parents. OpenTelemetry’s messaging conventions solve this with span links: the processing span records a link to each message’s creation context. A link says “this work is related to that span” without claiming that the span called it. The conventions use links as the default way to connect producers and consumers, and allow a direct parent only for the single-message case.
Links also help when the delay is long. A job scheduled for tomorrow would otherwise produce a trace that is open for 24 hours. Starting a fresh trace on the consumer and linking back keeps each trace short and still lets you hop between them.
Predict first
How it breaks in production
The common failure is silent. A library upgrade, a custom serializer, or a forwarding job copies the message body but drops its headers. Traces still appear, but every consumer span is now a root, with no producer above it. Nothing errors. You find out during an incident, when the trace you need ends at the queue.
You can detect it with a metric. Count consumer spans that have no parent and no link. In a healthy pipeline that count is near zero. When it climbs, a hop is dropping context.
Retries and dead-letter queues cause the same trouble. A redelivered message should keep its original context, so each attempt shows up under the same producer. When a failed message is republished to a dead-letter queue, the republisher must copy the headers. If it builds a new message, the context is lost exactly when you need to debug the failure.
Sources
- Messaging spans, OpenTelemetry semantic conventions, on creation context, links and batch processing.
- Traces, OpenTelemetry documentation, on spans and span links.
- Sergey Kanzhelev, Morgan McLean, Alois Reitbauer, Bogdan Drutu, Nik Molnar and Yuri Shkuro, Trace Context, W3C Recommendation, 2021.