Video in progress
How do you trace requests that pass through Kafka end to end (tracing, lag SLOs, alerts)?
I pass the trace context in Kafka headers (OpenTelemetry/Micrometer does this automatically), measure lag both in message count and in time, and set alerts tied to the business SLO. In an asynchronous chain, a trace and an end-to-end latency metric answer "where did it get delayed?".
📖 Answer
Three layers:
- Distributed tracing. The producer adds a
traceparentheader, and the consumer opens a span from it. In Spring Boot 3 with Micrometer Tracing this is one setting:
@Bean
ConcurrentKafkaListenerContainerFactory<String, Object> factory(ConsumerFactory<String, Object> cf) {
var f = new ConcurrentKafkaListenerContainerFactory<String, Object>();
f.setConsumerFactory(cf);
f.getContainerProperties().setObservationEnabled(true);
return f;
}
// for KafkaTemplate too: template.setObservationEnabled(true)
In Spring Kafka observation, the consumer span continues from the context in the header (it becomes a child of the producer span). The OpenTelemetry messaging conventions, especially for batches, recommend connecting with a "link" instead, so a single trace doesn't stretch over hours. In a batch listener each record has its own context, and you have to look at this by hand.
-
End-to-end latency. The event has
occurredAt; the consumer recordsnow - occurredAtas a histogram. This answers the business question "the user paid, how many seconds until the order updated?" directly. Lag (message count) alone isn't enough: a lag of 10,000 can be 2 seconds at high traffic and 1 hour at night. -
Alerts based on the SLO. "99% of order events are processed within 30 seconds": alert on the error budget burning for that SLO. Plus: a message landed in the DLT (each one needs someone to look), no active members in the consumer group,
UnderMinIsrpartitions on the broker, the producerrecord-error-rate.
On one project the most useful thing was a single dashboard per topic: "who writes, who reads, what the lag is, how many are in the DLT". During an incident, looking at one place saves a lot of time.
To export Kafka-level metrics (JMX) to Prometheus, use the JMX exporter, and for lag use kafka-exporter or Burrow; they measure independently of the consumer, so lag is visible even if the consumer is dead.
Sign up to mark studied questions and save your progress.