Spring Boot Monitoring with Grafana and Prometheus

Effective monitoring is crucial for maintaining the health and performance of modern software applications. In the context of Spring Boot applications, two open-source tools, Prometheus with its Alertmanager and Grafana, stand out as powerful and versatile solutions for comprehensive monitoring. This article delves into setting up and configuring these tools to effectively monitor your Spring Boot applications.

An Overview of the Monitoring components

Firstly, if you are new to Prometheus, we recommend to check the first article in this series: Monitoring Spring Boot with Prometheus

In the next section we will discuss briefly about the key components of this monitoring architecture. Then, we will see how to set up a quick environment using a Docker compose script.

Prometheus: The Metrics Collector

Prometheus is a popular open-source monitoring system that collects metrics from various sources, including applications, servers, and network devices. It employs a pull-based approach, periodically scraping metrics from configured targets. These metrics are stored in a time-series database, enabling efficient storage and retrieval of historical data. Prometheus’s strength lies in its flexibility and scalability, making it suitable for monitoring both small and large-scale deployments.

Alertmanager: Handling Alerts

Alertmanager is an optional component of the Prometheus monitoring stack that serves as a central hub for handling alerts generated by Prometheus. It receives alerts from Prometheus, filters and deduplicates them, and then sends notifications to designated receivers. This helps to ensure that critical alerts are not missed and that notifications are delivered in a timely and appropriate manner.

Grafana: The Data Visualization Platform

Grafana is a web-based data visualization platform that provides an intuitive interface for creating dashboards and charts from Prometheus metrics. It supports a wide range of graph types, including lines, bars, and tables, catering to various data visualization needs. Grafana also offers plugins for integrating with other data sources and extending its functionality.

Setting Up the Monitoring Environment

To set up the monitoring environment for your Spring Boot application, you’ll need to install and configure Prometheus, Grafana, and Alertmanager. Here’s an example Docker Compose docker-compose.yml file for setting up the monitoring environment:

version: '3'

services:
  prometheus:
    image: prom/prometheus:v2.42.0
    user: 0:0
    ports:
      - 9090:9090
    volumes:
      - ./prometheus:/etc/prometheus
      - prometheus-data:/prometheus
    extra_hosts:
      host.docker.internal: host-gateway
    command: --web.enable-lifecycle  --config.file=/etc/prometheus/prometheus.yml


  grafana:
    image: grafana/grafana:8.2.2
    user: 0:0
    ports:
      - 3000:3000
    restart: unless-stopped
    volumes:
      - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
      - grafana-data:/var/lib/grafana

  alertmanager:
    image: prom/alertmanager:v0.23.0
    user: 0:0
    restart: unless-stopped
    ports:
      - "9093:9093"
    volumes:
      - "./alertmanager:/config"
      - alertmanager-data:/data
    command: --config.file=/config/alertmanager.yml --log.level=debug

volumes:
  prometheus-data:
  grafana-data:
  alertmanager-data:

Prometheus Configuration

The Prometheus configuration file, prometheus.yml, specifies the targets to scrape, the scrape interval, and the Alertmanager configuration. Here’s an example Prometheus configuration file:

global:
  scrape_interval:  10s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
    - scheme: http
      static_configs:
        - targets: ['alertmanager:9093']
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - rules.yml

scrape_configs:
  - job_name: 'springboot'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['host.docker.internal:8080']

As you can see, the pre-requisite is that you need to enable the Prometheus Actuator metrics on Spring Boot.

Besides, in a Docker environment, containers are isolated from each other, making it difficult for Prometheus to directly scrape metrics from a Spring Boot application running inside a container. To address this, the host.docker.internal address is used as a workaround.

The host.docker.internal address is a special hostname that resolves to the internal IP address of the Docker host. By using this address, Prometheus can reach the Spring Boot application running inside the container and scrape its metrics.

Grafana Configuration

In order to visualize metrics from Grafana in a Datasource component, we can add the following grafana.yml file which defines a Datasource configuration targeting Promethues default host and port:

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

You will find the full Docker Compose project at the end of this article. Right now, we will be running the Docker Compose tool to bootstrap our environment.

Bootstrap Prometheus and Grafana

In order to start our Containers, first make sure that Docker is running:

service docker start

Then, start the docker-compose.yml file:

docker-compose up

Finally, start a Spring Boot application which is configured to expose Prometheus Metrics, such as the one discussed in this article: Monitoring Spring Boot with Prometheus

mvn spring-boot:run

Check Prometheus Connectivity

Firstly, we will be checking that we are able to scrape metrics from the Spring Boot application. For this purpose, head to http://localhost:9090/targets and check that the Spring Boot application is connected:

how to monitor spring boot with prometheus and visualize metrics with grafana

Check the Alert Manager

Next, verify that no critical Alerts have been sent to the Alert Manager. Based on the following configuration, this Rule will trigger an Alert if the Spring Boot application is down:

groups:
- name: example
  rules:

  - alert: service_down
    expr: up{job="springboot"} == 0
    labels:
      severity: major
    annotations:
      description: Service {{ $labels.instance }} is unavailable.
      value: DOWN ({{ $value }})

For example, if you try to shutdown the Spring Boot application you should see the following Alert at http://localhost:9093/#/alerts

spring boot prometheus grafana monitoring

Visualize Metrics with Grafana

Finally, we will visualize the Metrics scraped by Prometheus with Grafana. For this purpose, head to the Grafana main page at:. localhost:3000

Login with the default user (admin/admin)

spring boot with grafana

Then, we need to configure the following components:

Grafana Datasource

A datasource is a connection point to a data source, such as Prometheus, MySQL, or InfluxDB. It allows Grafana to retrieve metrics from the data source and display them in dashboards.

Make sure you have the Prometheus Datasource we have configured. This datasource points to the default Prometheus server: localhost:9990

Grafana Dashboard

A dashboard is a collection of panels that visualize metrics from one or more datasources. It provides a centralized view of the health and performance of your applications and infrastructure.

In the context of Spring Boot monitoring, a Grafana datasource would connect to Prometheus, which scrapes metrics from the Spring Boot application. The dashboard would then display these metrics in charts and graphs, providing insights into the application’s performance.

  • Open Grafana and navigate to Dashboards.
  • Click the Create Dashboard button.
spring boot grafana tutorial
  • Give your dashboard a name, such as SpringBoot Monitoring Dashboard.
  • Click the + Add panel button to add a new panel to the dashboard.
  • In the Metrics panel, select the Prometheus data source and the springboot job.
  • Choose a metric to visualize, such as process_cpu_usage_percent or jvm_memory_used_bytes

For example, the following Dashboard captures a Metric from the task_time Timed method of our Spring Boot application:

spring boot monitoring with grafana

Conclusion

In conclusion, combining Prometheus and Grafana provides a powerful and versatile solution for monitoring Spring Boot applications. Prometheus efficiently collects metrics from Spring Boot applications, while Grafana effectively visualizes these metrics in dashboards. Alertmanager further enhances the monitoring capabilities by handling alerts generated by Prometheus, ensuring that critical events are not overlooked.

The sample Docker-compose project is available here: https://github.com/fmarchioni/masterspringboot/tree/master/grafana

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon