Exploring Monitoring Solutions for Spring Boot with Grafana
Written on
Monitoring a Spring Boot application using Prometheus and Grafana
Greetings! In this article, we will demonstrate how to utilize Prometheus and Grafana for monitoring a Spring Boot application.
Contents
- Prerequisites
- Overview
- What is Prometheus?
- What is Grafana Open Source?
- Getting Started
- Spring Boot Application
- Setting Up Prometheus and Grafana via Docker
- Visualizing Data
- Conclusion
- References
Prerequisites
Before we begin, ensure you have the following prerequisites in place: - Spring Boot 3+ - Maven 3.8+ - Java 17 - Docker / Docker Compose installed
Overview
What is Prometheus?
Prometheus is a widely-used open-source toolkit for system monitoring and alerting, initially created at SoundCloud. Since its launch in 2012, it has gained popularity among various organizations and now boasts a vibrant community of developers and users. Prometheus became an independent open-source project, joining the Cloud Native Computing Foundation in 2016 as its second hosted project after Kubernetes.
For more information, visit the official documentation: https://prometheus.io/docs/introduction/overview/
Prometheus gathers and stores metrics as time series data, which means that the metrics are recorded with timestamps and can include optional key-value pairs known as labels.
What is Grafana Open Source?
Grafana is an open-source platform for data visualization and analytics, enabling users to query, visualize, alert on, and explore various metrics, logs, and traces, irrespective of where they are stored. It provides powerful tools to transform time-series database data into meaningful graphs and visualizations.
Getting Started
Spring Boot Application
We will initiate by creating a simple Spring Boot project through start.spring.io.
The spring-boot-actuator module offers all essential features for production readiness. Actuator endpoints allow for monitoring and interaction with the application. By default, web endpoints are not exposed; we need to enable them. We will expose the prometheus, health, and metrics endpoints as shown below:
management:
endpoint:
health:
show-details: always
show-components: always
endpoints:
web:
exposure:
include: ['health', 'prometheus', 'metrics']
To start our Spring Boot API, we can use the following command:
./mvnw spring-boot:run
Once the application is running, visit http://localhost:8080/actuator to see all available endpoints. The endpoint we will use for monitoring is http://localhost:8080/actuator/prometheus.
Setting Up Prometheus and Grafana via Docker
#### Configuring Prometheus Prometheus gathers metrics by scraping HTTP endpoints from specified targets. Below is a basic configuration for Prometheus in a file named prometheus.yml:
global:
scrape_interval: 15s # Default scrape interval
external_labels:
monitor: 'codelab-monitor'scrape_configs:
job_name: 'spring-boot-monitoring'
scrape_interval: 5s # Override default interval
metrics_path: '/actuator/prometheus'
static_configs:
targets: ['spring-boot-api:8080']
labels:
application: 'demo spring boot monitoring'
By default, Prometheus looks for the prometheus.yml file in the current directory. This can be changed with the --config.file command line option.
#### Grafana: Provisioning the Data Source Grafana has built-in support for Prometheus. You can manage data sources by adding YAML configuration files in the provisioning/datasources directory. Each config can list datasources to add or update at startup. If a data source already exists, Grafana will adjust it to match the new configuration.
Here’s the YAML configuration for connecting Grafana to Prometheus:
apiVersion: 1
datasources:
name: Prometheus
type: prometheus
access: proxy
isDefault: true # Set as default data source
Visualizing Data
Now, we can start all services using Docker. Here’s the complete Docker Compose configuration:
version: "3.8"
services:
prometheus:
image: prom/prometheus
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
networks:
- monitoring-network
grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
volumes:
- ./grafana:/etc/grafana/provisioning/datasources
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
networks:
- monitoring-network
api:
image: spring-boot-monitoring
container_name: spring-boot-api
ports:
- "8080:8080"
restart: unless-stopped
networks:
- monitoring-network
networks:
monitoring-network:
To launch the services, run the following command:
docker compose up -d
You can access Prometheus at http://localhost:9090 and Grafana at http://localhost:3000.
> Explore Prometheus
> Explore Grafana The default username and password are `admin`, as specified in the Docker Compose file. You can reset the password upon your first login.
We have already configured Prometheus as a data source. Navigate to Connections > Data sources in Grafana.
You can create your own dashboard or choose from the Grafana marketplace. For this guide, we used the following dashboards: - https://grafana.com/grafana/dashboards/12835-spring-boot-statistics-6756-tomcat/ - https://grafana.com/grafana/dashboards/15104-jvm-overview/
Conclusion
Congratulations! This article covered how to monitor a Spring Boot application using Prometheus and Grafana.
The complete source code can be found on GitHub.
If you enjoyed this article, please consider giving it a clap. Feel free to connect with me on Medium, Twitter, GitHub, and LinkedIn.
Support me through GitHub Sponsors.
Thank you for reading! See you in the next article.
References
- https://prometheus.io/docs/prometheus/latest/getting_started/
- https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources?pg=oss-prom&plcmt=deploy-box-1
- https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/import-dashboards/?pg=dashboards&plcmt=hero-btn2
- https://grafana.com/grafana/dashboards/