Last active
October 22, 2015 17:39
-
-
Save DaneGardner/0107844a6e996beb311e to your computer and use it in GitHub Desktop.
Dropwizard healthchecks based on logged errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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