Integracja z Prometheus i Grafana
Skonfiguruj Prometheus do pobierania metryk z Kafka i aplikacji Spring Boot, a następnie wizualizuj je za pomocą pulpitów Grafana.
Integracja z Prometheus i Grafana to bezpłatna lekcja Advanced Spring Boot 4: Event-Driven Architecture (Kafka) na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Advanced Spring Boot 4: Event-Driven Architecture (Kafka), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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!
Często zadawane pytania
Czy lekcja „Integracja z Prometheus i Grafana” jest bezpłatna?
Tak — pełny tekst „Integracja z Prometheus i Grafana” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Advanced Spring Boot 4: Event-Driven Architecture (Kafka), przejdź na CoddyKit PRO. Kurs Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zawiera 4 lekcji w sumie.
Co nauczysz się w „Integracja z Prometheus i Grafana”?
Skonfiguruj Prometheus do pobierania metryk z Kafka i aplikacji Spring Boot, a następnie wizualizuj je za pomocą pulpitów Grafana. Ćwiczysz Advanced Spring Boot 4: Event-Driven Architecture (Kafka) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Nie wymagamy żadnego doświadczenia. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Integracja z Prometheus i Grafana”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Tak. Każda lekcja Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Metryki Kafka (JMX) i testy stanu
- Integracja z Prometheus i Grafana
- Śledzenie rozproszone za pomocą Sleuth/Zipkin
- Monitorowanie opóźnienia konsumenta i konfigurowanie alertów