org.zodiac.actuate.health.reactive.AppReactiveDiscoveryClientHealthIndicator Maven / Gradle / Ivy
package org.zodiac.actuate.health.reactive;
import static java.util.Collections.emptyList;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient;
import org.zodiac.core.bootstrap.discovery.health.AppDiscoveryClientHealthIndicatorInfo;
import org.zodiac.core.event.discovery.AppInstanceRegisteredEvent;
import reactor.core.publisher.Mono;
public class AppReactiveDiscoveryClientHealthIndicator
implements AppReactiveDiscoveryHealthIndicator, Ordered, ApplicationListener> {
private final ReactiveAppDiscoveryClient discoveryClient;
private final AppDiscoveryClientHealthIndicatorInfo discoveryClientHealthIndicatorInfo;
private final Logger log = LoggerFactory.getLogger(AppReactiveDiscoveryClientHealthIndicator.class);
private AtomicBoolean discoveryInitialized = new AtomicBoolean(false);
private int order = Ordered.HIGHEST_PRECEDENCE;
public AppReactiveDiscoveryClientHealthIndicator(ReactiveAppDiscoveryClient discoveryClient,
AppDiscoveryClientHealthIndicatorInfo properties) {
this.discoveryClient = discoveryClient;
this.discoveryClientHealthIndicatorInfo = properties;
}
@Override
public void onApplicationEvent(AppInstanceRegisteredEvent> event) {
if (this.discoveryInitialized.compareAndSet(false, true)) {
this.log.debug("Discovery Client has been initialized");
}
}
@Override
public Mono health() {
if (this.discoveryInitialized.get()) {
return doHealthCheck();
} else {
return Mono
.just(Health.status(new Status(Status.UNKNOWN.getCode(), "Discovery Client not initialized")).build());
}
}
private Mono doHealthCheck() {
// @formatter:off
return Mono.just(this.discoveryClientHealthIndicatorInfo.isUseServicesQuery())
.flatMap(useServices -> useServices ? doHealthCheckWithServices() : doHealthCheckWithProbe())
.onErrorResume(exception -> {
this.log.error("Error", exception);
return Mono.just(Health.down().withException(exception).build());
});
// @formatter:on
}
private Mono doHealthCheckWithProbe() {
// @formatter:off
return Mono.justOrEmpty(this.discoveryClient)
.flatMap(client -> {
client.probe();
return Mono.just(client);
})
.map(client -> {
String description = (this.discoveryClientHealthIndicatorInfo.isIncludeDescription()) ? client.clientDescription() : "";
return Health.status(new Status(Status.UP.getCode(), description)).build();
});
// @formatter:on
}
private Mono doHealthCheckWithServices() {
// @formatter:off
return Mono.justOrEmpty(this.discoveryClient)
.flatMapMany(ReactiveAppDiscoveryClient::getServiceList)
.collectList()
.defaultIfEmpty(emptyList())
.map(services -> {
String description = (this.discoveryClientHealthIndicatorInfo.isIncludeDescription()) ?
this.discoveryClient.clientDescription() : "";
return Health.status(new Status(Status.UP.getCode(), description))
.withDetail("services", services).build();
});
// @formatter:on
}
@Override
public String getName() {
return discoveryClient.clientDescription();
}
@Override
public int getOrder() {
return this.order;
}
public void setOrder(int order) {
this.order = order;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy