Merge branch 'stable-2.14' into stable-2.15
* stable-2.14:
Add ability to disable healthchecks
Change-Id: I20542eb085b815f40e8b5c5ad65cbf22dfd2274e
diff --git a/README.md b/README.md
index fe38bc8..f375030 100644
--- a/README.md
+++ b/README.md
@@ -46,8 +46,9 @@
- ts: epoch timestamp in millis of the individual check
- elapsed: elapsed time in millis to complete the check
- result: result of the health check
-
+
- passed: the check passed successfully
+ - disabled: the check was disabled
- failed: the check failed with an error
- timeout: the check took too long and timed out
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
index d7ab48e..b92588b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfig.java
@@ -14,6 +14,8 @@
package com.googlesource.gerrit.plugins.healthcheck;
+import static com.google.common.base.Preconditions.checkNotNull;
+
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
@@ -40,6 +42,7 @@
private static final int LIMIT_DEFAULT = 10;
private static final String USERNAME_DEFAULT = "healthcheck";
private static final String PASSWORD_DEFAULT = "";
+ private static final boolean HEALTH_CHECK_ENABLED_DEFAULT = true;
private final AllProjectsName allProjectsName;
private final AllUsersName allUsersName;
@@ -107,6 +110,11 @@
return getStringWithFallback("password", healthCheckName, PASSWORD_DEFAULT);
}
+ public boolean healthCheckEnabled(String healthCheckName) {
+ return config.getBoolean(
+ HEALTHCHECK, checkNotNull(healthCheckName), "enabled", HEALTH_CHECK_ENABLED_DEFAULT);
+ }
+
private String getStringWithFallback(
String parameter, String healthCheckName, String defaultValue) {
String fallbackDefault =
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
index 26ef1ee..e8aeb95 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
@@ -29,12 +29,14 @@
private final String name;
private final ListeningExecutorService executor;
protected StatusSummary latestStatus;
+ protected boolean enabled;
protected AbstractHealthCheck(
ListeningExecutorService executor, HealthCheckConfig config, String name) {
this.executor = executor;
this.name = name;
this.timeout = config.getTimeout(name);
+ this.enabled = config.healthCheckEnabled(name);
this.latestStatus = StatusSummary.INITIAL_STATUS;
}
@@ -51,7 +53,7 @@
() -> {
Result healthy;
try {
- healthy = doCheck();
+ healthy = enabled ? doCheck() : Result.DISABLED;
} catch (Exception e) {
log.warn("Check {} failed", name, e);
healthy = Result.FAILED;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
index 4f6a532..858e142 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheck.java
@@ -15,6 +15,9 @@
package com.googlesource.gerrit.plugins.healthcheck.check;
import com.google.gson.annotations.SerializedName;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
public interface HealthCheck {
@@ -24,7 +27,9 @@
@SerializedName("failed")
FAILED,
@SerializedName("timeout")
- TIMEOUT;
+ TIMEOUT,
+ @SerializedName("disabled")
+ DISABLED;
}
public class StatusSummary {
@@ -33,6 +38,8 @@
public final Result result;
public final long ts;
public final long elapsed;
+ public static final Set<Result> failingResults =
+ new HashSet(Arrays.asList(Result.FAILED, Result.TIMEOUT));
public StatusSummary(Result result, long ts, long elapsed) {
this.result = result;
@@ -41,7 +48,7 @@
}
public Boolean isFailure() {
- return this.result != Result.PASSED;
+ return failingResults.contains(this.result);
}
}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index c150f93..2b7a224 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -49,7 +49,10 @@
- `projectslist` : check the ability to list projects with their descriptions
- `auth`: check the ability to authenticate with username and password
-The follwing parameters are available:
+Each check name can be disabled by setting the `enabled` parameter to **false**,
+by default this parameter is set to **true**
+
+The following parameters are available:
- `healthcheck.<checkName>.timeout` : Specific timeout (msec) for the
healthcheck to complete. Zero means that there is no timeout.
@@ -71,11 +74,11 @@
to the default ones that are always included.
Default: All-Projects, All-Users
-
+
- `healthcheck.auth.username` : Username to use for authentication
-
+
Default: healthcheck
-
+
- `healthcheck.auth.password` : Password to use for authentication
-
+
Default: no password
\ No newline at end of file
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
index 102e246..264db94 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckConfigTest.java
@@ -61,4 +61,28 @@
assertThat(config.getTimeout("fooCheck")).isEqualTo(1000);
assertThat(config.getTimeout("barCheck")).isEqualTo(2000);
}
+
+ @Test
+ public void shouldHaveAnEnabledValue() {
+ HealthCheckConfig config =
+ new HealthCheckConfig("[healthcheck \"fooCheck\"]\n" + "enabled=false");
+
+ assertThat(config.healthCheckEnabled("fooCheck")).isEqualTo(false);
+ }
+
+ @Test
+ public void shouldHaveEnabledAndDisabledValue() {
+ HealthCheckConfig config =
+ new HealthCheckConfig(
+ "[healthcheck \"fooCheck\"]\n"
+ + "enabled=false\n"
+ + "[healthcheck \"barCheck\"]\n"
+ + "timeout=1000"
+ + "[healthcheck \"bazCheck\"]\n"
+ + "enabled=true\n");
+
+ assertThat(config.healthCheckEnabled("fooCheck")).isEqualTo(false);
+ assertThat(config.healthCheckEnabled("barCheck")).isEqualTo(true);
+ assertThat(config.healthCheckEnabled("bazCheck")).isEqualTo(true);
+ }
}