Skip to content

Micrometer

ObservabilityMonitoring / MetricsJava

What it is

Micrometer is a metrics instrumentation library for Java applications that provides a vendor-neutral interface for capturing application metrics and exposing them to monitoring systems. It supports counters, gauges, timers, distribution summaries, and more.

Micrometer provides a consistent API to record metrics like counters, gauges, timers, and distribution summaries. Metrics can be tagged for filtering and organized for export to external monitoring systems.

Installation

Add dependency in pom.xml: <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> <version>1.12.1</version> </dependency>

Getting started

The smallest useful thing you can do with it, and what each part means.

Creating a counter
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Counter;

Counter counter = registry.counter("requests_total");
counter.increment();
Creates a counter metric and increments it to track the total number of requests.
Creating a gauge
import io.micrometer.core.instrument.Gauge;
import java.util.concurrent.atomic.AtomicInteger;

AtomicInteger queueSize = new AtomicInteger(0);
Gauge.builder("queue.size", queueSize, AtomicInteger::get)
     .register(registry);
Creates a gauge metric to monitor the current size of a queue.

Advanced usage

Where the library earns its place over a simpler alternative.

Using a timer
import io.micrometer.core.instrument.Timer;

Timer timer = registry.timer("requests_latency");
timer.record(() -> {
    // code to measure
});
Measures the execution time of a code block and records it as a timer metric.
Distribution summary
import io.micrometer.core.instrument.DistributionSummary;

DistributionSummary summary = DistributionSummary.builder("payload.size").register(registry);
summary.record(1024);
Records numeric values to track distributions, e.g., payload sizes.
Tagging metrics
Counter counter = Counter.builder("requests_total")
    .tags("endpoint", "/api/users", "status", "success")
    .register(registry);
counter.increment();
Adds tags to metrics for filtering and aggregation in monitoring systems.
Publishing to Prometheus
import io.micrometer.prometheus.PrometheusMeterRegistry;

PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
Configures Micrometer to expose metrics in Prometheus format for scraping by monitoring tools.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

MeterAlreadyExistsException
Occurs if a metric with the same name and tags is registered multiple times. Reuse existing metrics or use unique tags.
No registry configured
Ensure a MeterRegistry instance is available and properly configured before registering metrics.

Best practices

  • Instrument all important business and technical metrics using counters, gauges, and timers.
  • Use tags to differentiate metrics by dimensions like endpoint, region, or user.
  • Export metrics to a centralized monitoring system for alerting and visualization.
  • Avoid high-cardinality tags that can overwhelm the monitoring backend.
  • Leverage Micrometer integration with Spring Boot Actuator for automatic metrics collection.

Background

Why it exists, and what it was reacting to.

Micrometer was developed to unify metrics collection in Java applications, particularly for modern microservices architectures. It integrates seamlessly with Spring Boot and provides adapters for popular monitoring systems like Prometheus, Graphite, Datadog, and New Relic. This enables developers to monitor performance, track business metrics, and diagnose issues in production environments.