This repository provides a sample Spring Boot application integrated with Prometheus for metrics collection and Grafana for visualization and monitoring. It demonstrates how to expose application metrics using Spring Boot Actuator and Micrometer, scrape them with Prometheus, and create dashboards in Grafana for observing JVM metrics, HTTP requests, and other key performance indicators.
The project includes a basic Spring Boot REST API, configuration files for Prometheus, and a Docker Compose setup to run everything locally.
- Spring Boot application with Actuator and Micrometer for Prometheus metrics export.
- Prometheus configuration to scrape metrics from the Spring Boot app.
- Grafana setup with pre-configured dashboards for JVM and application metrics.
- Docker Compose for easy deployment of Prometheus and Grafana.
- Example endpoints to generate metrics (e.g., a simple "/hello" API).
- Java 17 or higher (for Spring Boot 3.x).
- Maven 3.6+ (for building the Spring Boot app).
- Docker and Docker Compose (for running Prometheus and Grafana).
- Git (to clone the repository).
-
Clone the Repository
git clone https://github.com/moshclouds/Prometheus-And-Grafana-With-Springboot.git cd Prometheus-And-Grafana-With-Springboot -
Build the Spring Boot Application Use Maven to build the project:
mvn clean install -
Run the Spring Boot Application Start the app locally:
mvn spring-boot:runThe application will run on
http://localhost:8080. Metrics will be exposed athttp://localhost:8080/actuator/prometheus.
Add the following dependencies to enable Actuator and Prometheus metrics:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>Enable the Prometheus endpoint:
management:
endpoints:
web:
exposure:
include: prometheus, health, info
metrics:
export:
prometheus:
enabled: trueConfigure Prometheus to scrape metrics from the Spring Boot app. Example for local setup:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['host.docker.internal:8080'] # Use 'localhost:8080' if not in DockerSet default admin credentials (change these in production):
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=admin
The repository includes a docker-compose.yml to spin up Prometheus and Grafana.
version: '3.9'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
env_file:
- ./grafana.env
depends_on:
- prometheusStart the services:
docker-compose up -d
- Access Prometheus at
http://localhost:9090. - Access Grafana at
http://localhost:3000(login with admin/admin).
- Log in to Grafana.
- Add Prometheus as a data source:
- URL:
http://prometheus:9090(orhttp://localhost:9090if accessing externally).
- URL:
- Import a dashboard:
- Use the provided
dashboard.jsonin the repository (if available), or search for "JVM Micrometer" on Grafana.com (Dashboard ID: 4701 or similar). - Example dashboard includes panels for JVM memory, CPU usage, HTTP requests, and more.
- Use the provided
-
Test the Spring Boot API
- Hit the example endpoint:
curl http://localhost:8080/hello - This will generate metrics like
http_server_requests_seconds_count.
- Hit the example endpoint:
-
View Metrics in Prometheus
- Go to
http://localhost:9090/targetsto verify the scrape target. - Query metrics, e.g.,
jvm_memory_used_bytes.
- Go to
-
Visualize in Grafana
- Create or import dashboards to monitor metrics in real-time.
- Metrics not appearing? Ensure the
/actuator/prometheusendpoint is accessible and Prometheus is configured to scrape it. - Docker host issues: Replace
host.docker.internalwith your machine's IP if needed. - Grafana login issues: Check the
grafana.envfile and restart the container. - For more logs, run Docker Compose without
-d.
Feel free to fork the repository and submit pull requests. Issues and suggestions are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.