Video in progress

What is Micrometer, and why don't we write metrics directly with the Prometheus library?

Micrometer is a facade for metrics, similar to SLF4J: in code you write Counter, Timer, Gauge once, and they are sent through a registry to systems like Prometheus, Datadog or CloudWatch.

📖 Answer

So the code doesn't get tied to one monitoring system. Today it's Prometheus, tomorrow the company moves to Datadog: with Micrometer you only swap the registry dependency.

@Service
public class OrderService {
    private final Counter created;

    public OrderService(MeterRegistry registry) {
        this.created = registry.counter("orders.created", "channel", "web");
    }

    public void create(Order o) {
        // ...
        created.increment();
    }
}

Spring Boot configures Micrometer automatically through Actuator: a MeterRegistry bean is ready, and HTTP requests, JVM memory, GC, threads and HikariCP pool metrics are collected on their own. For Prometheus you add micrometer-registry-prometheus and /actuator/prometheus appears.

For naming, Micrometer recommends dots (orders.created). When exporting to Prometheus it automatically turns into orders_created_total.