DEV Community

Uzosike Lynda
Uzosike Lynda

Posted on

MONITORING AN API WITH PROMETHEUS AND GRAFANA USING SPRING BOOT ACTUATOR

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

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

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.

- 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

Download and configure Prometheus

-First download Prometheus
-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

-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

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

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

Download and configure Grafana

- Download Grafana
- 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

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

Alt Text

- 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

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

- click on choose visualization and select "Graph"
- On the left menu tab select "queries" to add a query
Alt Text

- Make a query
Alt Text

-Our metrics will be populated in the graph

Alt Text
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 or DM on Twitter @arh_darh feel free to comment below.

THANKS!!!

Top comments (2)

Collapse
 
marciasheila profile image
marciasheila

Hello, I'm currently testing Grafana for OpenShift dedicated. Is there a way I can enable metrics for the applications running in the test containers? The dev team is using mongodb and weblogic.
Thank you so much!

Collapse
 
kirklewis profile image
Kirk Lewis

Good post! Monitoring and performance are two of my favourite topics. Keep it up.