Monitoring Java Applications: Micrometer Library and Integration with Prometheus

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

Monitoring Java Applications: Micrometer Library



Modern Java applications require constant monitoring of performance, memory consumption, response time, and other critical parameters. Micrometer is the de facto standard library for collecting metrics in the Java ecosystem, providing a universal API and supporting data export to popular monitoring systems such as Prometheus, Graphite, and Datadog.



What is Micrometer and why is it needed?



Micrometer is a facade for collecting metrics, similar to how SLF4J is a facade for logging. It abstracts application code from the specific monitoring system, allowing you to switch between backends without changing business logic.



Main features of the library:

  • Counters — for counting events (e.g., number of requests).
  • Timers — for measuring operation execution time.
  • Gauges — for tracking current values (e.g., thread pool size).
  • Distribution Summaries — for analyzing value distributions (e.g., request body size).
  • Tags — for adding dimensions to metrics (e.g., endpoint, status).


Installation and Configuration



To use Micrometer in a Maven project, add the following dependency to pom.xml:



<dependency>    <groupId>io.micrometer</groupId>    <artifactId>micrometer-core</artifactId>    <version>1.12.0</version></dependency>


If you are using Spring Boot, simply add the micrometer-registry-prometheus starter:



<dependency>    <groupId>io.micrometer</groupId>    <artifactId>micrometer-registry-prometheus</artifactId>    <version>1.12.0</version></dependency>


After this, Spring Boot will automatically configure the MeterRegistry and expose the /actuator/prometheus endpoint for metric collection.



Usage Examples



1. Creating a Request Counter



A simple example of counting the number of method calls:



import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.MeterRegistry;

public class RequestCounter { private final Counter requestCounter;

public RequestCounter(MeterRegistry registry) { this.requestCounter = Counter.builder("api.requests") .tag("endpoint", "/users") .description("Total number of requests to /users") .register(registry); }

public void handleRequest() { requestCounter.increment(); // business logic }}


2. Measuring Execution Time with a Timer



Use Timer.Sample to measure the execution time of a code block:



import io.micrometer.core.instrument.Timer;import io.micrometer.core.instrument.MeterRegistry;

public class RequestTimer { private final Timer requestTimer;

public RequestTimer(MeterRegistry registry) { this.requestTimer = Timer.builder("api.response.time") .tag("endpoint", "/data") .publishPercentiles(0.95, 0.99) .register(registry); }

public String processRequest() { Timer.Sample sample = Timer.start(registry); try { // simulate delay Thread.sleep(100); return "OK"; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return "ERROR"; } finally

Recommendations