Spring Boot: From Basics to Production · Observability with Actuator and Micrometer
Metrics with Micrometer and Prometheus
Measure what matters with timers, counters and observations.
Micrometer is Spring Boot's metrics facade (like SLF4J for metrics). Boot auto-instruments HTTP requests, JVM, database pools and more; add micrometer-registry-prometheus to expose /actuator/prometheus. Record business metrics with Counter, Timer and DistributionSummary, or the Observation API (which also produces traces). Keep tag cardinality low.
@Service
class CheckoutMetrics {
private final Counter ordersPlaced;
private final Timer paymentTimer;
CheckoutMetrics(MeterRegistry registry) {
this.ordersPlaced = Counter.builder("shop.orders.placed").description("Orders placed").register(registry);
this.paymentTimer = Timer.builder("shop.payment.duration").publishPercentileHistogram().register(registry);
}
PaymentResult timedCharge(Supplier<PaymentResult> charge) {
PaymentResult result = paymentTimer.record(charge);
ordersPlaced.increment();
return result;
}
}
Common pitfalls
- Tags with user IDs or order IDs (cardinality explosion).
- Only averages — enable histograms for percentiles.
- Measuring everything except business outcomes.
Key points
- Micrometer auto-instruments common components
- Custom business metrics
- Low-cardinality tags; histograms for percentiles
Further reading: Spring Boot: Metrics
Practice questions
Why shouldn't you tag a metric with userId?
Answer
Each unique tag value creates a separate time series; per-user tags create millions of series, overwhelming the metrics backend.
What does publishPercentileHistogram() enable?
Answer
It publishes histogram buckets so the backend (e.g., Prometheus) can compute percentiles like p95/p99 across instances.
Open in the app to mark lessons complete, take notes and track your progress.