To extend Spring Boot Actuator with specific monitoring capabilities, developers can define custom endpoints. This process involves creating a class annotated with @Endpoint and defining operations using specific annotatinos for HTTP methods.
Defining the Endpoint Clas
The core component is a POJO marked with @Endpoint. The id attribute determines the URL segment used to access the endpoint. Operations are mapped to HTTP verbs using @ReadOperation for GET requests and @WriteOperation for POST requests.
package com.example.monitoring.endpoint;
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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
@Endpoint(id = "sysstatus")
public class SystemStatusEndpoint {
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private String environment = "production";
@ReadOperation
public Map<String, Object> getStatus() {
Map<String, Object> response = new HashMap<>();
response.put("timestamp", LocalDateTime.now().format(formatter));
response.put("environment", environment);
response.put("active", true);
return response;
}
@WriteOperation
public void updateEnvironment(String environment) {
this.environment = environment;
}
}
Registering the Endpoint Bean
While component scanning might detect the class automatically in some configurations, explicitly defining the bean with in a configuration class ensures proper conditional loading. This setup uses conditions to verify that the endpoint is enabled and that no other bean of the same type exists.
package com.example.monitoring.config;
import com.example.monitoring.endpoint.SystemStatusEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MonitoringConfig {
@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public SystemStatusEndpoint systemStatusEndpoint() {
return new SystemStatusEndpoint();
}
}
Verification and Usage
Once the application starts, the custom endpoint is exposed under the actuator base path. Assuming the default configuration, the endpoint is accessible at /actuator/sysstatus.
To retrieve the current status, send a GET request to the URL. The response will contain the JSON payload defined in the read operation. To modify the environment variable, send a POST request with the desired parameter. Subsequent GET requests will reflect the updated configuration value.
Ensure that the actuator exposure settings in application.properties or application.yml include the custom endpoint ID or use * to expose all available endpoints.
management:
endpoints:
web:
exposure:
include: "health,info,sysstatus"