0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Leçon

Intégration avec Prometheus et Grafana

Configurez Prometheus pour collecter les métriques de Kafka et des applications Spring Boot, puis visualisez-les à l’aide de tableaux de bord Grafana.

Intégration avec Prometheus et Grafana est une leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced Spring Boot 4: Event-Driven Architecture (Kafka), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka) comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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,prometheus

This exposes metrics at /actuator/prometheus.

management.endpoints.web.exposure.include=health,info,prometheus

Your 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 port

Introducing 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_total for 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!

Questions Fréquemment Posées

La leçon « Intégration avec Prometheus et Grafana » est-elle gratuite ?

Oui — le texte complet de « Intégration avec Prometheus et Grafana » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka), passe à CoddyKit PRO. Le cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka) comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Intégration avec Prometheus et Grafana » ?

Configurez Prometheus pour collecter les métriques de Kafka et des applications Spring Boot, puis visualisez-les à l’aide de tableaux de bord Grafana. Tu pratiques Advanced Spring Boot 4: Event-Driven Architecture (Kafka) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ?

Aucune expérience préalable n'est requise. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « Intégration avec Prometheus et Grafana » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ?

Oui. Chaque leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Métriques Kafka (JMX) et vérifications d’état
  2. Intégration avec Prometheus et Grafana
  3. Traçage distribué avec Sleuth et Zipkin
  4. Surveiller le retard des consommateurs et configurer des alertes
← Retour à Advanced Spring Boot 4: Event-Driven Architecture (Kafka)