Monitor Spring Boot Microservice using Micrometer, Prometheus and Grafana

Teten Nugraha
6 min readJun 9, 2022

--

In the world of distributed systems / microservices, especially those that already have services in the hundreds or thousands, we may experience difficulties if we monitor each service manually. Metric is one way to see application performance. In this article, we will discuss how to create custom metrics using the micrometer library, which will then be recorded by the Prometheus tool and displayed / visualized using Grafana.

Why Monitoring? Why now?

Historically, it has been difficult to monitor multiple disparate applications. Because every language or framework has its own way of seeing application performance. But now people have done a lot of innovation regarding application monitoring and it has become very important and yes that tool is Prometheus.

What is Prometheus?

is a tool used to store point or metric data over time. So the metrics and performance data of your application will be retrieved within the time frame defined by Prometheus and this tool allows us to perform time series analysis of the metrics that have been collected. For friends who don’t understand what time series analysis is, according to Aileen Nielsen in her book, Practical Time Series Analysis.

Time series analysis is the endeavor of extracting meaningful summary and statistical information from points arranged in chronological order. It is done to diagnose past behavior as well as to predict future behavior.

In other words, time series analysis allows us to look at a collection of data over time and see what trends you are currently doing and maybe even a little estimate of the future. Prometheus runs separately from the application that we will monitor its performance. As mentioned above, Prometheus periodically retrieves performance metrics and data (pull based approarch) and then stores them in its database. This is good because prometheus is not tightly dependent on the application (tight coupling) so that if prometheus is not running at some point it will not interfere with the target application.

What is Grafana?

In the prometheus browser, there is a standard visual that displays a chart of application metrics. However, if we combine them using Grafana, we can create a dashboard page from the metrics that we want to monitor performance. Grafana can get data not only from Prometheus but also from Elasticsearch, InfluxDB etc., besides that, Grafana can also send messages or notification alerts to email, slack, hipchat etc.

What is Micrometer ?

Micrometer can make it easier for us to create custom metrics, such as how often the api/v1/payment endpoint is hit by a user or want to see user traffic that hits the api/v1/products endpoint for example. If you’ve ever used Log4J, the Micrometer is similar to that, we just need to decide where or in which method we want to create the metrics we want to monitor. There are three types of metric micrometers, namely:

  • Gauge Measures resource usage, capacity, etc. whose value can go up and down. An example is looking at cpu usage, visitor traffic from time to time.
  • Counter Measures an event or action whose value always increases and never decreases. An example is looking at the number of users who have registered from the beginning until now.
  • Timer Measures events that have a short duration, for example method execution time, request duration.

Demo Project

Making Application Samples

Create a Spring Boot project on the spring initializr site with the library specifications as below.

Adjust its application.properties to be like this.

management.endpoints.web.exposure.include=prometheus,health,info,metric

run the application, then check on localhost:8080/actuator, then the display will be like below

if you click on the prometheus section, a page will appear containing the metrics from the application

In this demo, I will try to create a class that every 1 second generates an integer number that we will use in the Gauge and Counter metrics.

then because we are making a scheduler application, don’t forget to add the @EnableScheduling annotation in the class that contains the main method. Run the application again and see if the custom_gauge and custom_counter metrics already exist or not.

Running Prometheus

Here I use docker to run the prometheus, you can get the images using

docker pull prom/prometheus

in the Spring Boot root folder above, create a prometheus.yml file which we will use to create specifications or mounting files for prometheus. But before that, we have to know what IP Address / Host we are using, I use this syntax to see it

ifconfig | grep inet

prometheus.yml

global:
scrape_interval: 5s
scrape_configs:
- job_name: prometheus
honor_labels: true
static_configs:
- targets: [ "localhost:9090" ]
- job_name: 'spring_micrometer'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['your_host_ip:8080']

then run the prometheus image into a container with the following syntax, (point the terminal to the root source of the Spring Boot application above)

docker run -d -p 9090:9090 -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

browser access with url localhost:9090/targets eat if successful it will look like this.

access the Graph menu then look for custom gauge click execute and on the tab d below select graph, then the time series of the custom_gauge metric will be displayed

at this step we have succeeded in exposing the metrics and captured by prometheus, the next thing is we will create a dashboard from the metrics above using Grafana.

Creating a Grafana visualization dashboard

first pull the graphana image using

docker pull grafana/grafana

and running the container

docker run -d -p 3000:3000 grafana/grafana

access localhost:3000, if there is a login form, the default login is username: admin and password: admin

Before we create a datasource in grafana, then we must know the IP Address of the prometheus container that we created earlier, using the command

docker ps
docker inspect <container ID prometheus>

after that go back to Grafana, in Datasource then select Prometheus

enter the URL according to the IP Address of the Prometheus container and its Access Browser, test then save.

and create a dashboard from the metrics that we have created, namely custom_gauge and custom_counter_total.

Thus this article, hopefully it can add insight and help friends in making monitoring for applications in Spring Boot. For the source code, you can get it here.

If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too or if you wanna talk internally with me , reach me in https://linktr.ee/teten_nugraha.

--

--

Teten Nugraha

Software Engineer, 8 years of experience. Expertise with Microservices, Spring Boot, CICD, Docker https://www.linkedin.com/in/teten-nugraha