Operations 10 min read

Integrating Prometheus with Spring Boot for Real‑time Monitoring and Grafana Visualization

This article explains how to use Prometheus together with Spring Boot Actuator and Micrometer to collect, expose, and visualize application metrics, including step‑by‑step dependency configuration, YAML settings, Docker deployment of Prometheus and Grafana, and adding custom metrics for comprehensive monitoring.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Integrating Prometheus with Spring Boot for Real‑time Monitoring and Grafana Visualization

Introduction

With the rise of micro‑service architecture, monitoring and management of services have become essential. Prometheus, an open‑source monitoring and alerting system, is popular for its powerful data collection, storage, and query capabilities. Spring Boot, a fast‑development framework for Java micro‑services, can be combined with Prometheus to achieve 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 . It provides a powerful query language for analyzing application behavior. The core component is the Prometheus Server , which gathers metrics and offers a query interface.

Official website: https://prometheus.io/

GitHub repository: https://github.com/prometheus/prometheus

2. Spring Boot Actuator

Spring Boot Actuator supplies a set of endpoints ( endpoints ) such as /health , /info , and /metrics that expose internal information like health status, configuration, and metric data.

3. Integrating Prometheus and Spring Boot

3.1 Add Dependencies

Add Spring Boot Actuator and Micrometer Prometheus Registry to the project dependencies:

<dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.7.15</version>
    </dependency>
<dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>1.9.14</version>
    </dependency>
</dependencies>

3.2 Configure Actuator

In application.yml expose the Prometheus endpoint and enable the exporter:

management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    export:
      prometheus:
        enabled: true

Other optional properties (example):

management.endpoints.web.exposure.include=*   # expose all endpoints
management.metrics.export.prometheus.enabled=true   # enable Prometheus exporter
management.endpoints.web.base-path="/status"   # change base path to avoid guessing
management.endpoints.server.request.metric-name="application:request"   # custom metric name
management.server.port=10001   # custom port, default same as server.port

3.3 Start Prometheus Server

Download and run the Prometheus server (or use Docker):

docker pull prom/prometheus

Create and run the container:

docker run --name prometheus -d -p 9090:9090 prom/prometheus

For custom configuration, mount a local prometheus.yml into the container:

docker run -d --name prometheus -p 9090:9090 -v D:\developsoft\docker\DockerDesktopWSL\data\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

Access the UI at http://localhost:9090 .

3.4 Configure Prometheus

Copy the default prometheus.yml from the container to the host, then edit it to add the Spring Boot application as a scrape target:

scrape_configs:
  - job_name: 'spring-boot-application'
    metrics_path: 'prometheus-demo/actuator/prometheus'
    scrape_interval: 15s
    static_configs:
      - targets: ['192.168.10.108:8091']

Replace the address with the actual host and port of your Spring Boot service.

3.5 Access Metrics

After the Spring Boot application starts, Prometheus will periodically scrape the /actuator/prometheus endpoint to collect metric data.

4. Grafana Visualization

Although Prometheus provides basic querying and visualization, Grafana is commonly used for richer dashboards.

4.1 Install Grafana

docker pull grafana/grafana
docker run -d --name=grafana -p 3000:3000 grafana/grafana

Open http://localhost:3000 (default login: admin/admin).

4.2 Add Data Source

In Grafana, configure 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 data source, and write PromQL queries (e.g., price(http_requests_total[5m]) ) to visualize metrics.

5. Custom Monitoring Metrics

Beyond the built‑in metrics from Spring Boot Actuator, you can add custom metrics using Micrometer.

5.1 Add Custom Metric

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 Metric in Grafana

Custom metrics are exposed to Prometheus in the same way as built‑in metrics and can be visualized in Grafana using standard PromQL queries.

--- End of article ---

monitoringPrometheusSpring BootGrafanaActuatorMicrometer
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.