Hi. Welcome to the continuation of my Spring boot monitoring tutorial.
With Spring boot actuator we already have production-ready metrics, health check-ups, auditing and more as discussed in the previous blog. In this entry, we will learn how we can integrate 2 of the most popular 3rd party libraries use in monitoring.
- Prometheus – an open-source system and service monitoring. It collects metrics on a given interval. Thus, it is mostly used in collecting time-series data.
- Grafana – an open-source charting software for time-series analytics.
Configuring the Project
With micrometer, Spring is able to integrate application metrics to an external monitoring system such as Prometheus. For more documentation please refer to the link in the reference section.
To integrate micrometer in a Spring project we just need to add a micrometer dependency. See the code below.
It collects and export application metrics in an HTTP endpoint. The application data exposed by this endpoint is formatted in such a way that Prometheus server can scrape it.
Let’s run the Spring application and see the Prometheus metrics by opening the URL SERVER/actuator/prometheus.
We will be running Prometheus using Docker. For the image documentation please see the link in the reference section below. But before we do that we first need to define the prometheus.yml configuration. So download a base copy of that file from https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus.yml and add a new job_name entry under scrape_configs:
– job_name: ‘spring-actuator’
– targets: [SPRING_HOST_IP:8080′]
Of course, don’t forget to update the static_configs.targets value which should point to where you install your Prometheus server, normally the default should be fine. Unless you change something in your network config or port.
To run Prometheus execute:
docker run -p 9090:9090 -v /home/czetsuya/project/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
The first parameter of the -v argument must be the path of the prometheus.yml file that you have downloaded and modified. prom/prometheus is the container. See the link in the reference section for more information.
Now fire up the browser and see what we can do. Open PROMETHEUS_HOST
Running Grafana is almost the same way with Prometheus, with Docker we just need to execute the command in the terminal below.
docker run -p 3000:3000 grafana/grafana
Now let’s access the URL GRAFANA_HOST
Inside the Grafana interface we need to:
- Add dashboard
- Create query
- Select Graph
- Update properties