avatarSzilárd Mátis

Summary

The article provides a guide on monitoring Java metrics using Prometheus and Grafana, with a focus on configuring a Java service for metric exposure and integrating it with pre-existing monitoring tools.

Abstract

This technical guide outlines the process of setting up a monitoring system for Java applications using Prometheus for metrics collection and Grafana for dashboard visualization. It begins by directing readers to a GitHub repository containing the source code for a sample Java project that includes a weather service built with Reactor Java and Spring Boot. The guide then walks through configuring the Java service to expose metrics via the Spring Boot Actuator and Micrometer library, setting up Docker Compose for running the service, and integrating it with Prometheus. It also covers how to configure Prometheus to scrape metrics from the Java service and how to import and set up a predefined JVM dashboard in Grafana for monitoring purposes. The article assumes prior knowledge of setting up Prometheus and Grafana with Docker Compose.

Opinions

  • The author suggests that using Micrometer with Spring Boot Actuator is an effective way to gather and expose application metrics for monitoring.
  • It is implied that running the Java service in Docker Compose is a convenient approach for local development and testing.
  • The author emphasizes the utility of predefined Grafana dashboards for monitoring Java or JVM metrics, indicating that these dashboards are readily available and can save time in setting up a monitoring system.
  • The article endorses the practice of using the same Docker network for the Java service, Prometheus, and Grafana to ensure seamless communication between services.
  • The author's choice to use the JVM (Micrometer) dashboard from Grafana's open dashboards indicates a preference for community-supported tools that are easy to integrate and provide immediate value.

Monitor Java metrics with Prometheus and Grafana

Use predefined Grafana dashboards for monitoring your application

The purpose of this article is to illustrate how Prometheus can be used for gathering Java metrics with Micrometer. The source code is available in the following GitHub repository: https://github.com/matisszilard/java-metrics.

My previous article followed trough the setup and configuration of Prometheus and Grafana in Docker Compose. This tutorial assumes that you have Prometheus and Grafana already up and running.

Step 1: Configure the Java service

We can start with creating a new Java project on https://start.spring.io/ . I selected Gradle, Java and Spring Boot 3.2.4 .

In the example project I created a weather service using Reactor Java, for the Rest API the project uses WebFlux. The Spring Boot Actuator is used to gather metrics what can be used to monitor our application, understand the behaviours in different scenarios. The Micrometer library helps us to expose the gathered metrics for prometheus.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'io.micrometer:micrometer-registry-prometheus:1.10.5'
}

In the application.yml file we need to enable and configure the actuator settings:

management:
  server:
    port: 9090
  endpoints:
    web:
      base-path: /
      path-mapping:
        prometheus: metrics
      exposure:
        include:
          - health
          - prometheus
  endpoint:
    health:
      enabled: true
      show-details: always

As you can see the config exposes the health and prometheus metrics. It also reconfigures the default path mapping for prometheus from /actuator/prometheus to /metrics.

The Java service can be started locally or from Docker. In this example we are going to run it from Docker Compose from the same network:

version: "3.5"

services:
  weather-service:
    image: weather-service:latest
    container_name: weather-service
    ports:
      - 8080:8080
      - 8081:9090
    restart: unless-stopped
    networks:
      - java-metrics

networks:
  java-metrics:
    name: java-metrics
    driver: bridge

Note the network configuration here, it uses the same network as the Prometheus and Grafana (see mentioned article).

Step 2: Configure Prometheus

We need to extend the Prometheus scrape configurations to collect the metrics from our Java service:

scrape_configs:
  - job_name: "weather-service"
    metrics_path: "/metrics"
    scheme: http
    honor_timestamps: true
    scrape_interval: 15s
    scrape_timeout: 10s
    static_configs:
      - targets: ["weather-service:9090"]

Restart the Prometheus service and open it’s the web ui:

Step 3: Setup Grafana dashboards

In this example we are going to use a predefined dashboard from Grafana open dashboards. Search for Java or JVM dashboards, you will see multiple ones for different purposes. In this example I will use the JVM (Micrometer) one.

https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

Just copy the dashboard ID and paste it in the Grafana import dashboard view.

Select the Prometheus instance you want to use, then click on import. You will see a similar dashboard as is in the image below.

Useful links

https://www.baeldung.com/micrometer https://readmedium.com/setup-grafana-and-prometheus-with-docker-compose-d8f946a7b27c https://github.com/matisszilard/java-metrics https://docs.spring.io/spring-framework/reference/web/webflux.html https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

Programming
Metrics
Prometheus
Java
Micrometer
Recommended from ReadMedium