Video in progress
How do you trace an event from a backend service through Kafka, the delivery service and the gateway until it reaches the browser?
You pass the trace context (W3C traceparent) in message headers at every hop, and create OpenTelemetry spans in Kafka, delivery and the gateway. The last step is the client's ack, so end-to-end latency and the places where messages are lost become visible.
📖 Answer
With HTTP, tracing is almost free: the agent passes the headers along itself. In an async real-time chain that isn't automatic. At every hop the context has to be passed through by hand or by instrumentation.
The chain:
- The producer service writes the event to Kafka. The OpenTelemetry Java agent instruments the Kafka producer and puts
traceparentinto the Kafka record header. - The delivery service opens a consumer span (as a link or a parent), makes routing and the inbox write separate spans, and when sending to the gateway adds the context to its own protocol (gRPC metadata or a field of the Redis message).
- The gateway creates a "send to client" span and puts the
traceIdinto a STOMP header or ametafield in the payload. - After showing the message, the client sends an
ackwith thetraceIdand the client-side time. The gateway closes the final span or records an event.
Passing the context in STOMP:
SimpMessageHeaderAccessor acc = SimpMessageHeaderAccessor.create();
acc.setNativeHeader("traceparent", currentTraceparent());
acc.setLeaveMutable(true);
messaging.convertAndSendToUser(userId, "/queue/events", payload, acc.getMessageHeaders());
There's a fan-out problem: if one event goes to 100,000 clients, 100,000 spans will kill the tracing backend. I sample at the fan-out stage: one shared "broadcast" span plus full spans for a random 0.1% of clients. Or an aggregate metric from them.
Besides tracing, to answer "where did this message get lost", keep structured logs with the eventId at every hop. Because of trace sampling the message you need may not be in a trace, but it will be in the log.
And take the end-to-end SLO not from traces but from the latency histogram of the client acks. Traces are for investigation, metrics are for alerts.
Sign up to mark studied questions and save your progress.