Integrating with Prometheus & Grafana
Set up Prometheus to scrape metrics from Kafka and Spring Boot apps, then visualize them using Grafana dashboards.
Integrating with Prometheus & Grafana is a free Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Monitor with P&G?
In modern systems, understanding what your applications and infrastructure are doing is critical. This is where monitoring comes in!
We'll explore how Prometheus and Grafana form a powerful duo for observing your Spring Boot Kafka applications and the Kafka cluster itself.
Introducing Prometheus
Prometheus is an open-source monitoring system that collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts.
- It uses a pull model: Prometheus scrapes (pulls) metrics from your services.
- It stores data as time-series data, making it excellent for tracking changes over time.
Spring Boot & Micrometer
Spring Boot applications can expose metrics using Actuator, Spring's production-ready features. Micrometer is an instrumentation library that Actuator uses to provide a vendor-neutral metrics facade.
This allows you to export metrics to various monitoring systems, including Prometheus, with minimal code changes.
Enabling Prometheus Endpoint
To make your Spring Boot app's metrics available for Prometheus, you need to add dependencies and configure Actuator.
First, add spring-boot-starter-actuator and micrometer-registry-prometheus to your pom.xml.
Then, enable the Prometheus endpoint in application.properties:
management.endpoints.web.exposure.include=health,info,prometheusThis exposes metrics at /actuator/prometheus.
management.endpoints.web.exposure.include=health,info,prometheusYour First Metric
This minimal Spring Boot application demonstrates how to expose a custom counter. When you hit the /hello endpoint, the app.hello.requests counter increments, which Prometheus can then scrape.
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MetricsApp {
private final Counter helloCounter =
Metrics.counter("app.hello.requests");
public static void main(String[] args) {
SpringApplication.run(MetricsApp.class, args);
}
@GetMapping("/hello")
public String hello() {
helloCounter.increment();
return "Hello CoddyKit!";
}
}Kafka Metrics with Exporter
Kafka brokers don't natively expose metrics in a Prometheus-friendly format. We use a separate component called Kafka Exporter.
Kafka Exporter is a lightweight service that scrapes JMX metrics from Kafka brokers and re-exposes them in a format that Prometheus can easily consume.
Prometheus Scrape Config
Prometheus needs to know where to find your metrics. You configure scrape targets in its prometheus.yml file. Here's an example:
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
- job_name: 'kafka-exporter'
static_configs:
- targets: ['localhost:9308'] # Kafka Exporter portIntroducing Grafana
Grafana is an open-source platform for monitoring and observability. It allows you to query, visualize, alert on, and understand your metrics no matter where they are stored.
Think of it as the display panel for all the data Prometheus collects. It uses dashboards with various panels to present metrics in an easy-to-understand way.
Grafana Data Sources
Before visualizing, Grafana needs to connect to a data source. Here, Prometheus will be our data source.
In Grafana's UI, navigate to "Connections" -> "Data sources" -> "Add new data source". Select "Prometheus" and configure its URL (e.g., http://localhost:9090, Prometheus' default port).
Creating a Dashboard
With Prometheus connected, you can start building dashboards.
- Create a new dashboard and add a panel.
- Select Prometheus as the data source.
- Write a PromQL query (Prometheus Query Language) to select your metric, e.g.,
app_hello_requests_totalfor our Spring Boot app. - Choose a visualization type like "Graph" or "Stat".
This brings your metrics to life!
Prometheus & Grafana Check
You've learned how Prometheus scrapes metrics and Grafana visualizes them. What is the primary role of Kafka Exporter in this monitoring setup?
Recap: P&G Power
Great job! You've learned how to integrate Prometheus and Grafana into your monitoring strategy for Spring Boot Kafka applications and Kafka itself.
- Prometheus scrapes metrics from endpoints.
- Micrometer/Actuator expose Spring Boot metrics.
- Kafka Exporter makes Kafka broker metrics available.
- Grafana visualizes these collected metrics.
This powerful combination provides deep insights into your system's health and performance!
Frequently asked questions
Is the “Integrating with Prometheus & Grafana” lesson free?
Yes — the full text of “Integrating with Prometheus & Grafana” is free to read here on the web, and the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced Spring Boot 4: Event-Driven Architecture (Kafka) course, upgrade to CoddyKit PRO.
What will I learn in “Integrating with Prometheus & Grafana”?
Set up Prometheus to scrape metrics from Kafka and Spring Boot apps, then visualize them using Grafana dashboards. You practise Advanced Spring Boot 4: Event-Driven Architecture (Kafka) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
No prior experience is required. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Integrating with Prometheus & Grafana” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson?
Yes. Every Advanced Spring Boot 4: Event-Driven Architecture (Kafka) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Kafka Metrics (JMX) and Health Checks
- Integrating with Prometheus & Grafana
- Distributed Tracing with Sleuth/Zipkin
- Monitoring Consumer Lag and Setting Alerts