Bird
Raised Fist0
Kafkadevops~10 mins

Prometheus and Grafana integration in Kafka - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Prometheus and Grafana integration
Start Kafka Exporter
↓
Prometheus Scrapes Metrics
↓
Store Metrics in Prometheus DB
↓
Grafana Queries Prometheus
↓
Grafana Visualizes Metrics
↓
User Views Dashboards
Metrics flow from Kafka exporter to Prometheus, then Grafana queries Prometheus to display dashboards for users.
Execution Sample
Kafka
kafka_exporter --kafka.server=localhost:9092
prometheus.yml:
  scrape_configs:
    - job_name: 'kafka'
      static_configs:
        - targets: ['localhost:9308']
Start Kafka exporter, configure Prometheus to scrape metrics from it.
Process Table
StepActionComponentResultNext Step
1Start Kafka ExporterKafka ExporterExporter runs on port 9308, exposing metricsPrometheus scrapes metrics
2Prometheus scrapes metricsPrometheusFetches metrics from localhost:9308Store metrics in DB
3Store metricsPrometheus DBMetrics stored with timestampsGrafana queries Prometheus
4Grafana queries PrometheusGrafanaQueries metrics via Prometheus APIVisualize metrics
5Visualize metricsGrafana DashboardDisplays Kafka metrics graphsUser views dashboard
6User views dashboardUserSees real-time Kafka metricsEnd
💡 User views dashboard; monitoring cycle complete
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Kafka Exporter StatusStoppedRunningRunningRunningRunningRunningRunning
Prometheus MetricsNoneNoneFetched metricsStored metricsStored metricsStored metricsStored metrics
Grafana Query ResultNoneNoneNoneMetrics dataMetrics dataMetrics dataMetrics data
Dashboard DisplayEmptyEmptyEmptyEmptyGraphs shownGraphs shownGraphs shown
Key Moments - 3 Insights
Why does Prometheus need to scrape metrics instead of Kafka pushing them?
Prometheus uses a pull model to control when and how often it collects metrics, as shown in execution_table step 2 where Prometheus actively fetches metrics.
What role does Kafka Exporter play in this integration?
Kafka Exporter exposes Kafka metrics in a format Prometheus can scrape, as seen in step 1 where it runs and exposes metrics on port 9308.
How does Grafana get the data to display dashboards?
Grafana queries Prometheus's stored metrics via its API (step 4), then visualizes them in dashboards (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Prometheus store the scraped metrics?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Result' column for step 3 describing metrics storage.
According to variable_tracker, what is the state of Grafana Query Result after step 4?
ANone
BMetrics data
CEmpty
DStored metrics
💡 Hint
Look at the 'Grafana Query Result' row under 'After Step 4' column.
If Kafka Exporter fails to start, which step in execution_table would not occur?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Prometheus scrapes metrics only after exporter runs (step 1), see execution_table step 2.
Concept Snapshot
Prometheus and Grafana integration:
- Kafka Exporter exposes Kafka metrics
- Prometheus scrapes metrics via pull
- Metrics stored in Prometheus DB
- Grafana queries Prometheus API
- Grafana visualizes metrics on dashboards
- Users monitor Kafka health visually
Full Transcript
This visual execution shows how Kafka metrics flow from Kafka Exporter to Prometheus and then to Grafana. First, Kafka Exporter runs and exposes metrics on port 9308. Prometheus scrapes these metrics regularly and stores them in its database. Grafana queries Prometheus to get the stored metrics and displays them on dashboards. Users then view these dashboards to monitor Kafka's performance and health in real time.

Practice

(1/5)
1. What is the main role of Prometheus in Kafka monitoring when integrated with Grafana?
easy
A. To collect Kafka metrics by scraping the metrics endpoint
B. To display visual dashboards of Kafka metrics
C. To send alerts directly to Kafka topics
D. To store Kafka messages for backup

Solution

  1. Step 1: Understand Prometheus function

    Prometheus collects metrics by scraping endpoints exposed by applications like Kafka.
  2. Step 2: Differentiate roles of Prometheus and Grafana

    Grafana is used for visualization, not data collection.
  3. Final Answer:

    To collect Kafka metrics by scraping the metrics endpoint -> Option A
  4. Quick Check:

    Prometheus collects metrics = A [OK]
