Monitoring Spring Boot applications with Prometheus and Grafana spring boot prometheus grafana scaled

Monitoring Spring Boot applications with Prometheus and Grafana

In order to compare the performance of different JDKs for reactive Spring Boot services, I made a setup in which a Spring Boot application is wrapped in a Docker container. This makes it easy to create different containers for different JDKs with the same Spring Boot application running in it. The Spring Boot application exposes metrics to Prometheus. Grafana can read these metrics and allows to make nice visualizations from it. This blog post describes a setup to get you up and running in minutes. A next post will show the JDK comparisons. You can download the code here (in the complete folder). To indicate how easy this setup is, getting it up and running and write this blog post took me less than 1.5 hours total. I did not have much prior knowledge on Prometheus and Grafana save for a single workshop at AMIS by Lucas Jellema (see here).

Wrapping Spring Boot in a Docker container

Wrapping Spring Boot applications in a Docker container is easy. See for example here. You need to do the following:

Create a Dockerfile as followed (change the FROM entry to get a different JDK)

Monitoring Spring Boot applications with Prometheus and Grafana dockerfile

Add a plugin to the pom.xml file.

Monitoring Spring Boot applications with Prometheus and Grafana pom plugin

And define the property used:

Monitoring Spring Boot applications with Prometheus and Grafana property

Now you can do mvn clean package dockerfile:build and it will create the Docker image springio/gs-reactive-rest-service:latest for you. You can run this with: docker run -p 8080:8080 -t springio/gs-reactive-rest-service:latest

Making Prometheus style metrics available from Spring Boot

In order to make Prometheus metrics available from the String Boot application, some dependencies need to be added (see here).

Monitoring Spring Boot applications with Prometheus and Grafana prometheus metrics

Now you can run the Docker container and go to an URL like: http://localhost:8080/actuator/prometheus and you will see something like:

Monitoring Spring Boot applications with Prometheus and Grafana prometheus from actuator

Provide Prometheus configuration

I’ve provided a small configuration file to make Prometheus look at the metrics URL from Spring Boot (see here):

Monitoring Spring Boot applications with Prometheus and Grafana prometheus config

Putting Spring Boot, Prometheus and Grafana together

As you can see in the above screenshot, I’ve used the hostname spring-boot. I can do this because of the docker compose configuration, container_name. This is as you can see below:

Monitoring Spring Boot applications with Prometheus and Grafana putting it together 1

Grafana and Prometheus are the official Docker images for those products. I’ve added the previously mentioned configuration file to the Prometheus instance (the volumes entry under prometheus).

Now I can do docker-compose up and it will start Spring Boot (available at localhost:8080), Prometheus with the configuration file (available at localhost:9090) and Grafana (available at localhost:3000). They will be put in the same Docker network and can access each other by the hostnames ‘prometheus’, ‘grafana’ and spring-boot’

Configure Grafana

In Grafana it is easy to add Prometheus as a data source.

Monitoring Spring Boot applications with Prometheus and Grafana grafana

When you have done this, you can add dashboards. An easy way to do this is to create a simple query in Prometheus and copy it to Grafana to create a graph from it. There are probably better ways to do thus but I have yet to dive into Grafana to learn more about its capabilities.

Monitoring Spring Boot applications with Prometheus and Grafana copy from prometheus

Monitoring Spring Boot applications with Prometheus and Grafana from prometheus to grafana

Finally

It is easy and powerful to monitor a Spring Boot application using Prometheus and Grafana. Using a docker-compose file, it is also easy to put an assembly together to start/link the different containers. This makes it easy to start fresh if you want to.

To try it out for yourself do the following (I’ve used the following VM (requires Vagrant and VirtualBox to build) with docker, docker-compose and maven preinstalled: here)


git clone https://github.com/MaartenSmeets/gs-reactive-rest-service
cd gs-reactive-rest-service/complete
mvn clean package
mvn dockerfile:build
docker-compose up

Then you can access the previously specified URL’s to access the Spring Boot application, Prometheus and Grafana.