How to Monitor Spring Boot Applications with Prometheus and Grafana
This guide explains how to integrate Prometheus with Spring Boot using Actuator and Micrometer, configure Docker containers, set up Grafana for visualization, and create custom metrics, providing a complete monitoring solution for microservice applications.
Introduction
With the rise of microservice architecture, monitoring services becomes crucial. Prometheus is an open‑source monitoring and alerting tool with powerful data collection, storage and query capabilities. Combining Spring Boot with Prometheus enables real‑time monitoring of Spring Boot applications.
1. Prometheus Overview
Prometheus is an open‑source system monitoring and alerting toolkit that collects and stores metrics, provides a powerful query language, and includes a Prometheus Server as the core component.
Official site: https://prometheus.io/
GitHub: https://github.com/prometheus/prometheus
2. Spring Boot Actuator
Spring Boot Actuator provides a set of endpoints (e.g., /health, /info, /metrics) for exposing internal application information such as health status, configuration, and metrics.
3. Integrating Prometheus with Spring Boot
3.1 Add Dependencies
Add Spring Boot Actuator and Micrometer Prometheus Registry to the project.
<dependencies>
<!-- Spring Boot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.15</version>
</dependency>
<!-- Micrometer Prometheus Registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.9.14</version>
</dependency>
</dependencies>3.2 Configure Actuator
management:
endpoints:
web:
exposure:
include: '*'
metrics:
export:
prometheus:
enabled: trueOther configuration properties:
management.endpoints.web.exposure.include=*
management.metrics.export.prometheus.enabled=true
management.endpoints.web.base-path="/status"
management.endpoints.server.request.metric-name="application:request"
management.server.port=100013.3 Start Prometheus Server
Docker commands to pull and run Prometheus.
docker pull prom/prometheus
docker run --name prometheus -d -p 9090:9090 prom/prometheusMount custom prometheus.yml if needed.
docker run -d --name prometheus -p 9090:9090 -v D:\developsoft\docker\DockerDesktopWSL\data\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheusConfigure prometheus.yml to scrape the Spring Boot application.
scrape_configs:
- job_name: 'spring-boot-application'
metrics_path: 'prometheus-demo/actuator/prometheus'
scrape_interval: 15s
static_configs:
- targets: ['192.168.10.108:8091']Access metrics at /actuator/prometheus.
4. Visualizing Metrics with Grafana
4.1 Install Grafana
docker pull grafana/grafana
docker run -d --name=grafana -p 3000:3000 grafana/grafanaOpen http://localhost:3000 (default credentials admin/admin).
4.2 Configure Data Source
In Grafana, add Prometheus as a data source pointing to the Prometheus server address.
4.3 Create Dashboard
Create a new dashboard, add panels, select Prometheus as the source, and write PromQL queries to visualize metrics.
5. Custom Monitoring Metrics
5.1 Add Custom Metrics
Use Micrometer API to define custom counters.
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
@RestController
public class CustomMetricsController {
private final Counter ordersCounter;
public CustomMetricsController(MeterRegistry registry) {
this.ordersCounter = Counter.builder("orders_count")
.description("The total number of orders")
.register(registry);
}
@GetMapping("/order")
public String createOrder() {
ordersCounter.increment();
return "Order created";
}
}5.2 Display Custom Metrics in Grafana
Custom metrics appear like any other Prometheus metric and can be added to Grafana panels using PromQL queries.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
