Spring Boot Monitoring with Prometheus & Grafana: Complete Setup Guide

Monitoring Spring Boot applications is essential for maintaining performance, diagnosing issues, and understanding user interactions. Combining Prometheus and Grafana provides a powerful, open-source solution for real-time metrics visualization and alerting. This guide walks you through the complete setup process to integrate Prometheus and Grafana with your Spring Boot application.

Prerequisites

  • Java 11 or higher installed
  • Spring Boot application ready
  • Docker installed for easy setup
  • Basic knowledge of Spring Boot and Docker

Step 1: Add Prometheus Dependencies to Spring Boot

Include the necessary dependencies to expose metrics in your Spring Boot application. If you're using Maven, add the following to your pom.xml:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-core</artifactId>
</dependency>

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Ensure your application is configured to enable metrics exposure by adding the following in application.properties:

management.endpoints.web.exposure.include=health,info,prometheus
management.endpoint.prometheus.enabled=true

Step 2: Create a Prometheus Configuration

Set up Prometheus to scrape metrics from your Spring Boot application. Create a prometheus.yml file with the following content:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'spring-boot-app'
    static_configs:
      - targets: ['host.docker.internal:8080']

Step 3: Run Prometheus with Docker

Use Docker to run Prometheus with your configuration file. Execute the following command:

docker run -d --name=prometheus -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

Step 4: Set Up Grafana

Download and run Grafana using Docker:

docker run -d -p 3000:3000 --name=grafana grafana/grafana

Access Grafana at http://localhost:3000. Log in with default credentials (admin/admin) and change the password upon first login.

Step 5: Add Prometheus Data Source to Grafana

In Grafana, navigate to Configuration > Data Sources. Click Add data source and select Prometheus. Enter the URL http://host.docker.internal:9090 and click Save & Test.

Step 6: Create Dashboards in Grafana

Import pre-built dashboards or create your own to visualize metrics such as JVM memory usage, HTTP request rates, and error rates. Use the + > Import feature and enter dashboard IDs like 11074 for Spring Boot metrics.

Additional Tips

  • Secure your Prometheus and Grafana endpoints in production.
  • Configure alerts in Grafana based on metrics thresholds.
  • Use Docker Compose for easier multi-container setup.

Monitoring your Spring Boot application with Prometheus and Grafana enhances visibility and helps maintain optimal performance. Follow this guide to set up a robust monitoring stack tailored to your development environment.