Expose Spring Boot Metrics with Prometheus and Visualize Them Using Grafana

This guide shows how to add Spring Boot Actuator dependencies, enable Prometheus endpoints, configure security, run the application, collect metrics with a Prometheus Docker container, and visualize the data through Grafana dashboards.

Top Architect
Top Architect
Top Architect
Expose Spring Boot Metrics with Prometheus and Visualize Them Using Grafana

First, add the Spring Boot Actuator dependency to the project:

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

Then add the Prometheus client library:

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

In the main application 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 login for the actuator endpoints in application.yml:

security:
  user:
    name: user
    password: pwd

After starting the application, the Prometheus‑compatible metrics are available at /application/prometheus. Use the provided username and password to access them.

Next, set up Prometheus using Docker. Pull the image:

$ docker pull prom/prometheus

Create a prometheus.yml configuration file that scrapes the Spring Boot endpoint:

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"]

Run the Prometheus container, mounting the configuration file:

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

Verify the service is up with docker ps and open http://localhost:9090/targets to see the Spring Boot scrape status.

Finally, install Grafana via Docker to visualize the metrics:

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

Log into Grafana (default admin/admin), add a Prometheus data source pointing to http://localhost:9090, and create dashboards to display the collected metrics.

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.

PrometheusGrafanaActuatorspring-boot
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.