Monitoring Spring Boot Application with Prometheus and Grafana

unsplash-logoCarlos Muza

In this post we will discuss how to integrate Prometheus monitoring system with Spring Boot and Grafana.

The code for this post is available on Github here

This setup has three major components.

Spring Boot and Micrometer

Spring Boot Actuator provides number of features to help us monitor and manage spring boot application. Spring Boot Actuator also provides dependency management and auto-configuration for Micrometer.

Micrometer is an application metrics facade that supports numerous monitoring systems.
To integrate Prometheus with spring boot we just need to add micrometer-registry-prometheus dependancy in class path.Once spring boot detects this dependancy it will expose /actuator/prometheus endpoint.

pom File
1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Along with this dependancy we need to set below property.

application.properties
1
management.endpoints.web.exposure.include=health,info,metrics,prometheus

Now if we run the application and check http://localhost:8080/actuator/prometheus

Prometheus

Prometheus is an open-source monitoring and alerting toolkit originally built at SoundCloud.
Prometheus does one thing and it does it well. It has a simple yet powerful data model and a query language that lets you analyse how your applications and infrastructure are performing.

Prometheus has a data scraper that pulls metrics data over HTTP periodically at a configured interval. It stores metric data in A time-series Database. It has simple user interface that can be used to run query and visualize data. It also has powerful Altering system.

let’s try to run Prometheus on docker.
We need to configure Prometheus to scrape our spring boot application.

prometheus.yml
1
2
3
4
5
6
7
8
9
global:
scrape_interval: 10s

scrape_configs:
- job_name: 'spring_micrometer'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['YOUR-MACHINE-IP:8080']

configuration is very much self explanatory. We have created job ‘spring_micrometer’ for scraping our spring boot application and provided the endpoint where prometheus can get the data. As we are runing Prometheus from docker in targets we need to provide IP of local machine.

Run Prometheus on docker

1
docker run -d -p 9090:9090 -v <File path of prometheus.yml>:/etc/prometheus/prometheus.yml prom/prometheus

Now if you goto http://localhost:9090/targets it should show our spring boot application.

Grafana

Grafana is open source visualization and analytics software. It allows you to query, visualize, alert on, and explore your metrics no matter where they are stored. It provides you with tools to turn your time-series database (TSDB) data into beautiful graphs and visualizations.

Grafana supports querying Prometheus from very initial version

let’s try to setup grafana using docker.

docker run
1
docker run -d -p 3000:3000 grafana/grafana 

Now if you goto http://localhost:3000 grafana interface will be available. Default username password is admin/admin.
you can add Prometheus data source and dashboard by following below steps.

The code for this post is available on Github here

Share Comments