How to collect metrics from Spring Boot application with micrometer Prometheus and Grafana

Kirill Grishchuk
3 min readMar 13, 2021

--

Sample result

Preconditions:

  1. Maven, but you can adapt it for gradle too.
  2. Docker and docker-compose, Prometheus and Grafana will be used as containers.
  3. Spring boot 2 and higher by default, but it was backported for 1.3–1.5 with dependencies.

Since Spring boot 2 Micrometer is default metric collector. And it is simple to expose metrics of application through new actuator approach. It is necessary to just add metric provider and read them. Here we will use Prometheus as metric collector.

I will use sample application as an example. All the source code will be available on GitHub.

Init spring boot 2 via spring initializr [optional]

Sample application can be created via spring initializr and it required to add dependencies for start.

Setup actuator

Spring boot provides a mechanism which is used for service of application. Firstly, add the dependency for actuator

By default actuator is available on port=8080 uri=/actuator”

So, sample app actuator is available on uri= localhost:8080/actuator

We can visit this page and see sample actuator json answer. Now actuator is working and we are ready to supply metrics.

Metrics collection enable

Micrometer is included default distribution, so you only need to metric export to Prometheus, just add a dependency for it.

Next we configure application.yml to provide metrics from application. By default Prometheus endpoint is disabled and we need to activate it. Here is sample config.

I opened two endpoints health and prometheus, moreover enabled metrics collection for prometheus and distribution.percentiles-histogram.http.server.requests will watch for endpoint performance and response codes. Also micrometer by default shows jvm metrics, so if we now launch application at http://localhost:8080/actuator/prometheus we will see jvm metrics and request distribution (It will be printed after first api request). undefined

Monitoring environment

Sample docker-compose.yml for Prometheus and grafana.

As you can see Grafana container is built. It is Grafana bug with preconfigured dashboards. I preconfigured sample dashboards for Grafana. See more in source repo Make sure to configure prometheus.yml for your url.

Dashboards

I have prepared two different dashboards, that are available at compose start, they are already bundled in Grafana it is extremely useful for examples.

  1. JVM Dashboard
Result dashboard for jvm

2. Response throughput

Response throughput dashboard

Conclusion

  1. We configured application to provide metrics.
  2. We configured an infrastructure to collect and visualise data.
  3. We looked through created dashboards.

Links

Github repo with sample

Originally published at http://github.com.

--

--

Kirill Grishchuk
Kirill Grishchuk

No responses yet