Search code examples
springspring-boot

Possible to access HealthIndicator methods as endpoints?


By implementing HealthIndicator you can add custom health checks using Spring Boot and its actuator module.

These checks are listed all together using the /health endpoint.

Is it possible to request these custom health endpoints, for example /health/myCustomCheck?


Solution

  • The short answer is no. That is not provided by the actuator. You can verify this by calling the /mappings endpoint and seeing that only /health is mapped.

    Now the long answer is you could add your own endpoint that invokes your custom health indicator. This isn't too hard to do. Spring will pick up any beans of type Endpoint for you and map them. A rough example would be something like this

    @Bean
    public Endpoint<Object> customHealthEndpoint(final MyHealthCheck health) {
    
        return new Endpoint<Object>() {
    
            @Override
            public String getId() {
                return "myHealthCheck";
            }
    
            @Override
            public Object invoke() {
               return health.health().getStatus();
            }
    
            @Override
            public boolean isEnabled() {
                return true;
            }
    
            @Override
            public boolean isSensitive() {
                return false;
            }
    
        };
    }
    

    You can then call /myHealthCheck and get back {"status":"UP"}.