I am using Spring boot Actuator API for my project the having a health check endpoint, and enabled it by :
management.endpoints.web.base-path=/ management.endpoints.web.path-mapping.health=healthcheck
Mentioned here
Now I want to enable log in my application log file when ever the status of this above /healthcheck
fails and print the entire response from this end point.
What is the correct way to achieve this?
Advertisement
Answer
Best way is to extend the actuator endpoint with @EndpointWebExtension
. You can do the following;
@Component @EndpointWebExtension(endpoint = HealthEndpoint.class) public class HealthEndpointWebExtension { private HealthEndpoint healthEndpoint; private HealthStatusHttpMapper statusHttpMapper; // Constructor @ReadOperation public WebEndpointResponse<Health> health() { Health health = this.healthEndpoint.health(); Integer status = this.statusHttpMapper.mapStatus(health.getStatus()); // log here depending on health status. return new WebEndpointResponse<>(health, status); } }
More about actuator endpoint extending here, at 4.8. Extending Existing Endpoints