Prometheus collects data about your app's health and performance. Grafana shows this data in easy-to-understand charts and graphs.
0
0
Prometheus and Grafana integration concept in Spring Boot
Introduction
You want to watch how your Spring Boot app is doing in real time.
You need to find out why your app is slow or crashing.
You want to see how many users are using your app at different times.
You want to get alerts if something goes wrong with your app.
You want to share app performance reports with your team.
Syntax
Spring Boot
1. Add Prometheus dependency to Spring Boot project. 2. Configure Spring Boot to expose metrics at /actuator/prometheus. 3. Set up Prometheus server to scrape metrics from Spring Boot app. 4. Install Grafana and add Prometheus as a data source. 5. Create dashboards in Grafana to visualize metrics.
Spring Boot uses Micrometer to expose metrics in Prometheus format.
Prometheus scrapes metrics by regularly requesting the /actuator/prometheus endpoint.
Examples
Add these dependencies in your build file to enable Prometheus metrics in Spring Boot.
Spring Boot
dependencies {
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}Configure Spring Boot to expose the Prometheus endpoint.
Spring Boot
management.endpoints.web.exposure.include=health,info,prometheus management.endpoint.prometheus.enabled=true
Example Prometheus config to scrape metrics from your Spring Boot app running on localhost port 8080.
Spring Boot
scrape_configs: - job_name: 'springboot-app' static_configs: - targets: ['localhost:8080']
Steps to connect Grafana to Prometheus and create visual dashboards.
Spring Boot
In Grafana UI: - Add Prometheus as data source with URL http://localhost:9090 - Create dashboard and add panels to show metrics like JVM memory, HTTP requests
Sample Program
This setup lets Prometheus collect metrics from your Spring Boot app and Grafana display them.
Spring Boot
1. Add to build.gradle: implementation 'io.micrometer:micrometer-registry-prometheus' implementation 'org.springframework.boot:spring-boot-starter-actuator' 2. In application.properties: management.endpoints.web.exposure.include=health,info,prometheus management.endpoint.prometheus.enabled=true 3. Run Spring Boot app on port 8080. 4. Prometheus config (prometheus.yml): scrape_configs: - job_name: 'springboot-app' static_configs: - targets: ['localhost:8080'] 5. Start Prometheus server. 6. Start Grafana, add Prometheus as data source (http://localhost:9090). 7. Create Grafana dashboard to visualize metrics.
OutputSuccess
Important Notes
Make sure your Spring Boot app allows access to the /actuator/prometheus endpoint.
Prometheus and Grafana usually run on ports 9090 and 3000 respectively by default.
Use Grafana's pre-built dashboards for Spring Boot and Micrometer to save time.
Summary
Prometheus collects app metrics by scraping an endpoint.
Grafana connects to Prometheus to show these metrics visually.
Spring Boot apps expose metrics easily using Micrometer and Actuator.