com.oath.micro.server.health.HealthChecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro-error-codes Show documentation
Show all versions of micro-error-codes Show documentation
Plugin for handling errors in a standardized way
package com.oath.micro.server.health;
import java.util.Queue;
import cyclops.control.Maybe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HealthChecker {
private final long timeThresholdForNormal;
@Autowired
public HealthChecker(
@Value("${health.check.time.threshold.from.error.to.normal:360000}") long timeThresholdForNormalInMillis) {
this.timeThresholdForNormal = timeThresholdForNormalInMillis;
}
HealthStatus checkHealthStatus(final Queue errors, final Queue fatal) {
Maybe latest = selectLatestError(errors, fatal);
HealthStatus.State state = state(latest);
final HealthStatus status = new HealthStatus(
state, errors, fatal);
return status;
}
private Maybe selectLatestError(Queue errors, Queue fatal) {
if (errors.size() == 0 && fatal.size() == 0) {
return Maybe.nothing();
}
if (fatal.size() > 0) {
return Maybe.just(fatal.peek());
}
return Maybe.just(errors.peek());
}
private HealthStatus.State state(Maybe errors) {
return errors.fold(this::handleError, () -> HealthStatus.State.Ok);
}
private HealthStatus.State handleError(ErrorEvent event) {
return event.visit(e -> HealthStatus.State.Fatal, e -> {
if (System.currentTimeMillis() - event.getTime()
.getTime() < timeThresholdForNormal) {
return HealthStatus.State.Errors;
} else {
return HealthStatus.State.Ok;
}
});
}
}