Monitoring
The broker metrics that matter, consumer lag, and Kafka in Prometheus
Kafka exposes thousands of JMX metrics. A handful tell you whether the cluster is healthy; one tells you whether your consumers are. The book's Monitoring Kafka chapter catalogues them; this lesson wires the important ones into the Prometheus & Grafana stack.
How metrics get out
- Brokers: the Prometheus JMX exporter runs as a Java agent inside each
broker (
broker/Dockerfile), turning selected MBeans into Prometheus text on port 7071 (published as 7071-7073).broker/jmx-exporter.ymldecides which MBeans are exported and how they're named. - Consumer lag: brokers don't publish lag directly.
kafka-exporter(port 9308) asks the cluster for each group's committed offsets and each partition's end offset, and exports the difference.
Prometheus scrapes both (jobs kafka and kafka-exporter), and Grafana
provisions the Kafka Overview dashboard:
cd ../prometheus && docker compose -f compose_prod.yaml up -d
open http://localhost:3001 # Dashboards -> Messaging -> Kafka OverviewCluster health: the first four
| Metric | Healthy | Meaning when not |
|---|---|---|
kafka_controller_kafkacontroller_activecontrollercount (summed) | Exactly 1 | 0: no controller, nothing can change. 2: split brain |
kafka_server_replicamanager_underreplicatedpartitions | 0 | A broker is down or a follower can't keep up |
kafka_controller_kafkacontroller_offlinepartitionscount | 0 | Partitions with no leader: reads and writes fail |
up{job="kafka"} | 1 per broker | A broker or its metrics endpoint is down |
These are the four alerts in prometheus/config/rules/alerts.yml:
KafkaNoActiveController, KafkaUnderReplicatedPartitions,
KafkaOfflinePartitions, and TargetDown.
activecontrollercount is 1 on the broker that's the active controller
and 0 on the others, so the meaningful signal is the sum across
brokers. On this cluster, kafka-1 reports 0 because kafka-3 holds the
KRaft leadership, and sum(...) returns 1.
Throughput and saturation
# messages and bytes in per second (topic="" = the broker-wide total)
sum(rate(kafka_server_brokertopicmetrics_messagesinpersec_total{topic=""}[5m]))
sum by (broker) (rate(kafka_server_brokertopicmetrics_bytesinpersec_total{topic=""}[5m]))
# busiest topics
topk(10, sum by (topic) (rate(kafka_server_brokertopicmetrics_messagesinpersec_total{topic!=""}[5m])))
# request latency: p99 time to complete a produce request
max by (broker) (kafka_network_requestmetrics_totaltimems{request="Produce", quantile="99thPercentile"})
# saturation: fraction of time request handler / network threads are idle
kafka_server_request_handler_avg_idle_ratio
kafka_network_processor_avg_idle_ratioIdle ratios below about 30% mean a broker is running out of threads to
handle requests. Latency rises next, then produce timeouts. The fix is more
brokers, more num.io.threads/num.network.threads, or less work per
request (bigger batches).
The JMX exporter lowercases metric names but not label values: the
quantile label is 99thPercentile, not 99thpercentile. The first
version of this module's dashboard used the lowercase value and its p99
panel was silently empty. When a Grafana panel shows "No data", copy its
query into Prometheus and check the label values.
Consumer lag
kafka_consumergroup_lag{consumergroup="analytics", topic="clicks", partition="0"} # per partition
sum by (consumergroup) (kafka_consumergroup_lag_sum) # per group
# run labs/lag.py: 200 msg/s in, 50 msg/s consumed
kafka_consumergroup_lag_sum{consumergroup="analytics",topic="clicks"} 5679 # after 45 sAbsolute lag is hard to alert on (10,000 is nothing for a group doing 50,000/s and a crisis for one doing 10/s). Better signals:
- Lag that keeps growing: consumers can't keep up. The
KafkaConsumerLagGrowingalert usesderiv()over 10 minutes. - Lag in time: how old is the oldest unprocessed record? Tools like
Burrow or Kafka's own
records-lag-maxconsumer metric help here. - Lag versus retention: a group whose lag approaches the retention window is about to lose data silently.
- Uneven lag across partitions: one partition far behind usually means a hot key or a stuck consumer.
Disk and the JVM
sum by (topic) (kafka_log_size_bytes) # data per topic per broker
jvm_heap_used_bytes / jvm_heap_max_bytes # heap pressure
rate(jvm_gc_collection_time_ms_total[5m]) # time spent in GCDisk is the capacity limit that bites first. Alert on free space with
node_exporter, and remember that retention is enforced per partition.
Long GC pauses look like a broker going away: followers drop out of
the ISR, and consumers get rebalanced.
Client-side metrics
The most useful metrics are sometimes in the clients: producer
record-error-rate and request-latency-avg, consumer records-lag-max
and rebalance counts. librdkafka emits them as JSON with
statistics.interval.ms; Java clients through JMX.
Lab
- Start the Prometheus stack, open Kafka Overview, and run
producer_basics.py. Find the throughput spike. - Run
lag.pyfor two minutes and watch Lag by consumer group and theKafkaConsumerLagGrowingalert go pending. - Stop a broker and watch Brokers up, Under-replicated partitions, and the leader counts change.
- In Prometheus, run
count by (__name__) ({job="kafka"})to see every metric the JMX exporter produces, then add one tojmx-exporter.yml.