Micrometer for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Micrometer for Java: Complete Guide with Examples

\n\n

1. What It Is and Why It's Needed

\n\n

Micrometer is a library for collecting metrics from Java (and JVM-language) applications, providing a facade over popular monitoring systems such as Prometheus, Graphite, Datadog, InfluxDB, New Relic, and others. It implements the Micrometer Observation API specification and is the de facto standard for metrics collection in the Spring Boot ecosystem (starting from version 2.0).

\n\n

Why is it needed? In modern microservice architectures, it is critically important to understand what is happening with the application: how many requests are being processed, what the response latency is, how much memory is being consumed, how often errors occur. Micrometer allows you to unify the collection of this data without being tied to a specific monitoring backend. You write the code once, and switching between Prometheus, Graphite, or CloudWatch can be done by simply changing a dependency in pom.xml or build.gradle.

\n\n

The library provides a rich set of tools: counters (Counter), histograms (Histogram), timers (Timer), distribution summaries (DistributionSummary), as well as support for tags for multidimensional metric aggregation. Built-in integration with the JVM (garbage collection, threads, memory) and popular frameworks (Spring, Hibernate, Jetty, Tomcat) makes it indispensable for production monitoring.

\n\n

2. Installation

\n\n

Micrometer is distributed via Maven Central. For Java projects, Maven or Gradle is used. Below are the exact commands for adding dependencies.

\n\n

Maven (pom.xml)

\n
<!-- Micrometer core module -->\n<dependency>\n    <groupId>io.micrometer</groupId>\n    <artifactId>micrometer-core</artifactId>\n    <version>1.14.5</version> <!-- Check the latest version on mvnrepository.com -->\n</dependency>\n\n<!-- Registry for Prometheus -->\n<dependency>\n    <groupId>io.micrometer</groupId>\n    <artifactId>micrometer-registry-prometheus</artifactId>\n    <version>1.14.5</version>\n</dependency>
\n\n

Gradle (build.gradle)

\n
dependencies {\n    implementation 'io.micrometer:micrometer-core:1.14.5'\n    implementation 'io.micrometer:micrometer-registry-prometheus:1.14.5'\n}
\n\n

Important: If you are using Spring Boot, simply add spring-boot-starter-actuator and micrometer-registry-prometheus — Spring Boot will automatically configure the MeterRegistry.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Let's create a simple console application that collects metrics and exports them in Prometheus format.

\n\n
import io.micrometer.core.instrument.Counter;\nimport io.micrometer.core.instrument.MeterRegistry;\nimport io.micrometer.prometheus.PrometheusConfig;\nimport io.micrometer.prometheus.PrometheusMeterRegistry;\n\npublic class QuickStart {\n    public static void main(String[] args) throws InterruptedException {\n        // 1. Create a metrics registry for Prometheus\n        PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);\n\n        // 2. Create a counter\n        Counter counter = Counter.builder("my.counter")\n                .description("Example counter")\n                .tag("env", "test")\n                .register(registry);\n\n        // 3. Increment the counter in a loop\n        for (int i = 0; i < 5; i++) {\n            counter.increment();\n            Thread.sleep(1000);\n        }\n\n        // 4. Output metrics in Prometheus format\n        System.out.println("=== Prometheus Metrics ===");\n        System.out.println(registry.scrape());\n    }\n}
\n\n

Console output:

\n
=== Prometheus Me                    

Recommendations