Learnitweb

Integrating Spring Boot Actuator and Micrometer with Prometheus

Why Send Metrics to External Systems?

While Micrometer captures and exposes metrics, those metrics are in-memory inside your application.

  • If the application restarts, you lose all previously collected metrics.
  • To persist metrics and analyze them over time (through graphs, alerts, dashboards), you must export them to an external monitoring system.

Some popular external systems include:

  • Prometheus
  • New Relic
  • Datadog
  • Splunk
  • Dynatrace
  • And many others

Overview of the Integration Flow

Here’s the overall idea:

  1. Micrometer collects metrics inside your Spring Boot application.
  2. Micrometer Registry (specific to a vendor like Prometheus) exposes those metrics at a new HTTP endpoint.
  3. Prometheus server scrapes (periodically fetches) the metrics from that HTTP endpoint.
  4. Prometheus then stores the metrics permanently in its database.
  5. You can query, analyze, and visualize these metrics over time.

How to Expose Metrics to Prometheus in Spring Boot

Spring Boot makes this process very simple. You don’t need to manually write any code — just add a dependency and configure a little bit.

Step 1: Add Dependency for Prometheus Registry

You must add the following dependency to your pom.xml if you are using Maven:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

This dependency automatically registers Prometheus as a supported monitoring system inside your application.

Step 2: Enable Spring Boot Actuator

Make sure you already have Spring Boot Actuator added:

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

Actuator is the module responsible for exposing all kinds of useful production-ready features, including metrics.

Step 3: Expose Prometheus Metrics Endpoint

By default, Spring Boot exposes several management endpoints.
However, to explicitly enable the Prometheus metrics endpoint, add the following configuration to your application.properties or application.yml:

# Expose all management endpoints including Prometheus
management.endpoints.web.exposure.include=*

# Set the path for Prometheus metrics scraping
management.endpoint.prometheus.enabled=true

This creates a new endpoint available at:

http://localhost:8080/actuator/prometheus

This URL will display all the metrics in a format that Prometheus server understands.

Step 4: How Prometheus Server Uses It

Later when we configure the Prometheus server, it will be instructed to scrape this /actuator/prometheus endpoint at regular intervals (for example, every 15 seconds).

That way:

  • Your Spring Boot app exposes the metrics
  • Prometheus fetches and stores the metrics permanently
  • You can build dashboards or setup alerts using the stored data