
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: alwaysAs 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: bridgeNote 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.

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/






