Spring Boot Actuators
Published: 2022-09-04 14:44:22

Spring boot actuator feature can be used to publish the application details, metrics, etc. that we can use to monitor the application easily.

We can use the actuator starter dependency to enable the actuator feature.

The below snippet shows the maven dependency.

Adding this dependency will autoconfigure the required configuration in the application and also enables the default actuator endpoints and health indicators.


<dependency>     <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>



**Actuator Endpoint**


Just by adding the actuator dependency, our application now has a lot of information exposed that would be very handy for debugging or general microservice insight.

Most endpoints are sensitive – meaning they’re not fully public – while a handful is not: /health and /info.


Description of Few Endpoints:

- /health: Exposes the application health status.

- /beans: Exposes the configured spring beans in the application context. Provides details like the scope of the bean, bean type, etc.

- /caches: Exposes available caches in the application.

- /env: Provides all available environment configuration property details.

- /configprops: Exposes all available configuration classes list.

- /mappings: Exposes all availabe HTTP request mapping details in the application.

- /metrics: Exposes application metrics like JVM metrics, system metrics, and also the tomcat server metrics, etc.

- /heapdump: Provides the application heap dump.

- /threaddump: Exposes the thread information.

- /loggers: Exposes the logging application configuration information like log level, etc.

- /logfile: For web applications, this endpoint returns the logfile content. We can also retrieve only a part of the log file.

- /shutdown: We can use this endpoint to gracefully shut down the spring boot application. This is disabled by default.


**Include or exclude actuator endpoints**

```
#To include all the default web endpoints:
management.endpoints.web.exposure.include=*
 
#To include specific web endpoints:
management.endpoints.web.exposure.include=health,info
 
#To exclude specific web endpoints:
management.endpoints.web.exposure.exclude=beans

```

**changing the actuator http management port**

```
management.server.port=9080
```


**Creating Custom Actuator Endpoint**

```
package com.actuatorsample;
 
import java.util.HashMap;
import java.util.Map; 
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
 
@Component
@Endpoint(id = "sampleEndpoint")
public class CustomActuator {
 
    @ReadOperation
    public Map<String, String> readEndpoint() {
        Map<String, String> map = new HashMap<>();
        map.put("readMessage", "This is our sample actuator endpoint.!!");
        return map;
    }
 
    @WriteOperation
    public Map<String, String> writeEndpoint(String value) {
 
        Map<String, String> map = new HashMap<>();
        map.put("writeMessage", "This is sample actuator write endpoint.!!" + value);
        return map;
    }
 
    @DeleteOperation
    public Map<String, String> deleteEndpoint() {
        Map<String, String> map = new HashMap<>();
        map.put("readMessage", "This is sample actuator delete endpoint.!!");
        return map;
    }
}
```