Monitoring Java Applications: Metrics, Alerts, and Best Practices

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: Metrics and Alerts



In modern development, the stability and performance of Java applications directly impact business metrics. Without quality monitoring, it is impossible to timely detect memory leaks, response time degradation, or service failures. In this article, we will explore how to set up monitoring for Java applications using metrics and alerts, starting from basic concepts and ending with practical code examples.



Why Monitoring Java Applications is Critically Important



Java applications run in environments with automatic memory management (Garbage Collection), multithreading, and complex dependencies. Without monitoring, you risk encountering:

  • Long GC pauses (Stop-The-World) that slow down system response;
  • Memory leaks leading to OutOfMemoryError;
  • Slow HTTP requests due to thread blocking;
  • Unexpected failures during peak load hours.

Monitoring allows not only to react to incidents but also to predict problems before they occur.



Key Metrics for Java Applications



For comprehensive health monitoring of an application, it is necessary to collect metrics from three main groups: JVM, application, and business metrics.



JVM Metrics

These metrics show the state of the Java Virtual Machine:

  • Heap Memory Usage — current and maximum size;
  • Thread Count — active, daemon, blocked;
  • GC Statistics — number of garbage collections, pause times, Young/Old GC frequency;
  • Class Loading — number of loaded and unloaded classes.


Application Metrics

Reflect the behavior of your code:

  • Response Time — average, p50, p95, p99;
  • Requests per second — RPS;
  • Error Rate — 4xx, 5xx status codes;
  • Connection Pool Status — active, pending, closed connections.


Business Metrics

Help evaluate the impact of code on the business:

  • Number of successful transactions;
  • Execution time of critical operations (e.g., order placement);
  • Sales funnel conversion (if the application is UI-related).


Tools for Collecting Metrics in Java



The modern monitoring stack for Java applications typically includes Micrometer, Prometheus, and Grafana. Let's look at their integration.



Micrometer — Metrics Facade

Micrometer provides a unified API for collecting metrics that supports many monitoring systems (Prometheus, Datadog, New Relic, etc.). Add the dependency to pom.xml:



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


Example of exporting metrics via the /actuator/prometheus endpoint (for Spring Boot):



import io.micrometer.core.instrument.MeterRegistry;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;

@RestControllerpublic class MetricController {

private final MeterRegistry registry;

public MetricController(MeterRegistry registry) { this.registry = registry; }

@GetMapping("/custom-metric") public String customMetric() { // Create a counter registry.counter("custom.requests.count").increment(); // Timer for measuring duration registry.timer("custom.request.duration").

Blogs

Book Recommendations