Spring Boot + Prometheus + Grafana Example (2024)
Spring Boot Actuator Tutorial :
- Spring Boot Actuator Example
- Spring Boot Actuator + Prometheus + Grafana Example
- Prometheus Interview Questions and Answers
- Grafana Interview Questions and Answers
In previous tutorial we have demonstrated Spring Boot Actuator, with multiple actuator endpoints.
This tutorial will explain how to integrate a spring boot actuator with Prometheus, a monitoring tool, and Grafana , a graphing solution.
Q: What is Prometheus?
Ans:
SoundCloud developed Prometheus, an open-source system monitoring and alerting toolkit. Prometheus gathers and saves metrics as time series data, which means that metrics data is kept alongside the timestamp at which it was captured, as well as optional key-value pairs known as labels.
Features
- time series data identifiable by metric name and key/value pairs in a multi-dimensional data model
- PromQL is a versatile query language that takes advantage of this dimensionality.
- There is no need for distributed storage because single server nodes are self-contained.
- The time series data is collected using a pull mechanism over HTTP.
- An intermediary gateway is used to push time series.
- Service discovery or static configuration are used to find targets.
- Multiple graphing and dashboarding options are supported.
What are metrics, and why are they important?
Ans:
In layman's words, metrics are numerical measurements, and time series refers to changes over time. The metrics that users wish to track differ from one app to the next. It could be request timings for a web server, number of active connections or active queries for a database, and so forth.
What is Grafana?
Feature-rich customizable dashboards can be set to display data from a variety of databases using visualisation tools like Heatmaps, Prometheus, Graphite. The platform is adaptable and simple to operate. A wide number of databases are supported natively.
For storing and visualising time series data, the combination of OSS grafana and Prometheus is becoming a more and more typical monitoring stack used by DevOps teams. The storage backend is Prometheus, while the analysis and visualisation frontend is open source grafana.
Now, let's start with the development.
Maven Dependencies
You must add the micrometer-registry-prometheus
dependency to integrate actuator with
Prometheus.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-actuator-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-actuator-example</name>
<description>Spring Boot Actuator Example Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Micrometer Prometheus registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Test the Actuator Endpoint
All application metrics data are accessible via an actuator endpoint named /prometheus
.
The Prometheus server also pull this endpoint on a regular basis to obtain metrics data.
The prometheus endpoint will begin to appear on the actuator endpoint-discovery page (http://localhost:8080/actuator).
Test the Prometheus Endpoint
The prometheus endpoint displays metrics information in a form that can be collected by a Prometheus server. To see the available metrics data, go to the prometheus endpoint at http://localhost:8080/actuator/prometheus.
Take a look at our suggested posts:
Install and Setup Prometheus
Prometheus dashboard
Follow the steps given in Prometheus dashboard page, to see the below dashboard.
System's CPU usage
-system_cpu_usage
Api Latency
When we start the Spring Boot Prometheus Example, and visit to http://localhost:8080/actuator/prometheus, we can see the statistics of the application.
Refer
http_server_requests_second_max{uri="/testApi"}
expression in Prometheus
graph to
see the test api latency.
Install and run Grafana
Follow the steps given here to download and run Grafana on your system.
Configure Grafana to load Prometheus metrics data
To import metrics from Prometheus and visualise them on Grafana, follow the steps given here.Finally, you can see a grafana graph consuming Prometheus data.
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot Actuator + Prometheus + Grafana Example