Custom Actuator Endpoint – Spring Boot actuators with Example

Normally an application gains access to a number of production-level services thanks to Spring Boot Actuator. There are numerous built-in endpoints for Spring Boot Actuators.


Actuators facilitate our interaction with and monitoring of your application. We may also construct our own endpoints with Spring Boot. We can enable and disable end points which are not required for our application.

Below are some of the Spring Boot Actuator endpoints,

EndPointDescription
/actuator/healthBasic health information of the application
/actuator/configpropslist of all @ConfigurationProperties
/actuator/infoArbitrary Information
/actuator/auditeventsaudit events of the application
/actuator/httptraceHTTP trace information
/actuator/metricsMetrics information of the application
/actuator/mappings@RequestMappings in the application
/actuator/scheduledtasksScheduled tasks in the application
/actuator/shutdownShutdowns the application

In this article, let us create our own custom actuator endpoint.

What are we going to do?

We are going to create a Custom Actuator Endpoint – one at Service level and another one at Controller level.

We will read the information from our application.properties file and VersionId and Articraft from Maven pom.xml.

Project Structure:

project structure

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.javainfinite</groupId>
	<artifactId>customactuator</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>customactuator</name>
	<description>SpringBoot Custom Actuator</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-web</artifactId>
		</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>

Information.java

package com.javainfinite.customactuator.model;

public class Information {

    private String versionId;
    private String artifactId;
    private String applicationName;
    private String status;

    public String getVersionId() {
        return versionId;
    }

    public void setVersionId(String versionId) {
        this.versionId = versionId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

application.properties:

spring.application.name=Actuator
spring.application.maven.version=@project.version@
spring.application.maven.artifactId=@project.artifactId@

Custom Actuator Endpoint – Service Level:

To create an actuator endpoint at Service level, we are going to use an annotation,

@EndPoint

ActuatorEndPoint.java

package com.javainfinite.customactuator.service;


import com.javainfinite.customactuator.model.Information;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "appinfo")
public class ActuatorEndPoint {

    @Value("${spring.application.maven.version}")
    private String versionId;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${spring.application.maven.artifactId}")
    private String artifactId;

    @ReadOperation
    public Information getApplicationInfo() {
        Information information = new Information();
        information.setApplicationName(applicationName);
        information.setVersionId(versionId);
        information.setArtifactId(artifactId);
        information.setStatus("Application status is running");
        return information;

    }
}

We are trying to read data from application.properties file – The application name, maven versionId and maven artifactId.

If you would like to know about various ways to reading data from application.properties file, please refer here.

Similar to @ReadOperation, we can also use @WriteOperation or @DeleteOperation based on our requirement.

But before running our application, we need to include our custom endpoint in application.properties file,

management.endpoints.web.exposure.include=appinfo

Our main class,

CustomactuatorApplication.java

package com.javainfinite.customactuator;

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

@SpringBootApplication
public class CustomactuatorApplication {

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

}

Let us run this application and call the actuator endpoint,

appinfo

We need to execute the endpoint with “/actuator/appinfo”.

We are able to see our custom actuator endpoint working.

Custom Actuator Endpoint – Controller Level:

For custom actuator endpoint at controller level, we will use the below annotation,

@RestControllerEndpoint

CustomActuatorController.java

package com.javainfinite.customactuator.controller;

import com.javainfinite.customactuator.model.Information;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RestControllerEndpoint(id = "restAppInfo")
public class CustomActuatorController {

    @Value("${spring.application.maven.version}")
    private String versionId;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${spring.application.maven.artifactId}")
    private String artifactId;;

    @GetMapping("/app")
    public Information getApplicationInformation() {
        Information information = new Information();
        information.setApplicationName(applicationName);
        information.setVersionId(versionId);
        information.setArtifactId(artifactId);
        information.setStatus("Application status is running");
        return information;
    }
}

We need to include this custom endpoint in application.properties,

management.endpoints.web.exposure.include=appinfo,restAppInfo

Now let us run this application and test this endpoint,

Code can be downloaded here.

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *