I have multiple health check in management.endpoint.health.group (foo, bar)
and I deploy my app within a kubernetes cluster,
What I want is when kube send me liveness request and the state of (foo, bar) is aggregated, at this moment (before replying) I want to run some logic based on this last state.
My Indicators are defined like this
@Component
public class FooHealthIndicator implements HealthIndicator {
@Override
public Health health() {
Health.Builder builder = new Health.Builder();
try {
// some foo specific logic
builder.up();
} catch (Exception exception) {
...
builder.down().withException(exception);
}
return builder.build();
}
the same goes for BarHealthIndicator
What you have to do is implementing a component which extends HealthEndpointWebExtension and where you can intercept the group aggregated status :
public class HealthEndpointCustomExtension extends HealthEndpointWebExtension {
private final Map<String,Status> lastStatusByGroup = new HashMap<>();
public HealthEndpointCustomExtension(HealthContributorRegistry registry, HealthEndpointGroups groups, Duration slowIndicatorLoggingThreshold) {
super(registry, groups, slowIndicatorLoggingThreshold);
}
@Override
public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, WebServerNamespace serverNamespace, SecurityContext securityContext, boolean showAll, String... path) {
WebEndpointResponse<HealthComponent> response = super.health(apiVersion, serverNamespace, securityContext, showAll, path);
if (path.length > 0) {
String group = path[0];
Status status = response.getBody().getStatus();
Status lastStatus = lastStatusByGroup.put(group, status);
if (lastStatus != null && !lastStatus.equals(status)) {
//..do some logic
}
}
return response;
}
}
Then you just have to declare a bean factory method for this component inside a configuration class :
@Bean
HealthEndpointWebExtension healthEndpointWebExtension(HealthContributorRegistry healthContributorRegistry,
HealthEndpointGroups groups, HealthEndpointProperties properties) {
return new HealthEndpointCustomExtension(healthContributorRegistry, groups,
properties.getLogging().getSlowIndicatorThreshold());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With