Monitoring Java Applications: Metrics and Alerts (Advanced Level)

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 at an Advanced Level



In modern development, monitoring Java applications is no longer an option — it is a necessity. However, basic metric collection (CPU, memory, request count) often fails to detect performance degradation or hidden errors in time. At an advanced level, it is necessary not only to collect data but also to configure intelligent alerts that respond to anomalies rather than static thresholds.



In this article, we will examine the architecture of advanced monitoring for Java: from collecting metrics with Micrometer to configuring alerts in Prometheus and visualization in Grafana. The material is aimed at developers already familiar with the basics of monitoring.



1. Advanced Metric Collection with Micrometer



Micrometer is a metrics facade that integrates with most systems (Prometheus, Datadog, New Relic). At an advanced level, it is important not just to connect the library, but to properly design custom metrics.



Key Components:

  • MeterRegistry — the central metrics store. For Prometheus, use PrometheusMeterRegistry.
  • Counter — a counter (e.g., number of errors).
  • Timer — measures execution time.
  • Gauge — a current value (e.g., queue size).
  • DistributionSummary — value distribution (for percentiles).


Example of a Custom Timer with Tags:



import io.micrometer.core.instrument.MeterRegistry;import io.micrometer.core.instrument.Timer;import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Servicepublic class PaymentService {

private final Timer paymentTimer;

public PaymentService(MeterRegistry registry) { this.paymentTimer = Timer.builder("payment.processing.time") .description("Payment processing time") .publishPercentiles(0.5, 0.95, 0.99) // P50, P95, P99 .publishPercentileHistogram() .register(registry); }

public void processPayment(String userId, double amount) { Timer.Sample sample = Timer.start(); try { // business logic Thread.sleep((long) (Math.random() * 100)); } catch (Exception e) { // error metric } finally { sample.stop(paymentTimer); } }}


Tips for Advanced Collection:

  • Use tags for filtering: endpoint, status, region.
  • Avoid high tag cardinality (e.g., do not add userId).
  • For business metrics, use Counter with an increment on each event.
  • Configure DistributionSummary for response sizes or latencies.


2. Integration with Prometheus and Alert Configuration



Prometheus is a collection and alerting system that uses a pull model. For Java applications, simply add the /actuator/prometheus endpoint (via Spring Boot Actuator + Micrometer).



Configuration in application.yml:



management:  endpoints:    web:      exposure:        include: prometheus, health  metrics:    export:      prometheus:        enabled: true    tags:      application: my-java-app      environment: production


Advanced Alerts in Prometheus (rules.yml):



Static alerts (e.g., CPU > 90%) are good, but often produce false positives. Use dynamic thresholds and anomalies.



groups:  - name: java_app_advanced    rules:\                

Blogs

Book Recommendations