Sign inSign up

themoah/klag

By themoah

•Updated 10 days ago

Klag is a service for monitoring Kafka consumer behaviour and lag. github.com/themoah/klag

Image
Monitoring & observability
0

8.2K

themoah/klag repository overview

⁠Klag — Kafka Consumer Lag Exporter

Know when your consumers fall behind, before it becomes a problem.

Klag continuously monitors all Kafka consumer groups and exports lag metrics to Prometheus, Datadog, or any OpenTelemetry-compatible backend. Inspired by kafka-lag-exporter⁠ (archived 2024). Built on Vert.x and Micrometer.

Scales to large clusters: monitors thousands of consumer groups in ~50MB heap. Configurable request batching prevents overwhelming brokers when monitoring 500+ groups.


⁠Quick Start

docker run -e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
           -e METRICS_REPORTER=prometheus \
           -p 8888:8888 \
           themoah/klag:latest

Metrics available at http://localhost:8888/metrics


⁠Why Klag?

Consumer lag is the gap between what Kafka has produced and what your consumers have processed. Left unmonitored, growing lag leads to:

  • Stale data in downstream systems
  • Memory pressure as consumers struggle to catch up
  • Silent failures when consumer groups die without alerts
  • Data loss when lag exceeds Kafka's retention window

⁠Key Features

FeatureWhy It Matters
Lag velocityKnow if lag is growing or shrinking — catch problems before they escalate
Time-based lag estimationSee lag in seconds/minutes, not just message counts
Hot partition detectionFind partitions with uneven load causing bottlenecks
Data loss preventionAlert before consumers fall past Kafka's retention window
Consumer group state trackingAlert on Rebalancing, Dead, or Empty states
Request batchingSafely monitor large clusters without overwhelming brokers
Stale group cleanupAutomatically stops reporting deleted/inactive groups

⁠Supported Metric Backends

BackendMETRICS_REPORTER value
Prometheus (scrape endpoint)prometheus
Datadogdatadog
OTLP / OpenTelemetry (Grafana Cloud, New Relic, etc.)otlp

⁠Metrics Reference

MetricDescription
klag.consumer.lagCurrent lag per partition (also .sum, .max, .min)
klag.consumer.lag.velocityRate of change — positive means falling behind
klag.consumer.lag.msLag converted to milliseconds using offset/timestamp interpolation
klag.consumer.lag.time_to_close_secondsEstimated seconds until lag reaches zero (catching-up groups only)
klag.consumer.lag.retention_percentLag as % of available messages — use to prevent data loss
klag.consumer.group.stateGroup health: Stable, Rebalancing, Dead, Empty
klag.hot_partitionThroughput × 100 for statistically abnormal partitions
klag.hot_partition.lagLag on hot partitions specifically
klag.topic.partitionsPartition count per topic
klag.partition.log_end_offsetLatest offset per partition
klag.consumer.committed_offsetLast committed offset per consumer

All metrics are tagged with consumer_group, topic, and partition where applicable.


⁠Configuration

VariableDefaultDescription
KAFKA_BOOTSTRAP_SERVERSlocalhost:9092Kafka broker addresses
KAFKA_SECURITY_PROTOCOL(empty)PLAINTEXT, SSL, SASL_PLAINTEXT, SASL_SSL
KAFKA_SASL_MECHANISM(empty)PLAIN, SCRAM-SHA-256, SCRAM-SHA-512
KAFKA_SASL_JAAS_CONFIG(empty)JAAS config string for SASL auth
KAFKA_CHUNK_COUNT1Split offset requests into N batches (large clusters)
KAFKA_CHUNK_DELAY_MS0Delay (ms) between batches
METRICS_REPORTERnoneprometheus, datadog, or otlp
METRICS_INTERVAL_MS60000Metric collection interval in milliseconds
METRICS_GROUP_FILTER*Glob pattern to filter consumer groups
METRICS_JVM_ENABLEDfalseExpose JVM metrics (memory, GC, threads, CPU)
HTTP_PORT8888HTTP server port
⁠OTLP / OpenTelemetry
METRICS_REPORTER=otlp
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-us-east-0.grafana.net/otlp
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic <base64-encoded-credentials>
OTEL_SERVICE_NAME=klag-production
⁠Datadog
METRICS_REPORTER=datadog
DD_API_KEY=<your-api-key>
DD_SITE=datadoghq.com

⁠Deployment

⁠Docker with env file
docker run --env-file .env -p 8888:8888 themoah/klag:latest

Sample .env:

KAFKA_BOOTSTRAP_SERVERS=instance.gcp.confluent.cloud:9092
KAFKA_SECURITY_PROTOCOL=SASL_SSL
KAFKA_SASL_MECHANISM=PLAIN
KAFKA_SASL_JAAS_CONFIG="org.apache.kafka.common.security.plain.PlainLoginModule required username=user password=pass;"
METRICS_REPORTER=prometheus
METRICS_INTERVAL_MS=30000
⁠Kubernetes (Helm)
helm install klag ./charts/klag \
  --set kafka.bootstrapServers="kafka-broker:9092"

The Helm chart includes liveness/readiness probes, ServiceMonitor support for Prometheus Operator, and SASL secret management.

⁠Health Endpoints
EndpointPurpose
/healthzLiveness probe — always 200
/readyzReadiness probe — 200 if Kafka connected, 503 if not
/metricsPrometheus scrape endpoint (when METRICS_REPORTER=prometheus)

⁠Security

Klag requires read-only Kafka access. It uses only the Kafka Admin Client API with DESCRIBE permissions — no write or alter access needed.

Required ACLs:

ResourcePermission
CLUSTERDESCRIBE — health check, list consumer groups
TOPIC *DESCRIBE — partition info and offsets
GROUP *DESCRIBE — group state and committed offsets

Works with self-managed Kafka, Confluent Cloud, and MSK.


⁠Grafana Dashboard

A pre-built Grafana dashboard (dashboard/demo-dashboard.json) is included in the GitHub repository⁠ with 40 panels covering:

  • Consumer lag by group (time series + thresholds)
  • Lag velocity trends
  • Consumer group state table
  • Time-based lag estimation
  • Data loss prevention (retention risk)
  • Hot partition detection
  • JVM metrics (optional)

Import via Grafana → Dashboards → Import → Upload JSON.


⁠Image Details

  • Base image: Eclipse Temurin JRE 21 (slim)
  • Architectures: linux/amd64, linux/arm64
  • Exposed port: 8888
  • Runtime flags: -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0
  • Also available: ghcr.io/themoah/klag:latest

Licensed under the Apache 2.0 License⁠.

Tag summary

Content type

Image

Digest

sha256:610579160…

Size

127 MB

Last updated

10 days ago

docker pull themoah/klag