Introduction
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
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
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=*
Run your Spring application and access the endpoint:
http://localhost:8080/actuator/prometheus
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']
Now access Prometheus interface on http://localhost:9090/
and query for
http_server_requests_seconds_count{job="spring_micrometer"}
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.
You should now see some cool data:
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.