Home > Software > How to Integrate Spring Boot with Prometheus and Grafana using Docker Compose

How to Integrate Spring Boot with Prometheus and Grafana using Docker Compose

Anastasios Antoniadis

Learn how to integrate Spring Boot with Prometheus and Grafana for efficient monitoring using Docker Compose. This guide provides a step-by-step setup to visualize application metrics seamlessly.

Docker (1)

Monitoring and metrics ensure performance and reliability in microservices and web applications. Spring Boot, a popular choice for building microservices in Java, offers extensive support for application metrics. Prometheus and Grafana stand out as a powerful duo for monitoring these metrics: Prometheus for metric collection and storage and Grafana for visualization. Using Docker Compose to orchestrate these components simplifies the setup and management process. This article provides a comprehensive guide on integrating Spring Boot with Prometheus and Grafana using Docker Compose, facilitating a seamless monitoring setup for your applications.

Prerequisites

Before diving into the Docker Compose setup, ensure you have the following prerequisites covered:

  • Docker and Docker Compose installed on your system.
  • A basic Spring Boot application with Actuator and Prometheus dependencies for metrics generation.

Overview of Components

  • Spring Boot Application: A Java-based application framework for microservices, equipped with Actuator for exposing metrics endpoints.
  • Prometheus: An open-source monitoring system that collects metrics from configured targets at specified intervals, evaluates rule expressions, and displays results.
  • Grafana is an open-source platform for monitoring and observability. It can visualize metrics from various sources, including Prometheus.

Docker Compose Setup

Below is an example Docker Compose file that orchestrates a Spring Boot application, Prometheus, and Grafana:

version: '3.7'

services:
  app:
    image: your-spring-boot-app-image
    ports:
      - "8080:8080"
    networks:
      - app-network

  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
    depends_on:
      - app
    networks:
      - app-network

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"
    depends_on:
      - prometheus
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

Key Components

  • app: Replace your-spring-boot-app-image with the Docker image of your Spring Boot application. Ensure your application exposes metrics via Spring Actuator at /actuator/prometheus.
  • prometheus: Prometheus is configured to scrape metrics from your Spring Boot application. You’ll need a prometheus.yml configuration file in the same directory as your Docker Compose file.
  • grafana: Grafana is set up to visualize the metrics collected by Prometheus. After starting Grafana, you’ll configure it to use Prometheus as a data source.

Prometheus Configuration (prometheus.yml)

Here’s a basic prometheus.yml configuration to scrape metrics from the Spring Boot application:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'spring-boot'
    static_configs:
      - targets: ['app:8080']

Running the Services

To launch your services, run the following command in the directory containing your docker-compose.yml and prometheus.yml files:

docker compose up -d

Configuring Grafana

After starting the services, access Grafana by visiting http://localhost:3000 in your browser. The default login is admin for both username and password. Follow these steps to configure Grafana to use Prometheus as a data source and create dashboards for visualizing your Spring Boot metrics:

  1. Add Prometheus as a Data Source: Navigate to “Configuration” > “Data Sources” > “Add data source”. Select “Prometheus” as the type and set the URL to http://prometheus:9090. Save your changes.
  2. Create a Dashboard: Go to “Create” > “Dashboard” and add a new panel. Choose the Prometheus data source and enter a query to visualize a metric, for example, jvm_memory_used_bytes. Customize the panel as desired and save the dashboard.

Conclusion

Integrating Spring Boot with Prometheus and Grafana using Docker Compose offers a powerful and efficient way to monitor your applications. This setup simplifies deployment and provides a scalable solution for monitoring microservices architecture. By following the steps outlined in this guide, you can gain valuable insights into your application’s performance and health, enabling you to make informed decisions and maintain optimal operation.

Anastasios Antoniadis
Follow me
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x