Monitoring Made Simple: Empowering Spring Boot Applications with Prometheus and Grafana

Arshit Moradiya
Simform Engineering
6 min readJul 6, 2023

Learn how to configure and use Prometheus and Grafana to collect metrics from your Spring Boot app and visualize them in various ways.

Monitoring Made Simple: Empowering Spring Boot Applications with Prometheus and Grafana

Imagine you have a production-level Spring Boot application that serves thousands of users at the same time. Without monitoring, you won’t get detailed information about your application’s performance, health, or whether there are any underlying problems. That is where Monitoring of the application comes into the picture.

Prometheus and Grafana offer a strong monitoring solution for Spring Boot apps. Prometheus collects metrics and manages alerts, while Grafana visualizes matrices and manages alerts. Together, they help detect and resolve problems before they affect users.

In this blog, we will explore the process of monitoring Spring Boot applications using Prometheus and Grafana, the steps involved in setting up these tools, and their benefits.

Monitoring Workflow

Introduction to Prometheus

Prometheus is a popular open-source monitoring and alerting system. It collects, stores, and analyzes metrics from various sources like applications, services, operating systems, and hardware devices. It offers insights into performance and system health.

Introduction to Grafana

Grafana is an open-source tool for data visualization, monitoring, and troubleshooting. It creates dashboards to visualize metrics from sources like Prometheus, Elasticsearch, InfluxDB, and CloudWatch. You can customize the dashboards with graphs, tables, charts, and maps to suit your needs.

Prerequisites

To follow this blog, you will need the following:

  • A running Spring Boot application for monitoring
  • Docker to run Prometheus and Grafana images

Setting up Spring Boot project to enable monitoring with Prometheus and Grafana

Add the below maven dependencies into the pom.xml file of your spring boot project.

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
  • Actuator dependency enables endpoints that expose information about your application’s health, metrics, configuration, and monitoring.
  • If you don’t have Spring Web dependency in your project, add it for web development and auto-configuration.
  • Micrometer dependency is used to configure Spring Boot applications to expose metrics in a format that Prometheus can scrape and store.

After adding these dependencies and without any further configuration, when you start the Spring Boot application and make a call tohttp://localhost:8080/actuator endpoint, it will display various default health information endpoints as follows:

Default Actuator endpoints

Enable all the actuator endpoints by adding the below configuration in the application.yml file of your spring boot project.

management:
endpoints:
web:
exposure:
include: '*'

Now if you call http://localhost:8080/actuator endpoint, it will provide you with a list of all the available actuator endpoints.

Since we require only http://localhost:8080/actuator/prometheus endpoint, we can enable it by adding the below configuration in the application.yml file of your Spring Boot project.

management:
endpoints:
web:
exposure:
include: [ "prometheus" ]

Now if you call http://localhost:8080/actuator/prometheus endpoint, it gives metrics data in a format compatible with Prometheus.

Prometheus metrics

That’s it!

We just need to configure this in the Spring Boot project to monitor it with Prometheus and Grafana.

Setting up Prometheus and Grafana using Docker

The Docker Compose file configures a Prometheus container to collect metrics from a Spring Boot application using the prometheus.yml file. It also sets up a Grafana container that uses the datasources.yml file to define Prometheus as the default data source for analyzing the collected metrics.

Ensure the path of the prometheus.yml and datasources.yml files are correct according to the docker-compose.yml file path.

Project Structure

Create docker-compose.yml

version: '3.7'

services:
prometheus:
image: prom/prometheus:v2.44.0
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml

grafana:
image: grafana/grafana:9.5.2
container_name: grafana
ports:
- "3000:3000"
restart: unless-stopped
volumes:
- ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources

Create prometheus.yml

scrape_configs:
- job_name: 'MyAppMetrics'
metrics_path: '/actuator/prometheus'
scrape_interval: 3s
static_configs:
- targets: ['host.docker.internal:8080']
labels:
application: 'My Spring Boot Application'

This scrape config instructs Prometheus to scrape the http://localhost:8080/actuator/prometheus endpoint on the host.docker.internal:8080 address every 3 seconds. The application label is used to identify the application in Prometheus.

Create datasources.yml

apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true

The data source you defined instructs Grafana to connect with Prometheus using http://prometheus:9090 and use it as the default data source. You can also add Prometheus data source manually after the Grafana container is up.

Let’s start the services by running the below command where the docker-compose.yml file is placed:

$ docker-compose up

Now, Prometheus is accessible via http://localhost:9090 and Grafana is accessible via http://localhost:3000.

Exploring Prometheus

Based on configured scrap intervals, the Prometheus server periodically queries the configured targets for their metrics, which are stored in a time series database.

Prometheus provides a rich query language called PromQL for exploring and analyzing the collected metrics. The following demo demonstrates how to get the metrics of a configured Spring Boot application.

Prometheus metrics retrieval

In addition to its monitoring capabilities, Prometheus offers robust alert support, which notifies you when certain metrics exceed a threshold. You can configure alerts to be sent to different channels, such as email, instant messaging platforms, or webhook endpoints.

Exploring Grafana

When you log in to Grafana for the first time, use the default username and password, which is “admin” for both. You will be prompted to reset the password, so enter your new password and confirm it.

Grafana Log in

Grafana has the ability to connect to various data sources, including databases, time-series databases like Prometheus, cloud platforms, and APIs. It enables you to query and aggregate data from your selected sources.

With Grafana, you can create customized dashboards to arrange and configure panels that present data in meaningful ways. Grafana offers a wide range of panel types, such as Graph, Singlestat, Table, Gauge, Heatmap, and more.

Important notes:

  • Since we have already set up the Prometheus data source in the docker-compose.ymlfile while creating the Docker container, there is no need to add the Prometheus data source manually in Grafana. You can directly create a dashboard using the pre-configured Prometheus data source.
  • Based on the Grafana docker image version you are using, you will get some differences in the UI of the Grafana.
Grafana Dashboard

Grafana also offers a marketplace with a wide selection of pre-configured dashboard templates. These templates are designed to provide specific monitoring or visualization functionality right away. Ready-to-use dashboards are typically shared as JSON files or provided as Grafana dashboard IDs. For our Spring Boot application, we can utilize a pre-configured dashboard designed specifically for Spring Boot from the marketplace. In the demo below, I have used the spring boot template with the id 11378.

Grafana Ready-To-Use Dashboard

Moreover, Grafana offers a built-in alerting system that allows you to set up rules and conditions based on your metrics. When the defined thresholds are breached, Grafana can trigger notifications via various channels like email, Slack, PagerDuty, etc.

Key learnings and takeaways

Prometheus is a highly scalable and flexible monitoring solution, while Grafana is a powerful and versatile visualization tool.

Prometheus enables you to collect metrics from your application, and Grafana can be used to display those metrics in a variety of ways. With Spring Boot Actuator, you can access metrics like request rate, average response time, and error count, which can be collected by Prometheus.

Combining Prometheus and Grafana creates a comprehensive monitoring solution for Spring Boot applications. This solution enables you to monitor application health, troubleshoot issues, and identify trends.

Interested in learning more about Grafana and Prometheus? Refer to the official documentation of Prometheus and Grafana.

Stay updated with the latest insights into software engineering with the Simform Engineering blog.

For more updates, connect with us on Twitter and LinkedIn

--

--

Arshit Moradiya
Simform Engineering

Software Engineer At Simform | Java | Spring Boot | Microservices