Under The Hood
backend springboot Spring Boot 3.x / Micrometer 1.x

Spring Actuator: Micrometer Counters, Gauges, Prometheus Scrape

Last updated
Prerequisites:
Spring Boot basics
Understanding of metrics and monitoring concepts
  • spring
  • actuator
  • micrometer
  • metrics
  • prometheus
  • monitoring

Read at your depth

The practical view

Spring Boot Actuator exposes health, metrics, and info endpoints (/actuator/health, /actuator/metrics). Metrics are collected with Micrometer: counters (increment), gauges (current value), timers (duration + count), distribution summaries. Enable Prometheus with the micrometer-registry-prometheus dependency; then /actuator/prometheus exposes metrics in Prometheus format, scraped by Prometheus. Create your own: Counter.builder("orders.created").register(meterRegistry) then counter.increment(). Use @Timed for timers around methods. Health indicators: HealthIndicator beans contribute to /actuator/health (UP/DOWN).

The same idea in other frameworks

react equivalent
N/A — server-side Spring concern.

Legacy vs modern

Log-based metrics vs Micrometer counters

Parsing logs to count events is brittle and lossy; a Micrometer counter gives an exact, queryable metric.

before → after
Counting events via log parsing
log.info("order created id={}", id); // ❌ OLD: counting requires log parsing
Micrometer counter
private final Counter ordersCreated; // ✅ NEW: exact, tagged, scrapable

Interview gotchas

Context

This tests the meter type semantics — interviewers want the monotonic-counter vs current-value distinction and the practical implications for Prometheus queries.

The mechanical answer

A Counter is a monotonically increasing value — it only goes up (it tracks the total number of events since start). In Prometheus, counters are queried with rate()/increase() to derive per-second rates, and they reset on restart (Prometheus handles resets). A Gauge is a current value that can go up and down — a snapshot at scrape time (queue depth, in-flight requests, temperature, memory usage). Choosing wrong causes wrong queries: using a gauge for an event total loses the rate history; using a counter for a current level (e.g. active sessions) produces meaningless rates. Micrometer enforces this conceptually: counters expose increment(), gauges wrap a supplier and are read at scrape time. Timers are a third type — they measure durations and expose count + total + histogram.

Trap

The naive answer is 'counter and gauge are interchangeable'. They have opposite semantics (monotonic total vs current level) and different query patterns. Another trap: creating a gauge that calls an expensive method at every scrape — gauges are read on scrape, so the supplier runs on each Prometheus pull. Candidates who map the meter type to the query pattern (rate vs snapshot) show real monitoring understanding.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.