Bird
Raised Fist0
Kafkadevops~30 mins

Prometheus and Grafana integration in Kafka - Mini Project: Build & Apply

Choose your learning style10 modes available
Prometheus and Grafana Integration with Kafka Metrics
📖 Scenario: You are working in a team that manages Kafka clusters. Your team wants to monitor Kafka metrics to ensure the system runs smoothly. You will set up Prometheus to scrape Kafka metrics and configure Grafana to visualize these metrics.
🎯 Goal: Build a simple Kafka metrics monitoring setup by configuring Prometheus to scrape Kafka JMX exporter metrics and create a Grafana dashboard to visualize the metrics.
📋 What You'll Learn
Create a Prometheus scrape configuration for Kafka JMX exporter
Define a Kafka JMX exporter endpoint variable
Write the Prometheus scrape job configuration for Kafka metrics
Print the final Prometheus scrape configuration
💡 Why This Matters
🌍 Real World
Monitoring Kafka clusters is essential to detect issues early and maintain system health. Prometheus and Grafana are popular tools for collecting and visualizing these metrics.
💼 Career
DevOps engineers and site reliability engineers often configure Prometheus and Grafana to monitor Kafka and other services to ensure uptime and performance.
Progress0 / 4 steps
1
Create Kafka JMX Exporter Endpoint Variable
Create a variable called kafka_jmx_exporter_endpoint and set it to the string "localhost:9404" which is the Kafka JMX exporter metrics endpoint host and port.
Kafka
Hint

This variable holds the host and port where Prometheus will scrape Kafka metrics.

2
Define Prometheus Scrape Job Configuration
Create a variable called prometheus_scrape_config and assign it a dictionary with a key scrape_configs that contains a list with one dictionary. This dictionary should have job_name set to "kafka" and static_configs set to a list with one dictionary containing targets set to a list with the kafka_jmx_exporter_endpoint variable.
Kafka
Hint

This dictionary configures Prometheus to scrape metrics from the Kafka JMX exporter endpoint.

3
Add Prometheus Scrape Interval Configuration
Add a key scrape_interval with the value "15s" to the prometheus_scrape_config dictionary to set the scrape interval to 15 seconds.
Kafka
Hint

The scrape interval tells Prometheus how often to collect metrics.

4
Print the Prometheus Scrape Configuration
Write a print statement to display the prometheus_scrape_config dictionary.
Kafka
Hint

Printing the configuration helps verify it is correct before using it in Prometheus.

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