How does Micrometer integrate with Spring Boot for metrics, and what backends can it export to?

8 minintermediatemicrometermetricsprometheus

Quick Answer

Micrometer is a vendor-neutral metrics instrumentation facade — similar in spirit to SLF4J for logging — that Spring Boot auto-configures automatically once spring-boot-starter-actuator is present, capturing built-in metrics (HTTP request timings, JVM memory/GC stats, data source pool usage) and letting application code register custom counters/timers/gauges through the same API regardless of which monitoring backend is actually used. Adding a specific 'micrometer-registry-X' dependency (Prometheus, Datadog, New Relic, CloudWatch, and many others) is all that's needed to export the same collected metrics to that backend, without changing any instrumentation code.

Detailed Answer

Micrometer is a vendor-neutral metrics facade — conceptually similar to how SLF4J decouples application logging calls from a specific logging backend (Logback, Log4j2). Application code instruments metrics through Micrometer's API, and a separate, swappable registry determines which actual monitoring backend those metrics get exported to.

Auto-configuration: as soon as spring-boot-starter-actuator is on the classpath, Spring Boot automatically wires up Micrometer and starts capturing a substantial set of built-in metrics with zero extra code:

  • HTTP request counts, latencies, and status-code breakdowns per endpoint (http.server.requests).
  • JVM metrics — heap/non-heap memory usage, garbage collection pause counts/durations, thread counts.
  • Data source connection pool metrics (active/idle connections, wait time) for HikariCP.
  • Cache hit/miss rates, if a supported caching provider is in use.

Registering custom application metrics:

@Service
class OrderService {
    private final Counter ordersPlacedCounter;
    private final Timer orderProcessingTimer;

    OrderService(MeterRegistry registry) {
        this.ordersPlacedCounter = registry.counter("orders.placed");
        this.orderProcessingTimer = registry.timer("orders.processing.time");
    }

    void placeOrder(Order order) {
        orderProcessingTimer.record(() -> {
            // ... process the order ...
            ordersPlacedCounter.increment();
        });
    }
}

Exporting to a specific backend just means adding the corresponding registry dependency — the instrumentation code above never needs to change regardless of which backend is chosen:

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

This automatically exposes an additional endpoint, /actuator/prometheus, in the text-based exposition format Prometheus's scraper expects. Similar registry dependencies exist for Datadog, New Relic, CloudWatch, Graphite, InfluxDB, and many other monitoring platforms — the same underlying metrics, exported through whichever registry/registries are on the classpath, potentially to multiple backends simultaneously if more than one registry dependency is present.

Why the facade design matters: it lets application code (and, importantly, all of Spring Boot's own built-in auto-generated metrics) remain completely decoupled from any specific monitoring vendor — switching from, say, an on-prem Prometheus setup to a hosted Datadog account (a real, not-uncommon migration) requires only a dependency and configuration change, not rewriting instrumentation scattered throughout the codebase.

Related Resources