DEV Community

loading...
Cover image for Spring boot metrics on Prometheus

Spring boot metrics on Prometheus

Fabio Hiroki
・3 min read

Introduction

gif showing happy kid after discovering his code is working
After days of hard development, my application has been finally deployed to production! No user has reported bugs, so it seems everything is fine. But does it really?

I don't like to just deploy and move to the next task, so I feel the need for checking if my new API is healthy and breathing. So, I instantly open a new browser tab and go check the logs on Kibana.

Now I have to remember which relevant log messages I left behind, and also the Lucene syntax, but this isn't scalable since I can't do this for every deployment. This process also doesn't work for non-technical people, like the product manager. I really wish there was a dashboard where I could simply configure once and then just visualize all the metrics.

Prerequisites

download.png

The Spring boot API will expose data that will be collected by Prometheus and then displayed on Grafana. Java 8 will be required to run the Spring boot application.

If you want to know how to install Prometheus and Grafana on a Linux based OS, check my previous article.

Setup Spring

Screenshot from 2021-02-24 07-21-09.png

On Spring initializr choose a gradle project with dependencies:

  • Spring Web
  • Spring Boot DevTools
  • Prometheus
  • Spring Boot Actuator

The final code is available on this Github repository.

Expose Prometheus metrics

On your application.properties file, add the following line:

management.endpoints.web.exposure.include=*
Enter fullscreen mode Exit fullscreen mode

Run your Spring application and access the endpoint:

http://localhost:8080/actuator/prometheus
Enter fullscreen mode Exit fullscreen mode

It should return a bunch of data in a format that is understandable by Prometheus.

Configure Prometheus to scrap this data

On your prometheus.yml file, add a new job under scrape_configs:

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'spring_micrometer'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']
Enter fullscreen mode Exit fullscreen mode

Now access Prometheus interface on http://localhost:9090/ and query for

http_server_requests_seconds_count{job="spring_micrometer"}
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2021-03-01 06-58-17.png

You will see the requests Prometheus is making to scrap data from your Spring boot application. That's a signal that everything is working so far.

Configure Grafana

Now we will configure Grafana to display this data. Open Grafana on your browser and import the JVM (Actuator) dashboard.

Screenshot from 2021-03-01 07-07-16.png

You should now see some cool data:
Screenshot from 2021-03-01 07-09-25.png

Conclusion

Now you have all your metrics in one place, just need to enter an URL from your browser. You could even buy a giant TV and keep the dashboard streaming 24/7.

What if you could send your own custom metric to Prometheus, like a business metric? What if you could create automated alerts based on Prometheus queries? Leave a comment below so I will know you're interested in these contents.

Discussion (0)

Forem Open with the Forem app