Micrometer for Java: Complete Guide with Examples
\n\n1. What It Is and Why It's Needed
\n\nMicrometer 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\nWhy 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.
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\n2. Installation
\n\nMicrometer is distributed via Maven Central. For Java projects, Maven or Gradle is used. Below are the exact commands for adding dependencies.
\n\nMaven (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\nGradle (build.gradle)
\ndependencies {\n implementation 'io.micrometer:micrometer-core:1.14.5'\n implementation 'io.micrometer:micrometer-registry-prometheus:1.14.5'\n}\n\nImportant: If you are using Spring Boot, simply add spring-boot-starter-actuator and micrometer-registry-prometheus — Spring Boot will automatically configure the MeterRegistry.
3. Quick Start — Minimal Working Example
\n\nLet's create a simple console application that collects metrics and exports them in Prometheus format.
\n\nimport 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\nConsole output:
\n=== Prometheus Me Also in library
PDF Generation in C# (.NET): Complete Guide to Libraries
Parsing HTML in C# (.NET): HtmlAgilityPack and AngleSharp Libraries
Cryptography in Java: Encryption and Hashing – A Practical Guide
Working with XML in PHP: A Complete Guide to Parsing and Creating XML Documents
Prost for Rust: Complete Guide with Examples