Operations 10 min read

Integrating Prometheus with Spring Boot and Visualizing Metrics Using Grafana

This guide explains how to monitor a Spring Boot application using Prometheus, configure Spring Boot Actuator, run Prometheus (including Docker deployment), set up Grafana for visualizing metrics, and create custom metrics with Micrometer, providing step‑by‑step instructions and code examples.

Architect
Architect
Architect
Integrating Prometheus with Spring Boot and Visualizing Metrics Using Grafana

With the rise of micro‑service architectures, monitoring and managing services becomes essential. Prometheus is an open‑source monitoring and alerting toolkit that offers powerful data collection, storage, and query capabilities, while Spring Boot Actuator provides built‑in endpoints for exposing application health, info, and metrics.

The article demonstrates how to combine Prometheus with a Spring Boot application to achieve real‑time monitoring.

1. Prometheus Overview

Prometheus collects and stores metrics, offering a flexible query language (PromQL). Its core component is the Prometheus Server, which scrapes metrics from configured targets.

2. Spring Boot Actuator

Spring Boot Actuator supplies a set of endpoints such as /health , /info , and /metrics that expose internal application data, making it easy to integrate with monitoring systems.

3. Integrating Prometheus and Spring Boot

3.1 Add Dependencies

Include the Actuator and Micrometer Prometheus registry in the project:

<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

Expose the Prometheus endpoint and enable metric export in application.yml :

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

Additional optional properties can customize the base path, request metric name, and server port.

3.3 Start Prometheus

Download the Prometheus server from the official site or run it via Docker:

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

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

3.4 Configure Prometheus

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 host and port with the actual address of the Spring Boot service.

3.5 Access Monitoring Data

After the Spring Boot app starts, Prometheus periodically scrapes the /actuator/prometheus endpoint to collect metrics.

4. Grafana Visualization

Grafana provides richer visualizations for Prometheus data.

4.1 Install Grafana

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

Access Grafana at http://localhost:3000 (default credentials: admin/admin).

4.2 Configure Data Source

In Grafana, add a new data source of type Prometheus and point it to the Prometheus server URL.

4.3 Create Dashboard

Create a dashboard, add panels, and use PromQL queries (e.g., price(http_requests_total[5m]) ) to display desired metrics.

5. Custom Monitoring Metrics

Beyond the built‑in Actuator metrics, you can define custom metrics with Micrometer.

5.1 Add Custom Metric

Example controller that records an orders_count counter:

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

After exposing the custom metric via the Actuator endpoint, it can be queried in Grafana just like any other Prometheus metric.

By following these steps, you can fully monitor a Spring Boot micro‑service, visualize its performance in Grafana, and extend observability with custom business‑specific metrics.

MonitoringDockermetricsPrometheusSpring BootGrafanaActuator
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

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.