Register Custom Health Indicators In Spring Boot Application
Hello everyone, Hope you are doing well. In this post, we will learn how to register custom health indicators in the Spring Boot application via Spring Actuator.
A little bit of Background
Spring Boot Actuator
Spring Boot Actuator is a sub-project of Spring Boot. It adds several production-grade services to your application with little effort on your part. It is used to expose operational information about the running application — health, metrics, info, dump, env, etc.
Spring Boot Actuator is a sub-project of Spring Boot. It adds several production-grade services to your application with little effort on your part. It is used to expose operational information about the running application — health, metrics, info, dump, env, etc.
HealthIndicator
The Health indicator strategy interface. It is used to contribute Health to the results returned from the HealthEndpoint.
The Health indicator strategy interface. It is used to contribute Health to the results returned from the HealthEndpoint.
AbstractHealthIndicator
Base HealthIndicator implementations that encapsulate the creation of Health instance and error handling. This implementation is only suitable if an Exception raised from doHealthCheck(org.springframework.boot.actuate.health.Health.Builder) should create a Status.DOWN health status.
Base HealthIndicator implementations that encapsulate the creation of Health instance and error handling. This implementation is only suitable if an Exception raised from doHealthCheck(org.springframework.boot.actuate.health.Health.Builder) should create a Status.DOWN health status.
Creating a simple Spring boot project
First, open the Spring initializr https://start.spring.io/
Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-actuator-custom-health-indicator. Here I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency, Spring Boot Actuator, Spring Data JPA, and H2 Database.
Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-actuator-custom-health-indicator) file and downloads the project.
Then, Extract the Zip file.
Import the project on your favourite IDE.
Then, run the Spring boot application.
mvn clean install
mvn spring-boot:run
mvn clean install
mvn spring-boot:run
Unlike in previous versions, Actuator comes with most endpoints disabled except the '/health' endpoint. After the application startup, access http://localhost:8080/actuator/health and the status should be UP.
First, verify the endpoint http://localhost:8080/actuator
Then, verify the endpoint http://localhost:8080/actuator/health
management.endpoint.health.show-details=always
Then, restart the spring boot application and verify the endpoint http://localhost:8080/actuator/health. Now the response would look like the below. Usually, spring starters come with their own health indicator auto-configurations.
Create a Custom Health Indicator
Option 1: extend the AbstractHealthIndicator
import java.util.Random;import org.springframework.boot.actuate.health.AbstractHealthIndicator;import org.springframework.boot.actuate.health.Health;import org.springframework.stereotype.Component;
@Componentpublic class VowelHealthIndicator2 extends AbstractHealthIndicator {
@Override protected void doHealthCheck(Health.Builder builder) {
char character = randomGenerator(); switch (character) { case 'a': builder.up().withDetail("status", character + " is vowel").build(); case 'e': builder.up().withDetail("status", character + " is vowel").build(); case 'i': builder.up().withDetail("status", character + " is vowel").build(); case 'o': builder.up().withDetail("status", character + " is vowel").build(); case 'u': builder.up().withDetail("status", character + " is vowel").build(); break; default: builder.down().withDetail("error", character + " is consonant").build();
}
}
private static char randomGenerator() {
Random r = new Random(); String alphabet = "abcdefgiopuxlm"; return alphabet.charAt(r.nextInt(alphabet.length())); }}
Option 2: implement the HealthIndicator
import java.util.Random;import org.springframework.boot.actuate.health.Health;import org.springframework.boot.actuate.health.HealthIndicator;import org.springframework.stereotype.Component;
@Componentpublic class VowelHealthIndicator1 implements HealthIndicator {
@Override public Health health() {
char character = randomGenerator(); switch (character) { case 'a': return Health.up().withDetail("status", character + " is vowel").build(); case 'e': return Health.up().withDetail("status", character + " is vowel").build(); case 'i': return Health.up().withDetail("status", character + " is vowel").build(); case 'o': return Health.up().withDetail("status", character + " is vowel").build(); case 'u': return Health.up().withDetail("status", character + " is vowel").build(); default: return Health.down().withDetail("error", character + " is consonant").build();
}
}
private static char randomGenerator() {
Random r = new Random(); String alphabet = "abcdefgiopuxlm"; return alphabet.charAt(r.nextInt(alphabet.length())); }}
The above definitions create two beans named vowelHealthIndicator1 and vowelHealthIndicator2 and will be picked by AutoConfiguredHealthContributorRegistry.
This can be confirmed by seeing the custom health component at http://localhost:8080/actuator/health. Now the response would look like the below.
- Status.UP
- Status.DOWN
- Status.OUT_OF_SERVICE
- Status.UNKNOWN
Complete pom.xml.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.knf.dev.demo</groupId> <artifactId>spring-actuator-custom-health-indicator</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-actuator-custom-health-indicator</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
Local Setup and Run the application
Step 1: Download or clone the source code from GitHub to the local machine - Click here
Step 2: mvn clean install
Step 3: Run the Spring Boot application -
mvn spring-boot:run
Step 1: Download or clone the source code from GitHub to the local machine - Click here
Step 2: mvn clean install
Step 3: Run the Spring Boot application -
mvn spring-boot:run