Prometheus and Grafana integration in Kafka - Time & Space Complexity
When integrating Prometheus with Grafana, it is important to understand how the data collection and querying time grows as the amount of monitored data increases.
We want to know how the system's response time changes when more metrics or queries are involved.
Analyze the time complexity of the following Kafka consumer code snippet that collects metrics for Prometheus.
val consumer = new KafkaConsumer[String, String](props)
consumer.subscribe(List("metrics-topic").asJava)
while(true) {
val records = consumer.poll(Duration.ofMillis(100))
records.forEach { record =>
processMetric(record.value())
}
}
This code continuously polls Kafka for metric messages and processes each one to be scraped by Prometheus.
- Primary operation: Looping over all Kafka records received in each poll.
- How many times: Once per poll cycle, processing all messages fetched.
As the number of messages per poll increases, the processing time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 metric process calls |
| 100 | 100 metric process calls |
| 1000 | 1000 metric process calls |
Pattern observation: The time to process metrics grows linearly with the number of messages received.
Time Complexity: O(n)
This means the processing time increases directly in proportion to the number of metric messages received each poll.
[X] Wrong: "Processing one message takes constant time regardless of how many messages arrive."
[OK] Correct: While one message is quick, total time depends on how many messages come in each poll, so more messages mean more total processing time.
Understanding how data collection scales helps you design monitoring systems that stay responsive as load grows, a key skill in real-world DevOps roles.
What if we changed the polling interval to fetch larger batches less often? How would the time complexity change?
Practice
Solution
Step 1: Understand Prometheus function
Prometheus collects metrics by scraping endpoints exposed by applications like Kafka.Step 2: Differentiate roles of Prometheus and Grafana
Grafana is used for visualization, not data collection.Final Answer:
To collect Kafka metrics by scraping the metrics endpoint -> Option AQuick Check:
Prometheus collects metrics = A [OK]
- Confusing Grafana as the collector
- Thinking Prometheus stores Kafka messages
- Assuming Prometheus sends alerts to Kafka
Solution
Step 1: Identify Kafka metrics port
Kafka metrics exporter commonly exposes metrics on port 9090.Step 2: Match correct target in Prometheus config
Only - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:9090'] uses port 9090, which is correct for scraping Kafka metrics.Final Answer:
- job_name: 'kafka'\n static_configs:\n - targets: ['localhost:9090'] -> Option AQuick Check:
Port 9090 for Kafka metrics = A [OK]
- Using Kafka broker port 2181 (Zookeeper port) instead of metrics port
- Confusing HTTP ports like 8080 for metrics
- Using random ports not related to Kafka metrics
sum(kafka_consumer_group_lag) by (group)What output will this query produce?
Solution
Step 1: Understand the metric and aggregation
The metric kafka_consumer_group_lag measures lag per consumer group and partition. Summing by group aggregates lag per group.Step 2: Interpret the query output
The query returns total lag number for each consumer group, not topics or CPU usage.Final Answer:
Total lag per consumer group as a number -> Option BQuick Check:
sum by group = total lag per group [OK]
- Thinking it lists topics instead of groups
- Confusing lag with broker count
- Assuming CPU metrics from Kafka lag metric
Solution
Step 1: Check Prometheus scraping setup
If Kafka metrics endpoint is not exposed or unreachable, Prometheus cannot collect data.Step 2: Understand impact on Grafana
Without data in Prometheus, Grafana dashboards show no metrics.Final Answer:
Kafka metrics endpoint is not exposed or unreachable -> Option CQuick Check:
No data means no scrape target reachable [OK]
- Blaming Grafana connection before checking Prometheus data
- Assuming Kafka broker down always causes no data
- Thinking scrape interval too short causes no data
Solution
Step 1: Identify correct data source for metrics
Prometheus collects Kafka metrics; Grafana connects to Prometheus for queries.Step 2: Choose correct metric names and queries
Common metrics are kafka_broker_cpu_usage and kafka_consumer_group_lag aggregated by group.Final Answer:
Connect Grafana to Prometheus; use queries kafka_broker_cpu_usage and sum(kafka_consumer_group_lag) by (group) -> Option DQuick Check:
Prometheus + correct queries = C [OK]
- Trying to connect Grafana directly to Kafka brokers
- Using wrong metric names or unaggregated lag
- Confusing Kafka Connect with Kafka broker metrics
Ready to prove it?
Pass the test and this pattern moves unknown
