Getting Started with Spring Boot Application monitoring using Prometheus and Grafana
Monitoring an application has become a very important part of software lifecycle as we want to be able to answer the “when or why” questions with data to proof it then avoid assumptions and surprises. i recently got to know about these tools and i was happy to use then share. Let jump right into this subject matter.
Application metrics and performance data is generated and exposed easily with the help of the micrometer and actuator, then prometheus fetches and stores these information at interval also allowing users to query the information stored but we need a dashboard for monitoring purpose and having an overview of the system, then we bring in grafana which is used to connect to the prometheus and build dashboard easily. These can be done for several instances of an application.
Spring Boot Application
The application is built using spring boot. To ensure that the application details are available, we have to ensure that the dependencies below are added to the project. (i am using Maven, you can try to import the gradle way if you are using gradle instead of maven).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId></dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
<version>1.2.2</version>
</dependency>
java.interceptor-api is optional
Also ensure that prometheus is enable and exposed on the configuration file(application.properties).
management.endpoints.web.exposure.include=prometheus,info,health
management.endpoint.prometheus.enabled=true
Ensure that you can view the details below when you visit the url : <scheme>://<host>:<port>/<context-path>/actuator/prometheus
Docker Compose to set up prometheus and grafana
version: “3”services:
prometheus:
image: prom/prometheus:latest
container_name: ‘prometheus’
volumes:
— ./prometheus.yml:/etc/prometheus/prometheus.yml
— ./rules.yml:/etc/prometheus/rules.yml
ports:
— ‘9090:9090’
grafana:
image: grafana/grafana:latest
container_name: ‘grafana’
ports:
— ‘3000:3000’
depends_on:
— prometheus
alertmanager:
image: prom/alertmanager:latest
ports:
— 9093:9093
volumes:
— ./alertmanager.yml:/etc/alertmanager/alertmanager.yml
Prometheus
So Prometheus pulls metrics from the spring boot application. This process is called scrap but we need to create the configuration file called prometheus.yml. Below is a sample prometheus.yml file
scrape_configs:
# Path can be different depending on your Spring Boot configuration
- job_name: 'application_x'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
scheme: http
static_configs:
- targets: ['<ip>:<port>']
- job_name: 'application_y'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
scheme: http
static_configs:
- targets: ['<ip>:<port>']
rule_files:
- 'rules.yml' #file containing all the alert rules
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093 #location to the alert manager
job_name should be unique while scrape_interval is how often prometheus scrapes the targets for that job_name.
Note that if your spring-boot application is running on your local machine but not as a docker container, please use “docker.for.mac.localhost” for mac or “docker.for.win.localhost” for windows as your ip.
I guess you will be wondering whats these configurations for…
rule_files:
- 'rules.yml' #file containing all the alert rules
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093 #location to the alert manager
They are alert configuration on prometheus but we need to set the rules which can be tailored to suit what you want. we have a sample rule which is set on the rules.yml file
— ./rules.yml:/etc/prometheus/rules.yml
you also noticed the line above on the prometheus.yml file. thats where the rules are set.
rules.yml
#This is where the rules are set
groups:
- name: default
rules:
- alert: RequestRate #alert name
expr: rate(http_server_requests_seconds_count{uri="/test"}[5m]) > 0 #Actual spring boot - prometheus report to monitor
for: 1m #this is how long it monitors before triggering alerts
labels:
severity: high
annotations:
summary: Application receiving too many request #email content
Also we need to configure the alertmanager. the configuration is done on the alertmanager.yml.
route:
receiver: emailer
receivers:
- name: emailer
email_configs:
- to: <smtp_username>
from: <smtp_username>
smarthost: smtp.gmail.com:587
auth_username: <smtp_username>
auth_identity: <smtp_username>
auth_password: <smtp_password>
Wow, we have setup prometheus and alertmanager.
Please run the docker-compose file by running the cmd “docker-compose -f <docker-compose file.yml> up -d”, once the containers are up. Please visit the url : http://localhost:9090
View the alert configured by clicking on “Alerts”
Grafana
Next is Grafana which is used to visualize the reports stored on prometheus. Please visit http://localhost:3000
Default username and password is admin
First we need to configure the datasource which is prometheus on grafana.
Finally, we need to create the dashboard. Good news, we don’t have to create a dashboard manually, you can import a dashboard from https://grafana.com/grafana/dashboards but i will recommend
To import a dashboard. Click on Import
So you supply the dashboard id and click on the “Load” button.
Then you have the dashboard. Congratulations.
Note that you can add custom panels containing custom report on the dashboard.
Thank you for your time.