BackendBench
Coffee

Unravel › Java concepts › Cloud and operations

Errors and logging

Metrics with Micrometer

Micrometer is the metrics facade of Spring Boot.

🔎 Unravel this example

At a glance

Mental model

The instruments on a factory line: a counter on the conveyor belt, a stopwatch per item, and a gauge showing how full the bin is.

What happens, in order
  1. 1A Counter only goes up (orders placed, errors)registry.counter("orders.placed", "channel", "web").increment() counts one event with a tag.
  2. 2A Timer records how long something took and how often: timer.record(() -> work())It yields count, total time, max and percentiles. @Timed on a method does the same.
  3. 3A Gauge reports a value that goes up and down, read when scraped (queue size, active sessions)Gauges hold only a weak reference to the object they watch.
  4. 4Tags (dimensions) split a metric, for example by status or endpointBoot also instruments HTTP requests, the JVM, connection pools and caches automatically.
Remember
Watch out
High-cardinality tags such as user idsCreating a gauge on a local variable that is garbage collectedTiming only the happy pathUsing a counter for something that can decrease
Example (highlighted lines explain it)
public class CheckoutService {    private final Counter placed;    private final Timer latency;    public CheckoutService(MeterRegistry registry) {        this.placed = registry.counter("orders.placed", "channel", "web");        this.latency = registry.timer("checkout.latency");    }    public Receipt checkout(Cart cart) {        return latency.record(() -> {            Receipt r = process(cart);            placed.increment();            return r;        });    }}
  1. line 5 uses MeterRegistry, the core of Metrics with Micrometer
Read the full explanation (about 2 min)

Micrometer is the metrics facade of Spring Boot. You count events, time operations and expose current values, and a monitoring system (Prometheus, Datadog, CloudWatch) draws graphs and alerts from them.

A mental model

The instruments on a factory line: a counter on the conveyor belt, a stopwatch per item, and a gauge showing how full the bin is. Micrometer is the standard socket that fits any monitoring screen.

How it works

A Counter only goes up (orders placed, errors). registry.counter("orders.placed", "channel", "web").increment() counts one event with a tag.

A Timer records how long something took and how often: timer.record(() -> work()). It yields count, total time, max and percentiles. @Timed on a method does the same.

A Gauge reports a value that goes up and down, read when scraped (queue size, active sessions). Gauges hold only a weak reference to the object they watch.

Tags (dimensions) split a metric, for example by status or endpoint. Boot also instruments HTTP requests, the JVM, connection pools and caches automatically.

Going deeper

Never use unbounded values (user id, URL with ids, order number) as tag values. Each distinct combination is a separate time series, and cardinality explosions crash monitoring systems and cost money.

Prefer counts and timers over logging for numbers you want to alert on; logs answer "what happened", metrics answer "how often, how slow".

Percentile histograms (publishPercentileHistogram) let the monitoring server compute quantiles across instances; client-side percentiles cannot be averaged correctly.

Under the hood

A MeterRegistry implementation per backend translates the common meters to its format. A composite registry fans out to several. Meters are looked up by name and tags in a concurrent map, so creating the same meter repeatedly is cheap but should still be cached in a field on hot paths.

Common mistakes

  • High-cardinality tags such as user ids.
  • Creating a gauge on a local variable that is garbage collected.
  • Timing only the happy path.
  • Using a counter for something that can decrease.

How Unravel recognises it

When you paste code, Unravel looks for these signs of metrics with micrometer. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Metrics with Micrometer?

Micrometer is the metrics facade of Spring Boot. You count events, time operations and expose current values, and a monitoring system (Prometheus, Datadog, CloudWatch) draws graphs and alerts from them.

What are the most common mistakes with Metrics with Micrometer?

High-cardinality tags such as user ids. Creating a gauge on a local variable that is garbage collected. Timing only the happy path. Using a counter for something that can decrease.

How can I see Metrics with Micrometer in real code?

Paste your own Java into Unravel and it will point out where metrics with micrometer appears, explain how it works at the detail level you choose, and draw the flow of the code around it.