Thursday, November 1, 2018

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 JDK's 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 the setup. A next post will show the results. You can download the code here (in the complete folder). To indicate how easy this is, getting this setup 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.



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)


Add a plugin to the pom.xml file.


And define the property used:


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 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).

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


Provide Prometheus configuration

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


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:


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.


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.



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 and docker-compose 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.

No comments:

Post a Comment