Spring boot metrics on Prometheus
Tags: tutorial, java, devops, webdev
Published: 2021-03-02 09:51:18

# Introduction
![gif showing happy kid after discovering his code is working](https://media.giphy.com/media/12BYUePgtn7sis/giphy.gif)
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1614160048266/KJV9JANjb.png)

The Spring boot API will expose data that will be collected by [Prometheus](https://prometheus.io/docs/introduction/first_steps/) and then displayed on [Grafana](https://grafana.com/grafana/download). 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](https://dev.to/fabiothiroki/monitoring-spring-boot-with-prometheus-grafana-36p4).

# Setup Spring

![Screenshot from 2021-02-24 07-21-09.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614162137266/8novLVOBR.png)

On [Spring initializr](https://start.spring.io/) choose a gradle project with dependencies:
- `Spring Web`
- `Spring Boot DevTools`
- `Prometheus`
- `Spring Boot Actuator`

The final code is available on this [Github repository](https://github.com/fabiothiroki/spring-boot-micrometer).

# 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`:
```yml
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
```promql
http_server_requests_seconds_count{job="spring_micrometer"}
```

![Screenshot from 2021-03-01 06-58-17.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614592735388/VUOvFoy1g.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](https://grafana.com/grafana/dashboards/9568).

![Screenshot from 2021-03-01 07-07-16.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614593267448/qPOqnP1Ss.png)

You should now see some cool data:
![Screenshot from 2021-03-01 07-09-25.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1614593407963/WGPmxkkpf.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.