Learn Labs
Operations

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.yml decides 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 Overview

Cluster health: the first four

MetricHealthyMeaning when not
kafka_controller_kafkacontroller_activecontrollercount (summed)Exactly 10: no controller, nothing can change. 2: split brain
kafka_server_replicamanager_underreplicatedpartitions0A broker is down or a follower can't keep up
kafka_controller_kafkacontroller_offlinepartitionscount0Partitions with no leader: reads and writes fail
up{job="kafka"}1 per brokerA broker or its metrics endpoint is down

These are the four alerts in prometheus/config/rules/alerts.yml: KafkaNoActiveController, KafkaUnderReplicatedPartitions, KafkaOfflinePartitions, and TargetDown.

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_ratio

Idle 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).

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 s

Absolute 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 KafkaConsumerLagGrowing alert uses deriv() over 10 minutes.
  • Lag in time: how old is the oldest unprocessed record? Tools like Burrow or Kafka's own records-lag-max consumer 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 GC

Disk 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

  1. Start the Prometheus stack, open Kafka Overview, and run producer_basics.py. Find the throughput spike.
  2. Run lag.py for two minutes and watch Lag by consumer group and the KafkaConsumerLagGrowing alert go pending.
  3. Stop a broker and watch Brokers up, Under-replicated partitions, and the leader counts change.
  4. In Prometheus, run count by (__name__) ({job="kafka"}) to see every metric the JMX exporter produces, then add one to jmx-exporter.yml.

On this page