Monitor Spring Boot Metrics using Prometheus and Grafana

Prometheus has certainly become the most used solution to monitor applications running in a distributed system such as Kubernetes.

It quite simple to expose metrics in a Spring Boot application that can be scraped using Prometheus.

Expose Metrics in Spring Boot

First we need to add the required dependencies to our build.

Using configuration we activate the Prometheus actuator, add an application name, and a common application tag.

The library micrometer-jvm-extras adds, as it’s name suggests, some more metrics to the registry. To enable it, we have to add two additional beans into the application context.

You should now be able to access the metrics in Prometheus format using a simple HTTP call: curl -s localhost:8080/actuator/prometheus

Prometheus Configuration

Prometheus is a time-series database as well as a data collection agent. It can either use service discovery (such as Kubernetes, Consul, EC2, and others) or a static target configuration. The configuration sample below statically scrapes the hosts grafana, prometheus, and application every 15 seconds.

Prometheus runs by default on port 9090 and when you have an active port mapping, you can access it on localhost:9090.

Prometheus lists all scrape targets

Grafana Configuration

Grafana supports Prometheus as well as many other data sources.

Prometheus data source in Grafana

You can now query data using the Prometheus Query Language (PromQL).

Grafana visualizing Prometheus data

Working Example

I’ve published the complete example on GitHub: gysel/spring-boot-metrics-prometheus. Prometheus is configured to scrape all the Spring Boot application as well as Grafana and itself. A Grafana dashboard is loaded automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.