Skip to content

Instantly share code, notes, and snippets.

@DaneGardner
Last active October 22, 2015 17:39
Show Gist options
  • Save DaneGardner/0107844a6e996beb311e to your computer and use it in GitHub Desktop.
Save DaneGardner/0107844a6e996beb311e to your computer and use it in GitHub Desktop.
Dropwizard healthchecks based on logged errors
@Override
public void doRun(SwitchboardConfiguration configuration, Environment environment) throws Exception {
Optional<Meter> allLogs = Optional.fromNullable( environment.metrics().getMeters().get("ch.qos.logback.core.Appender.all") );
Optional<Meter> warnLogs = Optional.fromNullable( environment.metrics().getMeters().get("ch.qos.logback.core.Appender.warn") );
Optional<Meter> errorLogs = Optional.fromNullable( environment.metrics().getMeters().get("ch.qos.logback.core.Appender.error") );
if(allLogs.isPresent()) {
if(errorLogs.isPresent()) registerLogMeterAsHealthCheck(environment, allLogs.get(), errorLogs.get(), "logged_errors", 0.05D);
if(warnLogs.isPresent()) registerLogMeterAsHealthCheck(environment, allLogs.get(), warnLogs.get(), "logged_warns", 0.25D);
}
}
/**
* Registers logged event meters as a health check by comparing the five minute rates for all logs versus the supplied metric
* @param environment yup
* @param allMeter meter for all logs
* @param meter meter for error or warn logs (recommended)
* @param name name of healthcheck
* @param threshold is inclusive
*/
private void registerLogMeterAsHealthCheck(Environment environment, Meter allMeter, Meter meter, String name, Double threshold) {
environment.healthChecks().register(name, new HealthCheck() {
@Override protected Result check() throws Exception {
Double ratio = (meter.getFiveMinuteRate() / Double.max(allMeter.getFiveMinuteRate(), Double.MIN_VALUE)) * 100.0D;
String message = ratio + "%";
if(ratio >= threshold) return HealthCheck.Result.unhealthy(message);
else return HealthCheck.Result.healthy(message);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment