Exposing Spring Boot Metrics for Prometheus Monitoring and Visualizing with Grafana

This tutorial explains how to add Actuator and Prometheus dependencies to a Spring Boot application, configure security, run the app to expose Prometheus‑format metrics, and then set up Docker‑based Prometheus and Grafana containers to collect and visualize those metrics.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Exposing Spring Boot Metrics for Prometheus Monitoring and Visualizing with Grafana

First, add the Spring Boot Actuator dependency to the project's pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Next, add the Prometheus client for Spring Boot:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.0.26</version>
</dependency>

In the main class Application.java, enable the required annotations:

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Configure a default user name and password in application.yml:

security:
  user:
    name: user
    password: pwd

After starting the application, the Actuator endpoint /application/prometheus will expose metrics in Prometheus format, which can be accessed with the configured credentials.

To collect these metrics, pull the Prometheus Docker image and create a prometheus.yml configuration file:

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: spring-boot
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /application/prometheus
    scheme: http
    basic_auth:
      username: admin
      password: 123456
    static_configs:
      - targets:
        - 192.168.11.54:8099  # replace with your Spring Boot IP and port

Run Prometheus with the configuration mounted into the container:

docker run -d --name prometheus -p 9090:9090 \
    -v D:\test\actuator\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

Verify the container is running with docker ps and open http://localhost:9090/targets in a browser; the Spring Boot target should appear as up.

Next, pull the Grafana Docker image and start a Grafana container:

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

Access Grafana at http://localhost:3000/, log in with the default credentials (admin / admin), and add a Prometheus data source pointing to http://localhost:9090.

After adding the data source, create dashboards to visualize the collected metrics, such as CPU usage, memory, request latency, etc.

Throughout the tutorial, promotional links to programmer referral groups are included, but they are not part of the technical steps.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavamonitoringDockerPrometheusGrafanaspring-boot
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

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.