Add unit tests with mockito for metric stats (#38)

This commit is contained in:
Stephan Schnabel 2023-05-02 09:30:58 +02:00 committed by GitHub
parent cfce0dee45
commit 99a01e5c02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 424 additions and 9 deletions

View file

@ -30,21 +30,21 @@ public class MetricsStatsFactoryImpl implements MetricsStatsFactory {
@Override
public void postInit(KeycloakSessionFactory factory) {
if (!"true".equals(System.getenv().get("KC_METRICS_STATS_ENABLED"))) {
if (!"true".equals(getenv("KC_METRICS_STATS_ENABLED"))) {
log.infov("Keycloak stats not enabled.");
return;
}
var intervalDuration = Optional
.ofNullable(System.getenv("KC_METRICS_STATS_INTERVAL"))
.ofNullable(getenv("KC_METRICS_STATS_INTERVAL"))
.map(Duration::parse)
.orElse(Duration.ofSeconds(60));
var infoThreshold = Optional
.ofNullable(System.getenv("KC_METRICS_STATS_INFO_THRESHOLD"))
.ofNullable(getenv("KC_METRICS_STATS_INFO_THRESHOLD"))
.map(Duration::parse)
.orElse(Duration.ofMillis(Double.valueOf(intervalDuration.toMillis() * 0.5).longValue()));
var warnThreshold = Optional
.ofNullable(System.getenv("KC_METRICS_STATS_WARN_THRESHOLD"))
.ofNullable(getenv("KC_METRICS_STATS_WARN_THRESHOLD"))
.map(Duration::parse)
.orElse(Duration.ofMillis(Double.valueOf(intervalDuration.toMillis() * 0.75).longValue()));
log.infov("Keycloak stats enabled with interval of {0} and info/warn after {1}/{2}.",
@ -64,4 +64,8 @@ public class MetricsStatsFactoryImpl implements MetricsStatsFactory {
@Override
public void close() {}
String getenv(String key) {
return System.getenv().get(key);
}
}

View file

@ -43,7 +43,7 @@ public class MetricsStatsTask implements Provider, ScheduledTask {
scrape(session);
} catch (Exception e) {
if (e instanceof org.hibernate.exception.SQLGrammarException) {
log.infov("Metrics status task skipped, database not ready");
log.infov("Metrics status task skipped, database not ready.");
} else {
log.errorv(e, "Failed to scrape stats.");
}
@ -52,13 +52,13 @@ public class MetricsStatsTask implements Provider, ScheduledTask {
var duration = Duration.between(start, Instant.now());
if (duration.compareTo(interval) > 0) {
log.errorv("Finished scrapping keycloak stats in {0}, consider to increase interval", duration);
log.errorv("Finished scrapping keycloak stats in {0}, consider to increase interval.", duration);
} else if (duration.compareTo(warnThreshold) > 0) {
log.warnv("Finished scrapping keycloak stats in {0}, consider to increase interval", duration);
log.warnv("Finished scrapping keycloak stats in {0}, consider to increase interval.", duration);
} else if (duration.compareTo(infoThreshold) > 0) {
log.infov("Finished scrapping keycloak stats in {0}", duration);
log.infov("Finished scrapping keycloak stats in {0}.", duration);
} else {
log.debugv("Finished scrapping keycloak stats in {0}", duration);
log.debugv("Finished scrapping keycloak stats in {0}.", duration);
}
}