Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable logging in spring boot actuator health check API

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?

like image 710
Amandeep Singh Avatar asked Mar 04 '19 05:03

Amandeep Singh


People also ask

How do I enable actuator health in spring boot application?

2.1. Getting Started. To enable Spring Boot Actuator, we just need to add the spring-boot-actuator dependency to our package manager.

How do I turn on my actuator health?

You probably need to expose actuator endpoints in application. yaml file. And if you have security enabled, you need to write you own SecurityFilterChain implementation in which you will disable security on all Actuator endpoints, or in your case only on those that you exposed in your application. yaml file.


1 Answers

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

like image 151
buræquete Avatar answered Sep 20 '22 23:09

buræquete