Hint: Remember: Prometheus collects, Grafana displays [OK]
Common Mistakes:
  • Confusing Grafana as the collector
  • Thinking Prometheus stores Kafka messages
  • Assuming Prometheus sends alerts to Kafka
2. Which of the following is the correct Prometheus scrape configuration snippet to monitor Kafka metrics exposed at port 9090?
easy
A. - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:9090']
B. - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:2181']
C. - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:8080']
D. - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:1234']

Solution

  1. Step 1: Identify Kafka metrics port

    Kafka metrics exporter commonly exposes metrics on port 9090.
  2. 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.
  3. Final Answer:

    - job_name: 'kafka'\n static_configs:\n - targets: ['localhost:9090'] -> Option A
  4. Quick Check:

    Port 9090 for Kafka metrics = A [OK]
Hint: Kafka metrics usually on port 9090, check targets carefully [OK]
Common Mistakes:
  • 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
3. Given this Prometheus query to monitor Kafka consumer lag:
sum(kafka_consumer_group_lag) by (group)
What output will this query produce?
medium
A. Current CPU usage of Kafka brokers
B. Total lag per consumer group as a number
C. Number of active Kafka brokers
D. List of all Kafka topics with their lag

Solution

  1. 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.
  2. Step 2: Interpret the query output

    The query returns total lag number for each consumer group, not topics or CPU usage.
  3. Final Answer:

    Total lag per consumer group as a number -> Option B
  4. Quick Check:

    sum by group = total lag per group [OK]
Hint: sum(... by (group)) means total per group [OK]
Common Mistakes:
  • Thinking it lists topics instead of groups
  • Confusing lag with broker count
  • Assuming CPU metrics from Kafka lag metric
4. You configured Prometheus to scrape Kafka metrics but Grafana shows no data. Which is the most likely cause?
medium
A. Prometheus scrape interval is too short
B. Grafana dashboard is not connected to Prometheus
C. Kafka metrics endpoint is not exposed or unreachable
D. Kafka broker is down

Solution

  1. Step 1: Check Prometheus scraping setup

    If Kafka metrics endpoint is not exposed or unreachable, Prometheus cannot collect data.
  2. Step 2: Understand impact on Grafana

    Without data in Prometheus, Grafana dashboards show no metrics.
  3. Final Answer:

    Kafka metrics endpoint is not exposed or unreachable -> Option C
  4. Quick Check:

    No data means no scrape target reachable [OK]
Hint: No data? Check if metrics endpoint is reachable first [OK]
Common Mistakes:
  • Blaming Grafana connection before checking Prometheus data
  • Assuming Kafka broker down always causes no data
  • Thinking scrape interval too short causes no data
5. You want to create a Grafana dashboard showing Kafka broker CPU usage and consumer lag side by side. Which data sources and queries should you use?
hard
A. Connect Grafana to Prometheus; use queries kafka_cpu_usage and kafka_topic_lag
B. Connect Grafana directly to Kafka; use queries kafka_cpu and kafka_lag
C. Connect Grafana to Kafka Connect; use queries cpu_usage and consumer_lag
D. Connect Grafana to Prometheus; use queries kafka_broker_cpu_usage and sum(kafka_consumer_group_lag) by (group)

Solution

  1. Step 1: Identify correct data source for metrics

    Prometheus collects Kafka metrics; Grafana connects to Prometheus for queries.
  2. Step 2: Choose correct metric names and queries

    Common metrics are kafka_broker_cpu_usage and kafka_consumer_group_lag aggregated by group.
  3. Final Answer:

    Connect Grafana to Prometheus; use queries kafka_broker_cpu_usage and sum(kafka_consumer_group_lag) by (group) -> Option D
  4. Quick Check:

    Prometheus + correct queries = C [OK]
Hint: Grafana queries Prometheus metrics, not Kafka directly [OK]
Common Mistakes:
  • Trying to connect Grafana directly to Kafka brokers
  • Using wrong metric names or unaggregated lag
  • Confusing Kafka Connect with Kafka broker metrics
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard