MONITORING AN API WITH PROMETHEUS AND GRAFANA USING SPRING BOOT ACTUATOR
Tags: grafana, devops
Published: 2019-09-11 16:07:22

**This** tutorial provides a complete guide on how to monitor an API on your local system using a visualizing tool Grafana.

We will be integrating Spring Boot Actuator into Prometheus then, visualizing and monitoring on Grafana

Spring Boot Actuator generates metrics for our API which prometheus queries and binds to grafana where it is visualized and monitored.

**SPRING BOOT ACTUATOR** provides production-ready features such as metrics gathering, HTTP tracing etc.these features can be accessed over JMX or HTTP endpoints and can be integrated with external application monitoring systems like Prometheus, Graphite etc. [read more](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html)

*Integrating Spring Boot actuator into our application.*

The prometheus endpoint is exposed by Spring Boot when micrometer-registry-prometheus dependency is available on the classpath.


Check out how to integrate Spring boot Actuator into your Application [here](https://www.tutorialspoint.com/spring_boot/spring_boot_actuator.html)

I have integrated spring boot actuator to a simple Api i created on my local system.

*You can get the the application we used for the Prometheus setup from this [GitHub repository](https://github.com/Lyndaar/Grafana-Monitoring).*

**-** Run your application on your Local IDE.
**-** Then configure Prometheus to query the application and get results.


The prometheus endpoint exposes metrics data in a format that can be scraped by a Prometheus server. You can see the exposed metrics data by navigating to the prometheus endpoint (http://localhost:8080/actuator/prometheus).

**PROMETHEUS** is a time-series database and an open source monitoring system.[read more](https://prometheus.io/)

**Download and configure Prometheus**  

  **-**First download [Prometheus](https://prometheus.io/download/)
  **-**Extract to your selected directory
  **-**To configure your application to be monitored, In the prometheus folder, 
       you can either use the sample prometheus.yml file or create new .yml 
       files. 
         
       *Below is the configuration of a new file i have created*
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/z9343huujdrlv8971xhi.png)

  -Run prometheus server using the command prometheus.exe --config.file=YAML_FILE_PATH.

**example**: C:\Users\user\Desktop\prometheus-2.12.0.windowsamd64>prometheus.exe --config.file=tutor.yml
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/61gp8mhdi5d7a3xdiuev.png) 
       

Browse the URL http://localhost:9090/ to test the prometheus sever. If you see the page below, Prometheus server running fine and scraping the metrics which is based on the configuration you made using tutor.yml on starting the server.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/l83i35ksfd0np9ttiovr.png)

**GRAFANA** is an open source software used for visualizing, monitoring and analysing data performances.It has very beautiful visual representations and supports different datasourses.[read more](https://grafana.com/)
 
 **Download and configure Grafana**

**-** Download [Grafana](https://grafana.com/grafana/download)
**-** Extract files to desired directory
**-** In the extracted grafana folder,open the "conf" directory,configurations can be done in the ".ini" files, To avoid complications, we will continue with the default settings.which will configure "admin/admin" as the username and password.

**-** In the"bin" directory of the Grafana folder. Run the server by executing grafana-server.exe

**-** Now you can log in to Grafana inputing http://localhost:3000 in your
browser.
Remember username and password are "admin/admin". You can change this anytime using the .ini file we discussed earlier.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/zlw99ekjtolidtgneaq8.png)

**-** On the Home page Click on "create your first data source". This will open a page to add a data source.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7b91p6tup2z6jti9e9jz.png)

**-** Select Prometheus as the datasource 
**-** Give a suitable name to the data source.
**-** Select Prometheus as the "type".
**-** The URL should be "http://localhost:9090" since we have Prometheus running
on port 9090.

**-** Click "Save & Test."
If Grafana is able to connect with Prometheus with the details provided, then you will get a message saying "Data source is working." If you get any errors, check your values.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/otyzra6if1u54p7qgsvb.png)

**Build your Grafana Dashboard**
**-** You can create your own dashboard or import dashboard
**-** Click on the "+" option on the left menu of the home screen to create or import a dashboard.
  
 *I'm creating a new dashboard*
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/19vz1fuwkkpwzc2knlwn.png)

**-** click on choose visualization and select "Graph"
**-** On the left menu tab select "queries" to add a query
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9etfgot2j96g103tj7yj.png)

**-** Make a query
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/xsc8471zdam5dq0eyl32.png)

**-**Our metrics will be populated in the graph

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/7gobsvhgdfjukrwcs0ln.png)
*Awesome! we can now visualize and monitor metrics of the* "jvm_gc_pause_seconds_count" *realtime.* 

You can now add more queries to your dashboard.


**CONCLUSION**
API Monitoring is important to gain visibility into performance, availability and functional correctness.Creating dashboards on Grafana helps us quickly address errors, check the health of our application, if an application is responding correctly and if the service is accessible at all time etc.
Using spring boot actuator to generate and customize your metrics and Prometheus to query these metrics and visualize them on Grafana, Has made monitoring an application quite easy.

If you have any questions  or contibutions regarding this post you can send an [Email](uzosikelynda20@gmail.com) or **DM** on Twitter [@arh_darh](https://twitter.com/arh_darh)   feel free to comment below.

**THANKS!!!**