Let say you need a specific actuator for your Spring-Boot backend. This small post show you how to extend the interface HealthIndicator
to create your custom actuator.
First you’ll have implement the HealthIndicator
interface:
public class CustomHealthIndicator implements HealthIndicator {
// You can inject all the others HealthIndicators if your custom actuators need it for your processing
private final List<HealthIndicator> healthIndicators;
public CustomHealthIndicator(List<HealthIndicator> healthIndicators) {
this.healthIndicators = healthIndicators;
}
@Override
public Health health() {
Map<String, ?> details = Map.of();
...
return Health.up().withDetails(details).build();
}
}
Now you will try to make your custom actuator load at the start of you application, here is the “auto-configuration” you can use.
@Configuration
public class CustomHealthIndicatorAutoConfiguration {
@ConditionalOnProperty(prefix = "custom-actuator", name = "enable", havingValue = "true", matchIfMissing = false)
@Bean("CustomHealthIndicator")
public HealthIndicator getCustomHealth(@Autowired List<HealthIndicator> healthIndicators) {
return new CustomHealthIndicator(healthIndicators);
}
}
Leave a Reply