Sunday, 22 June 2025

Spring Boot and Prometheus Integration Step by Step Guide

In today’s modern software systems, especially microservices and cloud-native applications, observability is key to maintaining performance, reliability, and user satisfaction. You need a way to measure what your application is doing internally, and that’s where Prometheus comes in.

 

What is Prometheus?

Prometheus is an open-source monitoring and alerting toolkit originally developed at SoundCloud. It’s now part of the Cloud Native Computing Foundation (CNCF), just like Kubernetes.

 

Prometheus works by scraping metrics from instrumented applications at defined intervals and storing them in a time-series database. You can then use tools like Grafana to visualize and analyze these metrics.

 

 

Why to Enable Prometheus in a Spring Boot App?

Spring Boot provides a powerful Actuator module that can expose a wide range of application metrics out of the box, including memory usage, thread count, CPU usage, HTTP request timings, database stats, and more.

 

By integrating Micrometer (a metrics instrumentation library used by Spring) with Prometheus, you can:

 

·      Monitor application health and performance

·      Set up alerts when things go wrong

·      Visualize metrics over time in Grafana

·      Track usage patterns and optimize performance

·      Debug issues more effectively with data-backed insights

 

Common Use Cases

·      Monitoring API response times

·      Tracking number of active threads or connections

·      Checking JVM memory or GC stats

·      Measuring custom business metrics, e.g., number of orders placed, login attempts, etc.

·      Enabling auto-scaling decisions in Kubernetes based on metrics

 

Follow below step-by-step procedure to build the complete working application.

 

Step 1: Create new maven project ‘spring-boot-prometheus-integration’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml 

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>spring-boot-prometheus-integration</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.source>21</maven.compiler.source>
		<maven.compiler.target>21</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.4.4</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<dependencies>
		<!-- Spring Boot Actuator for exposing metrics -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- Micrometer registry for Prometheus -->
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
		</dependency>

		<!-- Your usual dependencies like web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.11.0</version> <!-- or latest -->
				<configuration>
					<release>21</release>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

Step 3: Create application.yml file in src/main/resources and add below content.

 

application.yml

 

management:
  endpoints:
    web:
      exposure:
        include: health,info,prometheus
  metrics:
    export:
      prometheus:
        enabled: true

Step 4: Define HelloController class.

 

HelloController.java

 

package com.sample.app.controller;

import io.micrometer.core.annotation.Timed;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @Timed(value = "hello.request", description = "Time taken to return hello")
  @GetMapping("/hello")
  public String hello() {
    return "Hello, Prometheus!";
  }
}

Step 5: Define App.java

 

App.java

 

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }
}

 

Open terminal, navigate to the pom.xml file where this project is located, and execute following command.

mvn clean install

 

It generates spring-boot-prometheus-integration-0.0.1-SNAPSHOT.jar file, execute below command to run the application.

 

java -jar ./target/spring-boot-prometheus-integration-0.0.1-SNAPSHOT.jar

 

Open the url ‘http://localhost:8080/actuator/prometheus’ in browser, you can see following metrics screen.

 


Let’s configure /actuator/prometheus endpoint in prometheus.yml file and start reading these metrics from Prometheus UI.  

scrape_configs:
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']

 

Final prometheus.yml file.

 

prometheus.yml

 

global:
  scrape_interval: 15s
  evaluation_interval: 15s

rule_files:
  - "rules_with_labels.yaml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'grafana'
    static_configs:
      - targets: ['localhost:3000']
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']

 

Open terminal and execute below command to start the Prometheus server.

 

prometheus --config.file=./prometheus.yml

 

Open the Prometheus targets url to see all the configured targets.

 

http://localhost:9090/targets

 


You can start firing the queries like process_cpu_time_ns_total to extract the metrics published to Prometheus from Java Spring Boot Application.  

 


You can download this Application from this link.


 

 

Note

Micrometer is a metrics instrumentation library for Java applications. It provides a simple, unified API to collect and expose application metrics such as counters, gauges, timers, and distribution summaries.

 

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment