mirror of https://github.com/jenkinsci/jenkins.git
[JENKINS-75530] Implement dedicated healthcheck endpoint (#10522)
This commit is contained in:
commit
f68ec5bfdc
|
@ -27,6 +27,7 @@ package jenkins.diagnostics;
|
|||
import hudson.Extension;
|
||||
import hudson.init.InitMilestone;
|
||||
import hudson.model.AdministrativeMonitor;
|
||||
import jenkins.health.HealthCheck;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.Symbol;
|
||||
import org.kohsuke.accmod.Restricted;
|
||||
|
@ -49,9 +50,26 @@ public class CompletedInitializationMonitor extends AdministrativeMonitor {
|
|||
|
||||
@Override
|
||||
public boolean isActivated() {
|
||||
return !isInitCompleted();
|
||||
}
|
||||
|
||||
private static boolean isInitCompleted() {
|
||||
final Jenkins instance = Jenkins.get();
|
||||
// Safe to check in such way, because monitors are being checked in UI only.
|
||||
// So Jenkins class construction and initialization must be always finished by the call of this extension.
|
||||
return instance.getInitLevel() != InitMilestone.COMPLETED;
|
||||
return instance.getInitLevel() == InitMilestone.COMPLETED;
|
||||
}
|
||||
|
||||
@Extension
|
||||
public static final class HealthCheckImpl implements HealthCheck {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "completedInitialization";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check() {
|
||||
return isInitCompleted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2025, CloudBees, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package jenkins.health;
|
||||
|
||||
import hudson.ExtensionPoint;
|
||||
import org.kohsuke.accmod.Restricted;
|
||||
import org.kohsuke.accmod.restrictions.Beta;
|
||||
|
||||
/**
|
||||
* <p>Specifies a health check that is essential for Jenkins to function properly.
|
||||
* <br>If this health check fails, monitoring systems will treat the controller as unusable for meaningful tasks.
|
||||
* <br>It is assumed that restarting Jenkins can resolve the issue.
|
||||
* <p>For instance, low disk space is not a good health check because it can't be solved by restarting Jenkins.
|
||||
* <br>Detecting a deadlock in a critical singleton thread would be a good health check as long as associated information (thread dump) is reported somewhere before restarting.
|
||||
* @since TODO
|
||||
*/
|
||||
@Restricted(Beta.class)
|
||||
public interface HealthCheck extends ExtensionPoint {
|
||||
|
||||
/**
|
||||
* @return the name of the health check. Must be unique among health check implementations.
|
||||
* Defaults to the fully qualified class name.
|
||||
*/
|
||||
default String getName() {
|
||||
return getClass().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the health check passed.
|
||||
*/
|
||||
boolean check();
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2025, CloudBees, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package jenkins.health;
|
||||
|
||||
import hudson.Extension;
|
||||
import hudson.ExtensionList;
|
||||
import hudson.model.InvisibleAction;
|
||||
import hudson.model.UnprotectedRootAction;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.kohsuke.accmod.Restricted;
|
||||
import org.kohsuke.accmod.restrictions.NoExternalUse;
|
||||
import org.kohsuke.stapler.HttpResponse;
|
||||
import org.kohsuke.stapler.json.JsonHttpResponse;
|
||||
|
||||
/**
|
||||
* Provides a health check action for Jenkins.
|
||||
*/
|
||||
@Extension
|
||||
@Restricted(NoExternalUse.class)
|
||||
public final class HealthCheckAction extends InvisibleAction implements UnprotectedRootAction {
|
||||
|
||||
@Override
|
||||
public String getUrlName() {
|
||||
return "health";
|
||||
}
|
||||
|
||||
public HttpResponse doIndex() {
|
||||
boolean success = true;
|
||||
var failing = new JSONArray();
|
||||
for (var healthCheck : ExtensionList.lookup(HealthCheck.class)) {
|
||||
var check = healthCheck.check();
|
||||
success &= check;
|
||||
if (!check) {
|
||||
failing.add(healthCheck.getName());
|
||||
}
|
||||
}
|
||||
var payload = new JSONObject().element("status", success);
|
||||
if (!success) {
|
||||
payload = payload.element("failures", failing);
|
||||
}
|
||||
return new JsonHttpResponse(payload, success ? 200 : 503);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2025, CloudBees, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package jenkins.health;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import hudson.ExtensionList;
|
||||
import java.util.logging.Level;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
import org.jvnet.hudson.test.LoggerRule;
|
||||
import org.jvnet.hudson.test.TestExtension;
|
||||
|
||||
|
||||
public class HealthCheckActionTest {
|
||||
@Rule
|
||||
public JenkinsRule r = new JenkinsRule();
|
||||
|
||||
@Rule
|
||||
public LoggerRule loggingRule = new LoggerRule().record(HealthCheckAction.class, Level.WARNING).capture(10);
|
||||
|
||||
@Test
|
||||
public void healthCheck() throws Exception {
|
||||
try (var webClient = r.createWebClient()) {
|
||||
var page = webClient.goTo(healthUrl(), "application/json");
|
||||
assertThat(page.getWebResponse().getStatusCode(), is(200));
|
||||
assertEquals(JSONObject.fromObject("""
|
||||
{
|
||||
"status": true
|
||||
}
|
||||
"""), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static String healthUrl() {
|
||||
return ExtensionList.lookupSingleton(HealthCheckAction.class).getUrlName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void healthCheckSuccessExtension() throws Exception {
|
||||
try (var webClient = r.createWebClient()) {
|
||||
var page = webClient.goTo(healthUrl(), "application/json");
|
||||
assertThat(page.getWebResponse().getStatusCode(), is(200));
|
||||
assertEquals(JSONObject.fromObject("""
|
||||
{
|
||||
"status": true
|
||||
}
|
||||
"""), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
|
||||
}
|
||||
}
|
||||
|
||||
@TestExtension({"healthCheckSuccessExtension", "healthCheckFailingExtension"})
|
||||
public static class SuccessHealthCheck implements HealthCheck {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "success";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void healthCheckFailingExtension() throws Exception {
|
||||
try (var webClient = r.createWebClient()) {
|
||||
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
|
||||
webClient.getOptions().setPrintContentOnFailingStatusCode(false);
|
||||
var page = webClient.goTo(healthUrl(), "application/json");
|
||||
assertThat(page.getWebResponse().getStatusCode(), is(503));
|
||||
assertEquals(JSONObject.fromObject("""
|
||||
{
|
||||
"status": false,
|
||||
"failures": ["failing"]
|
||||
}
|
||||
"""), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
|
||||
}
|
||||
}
|
||||
|
||||
@TestExtension("healthCheckFailingExtension")
|
||||
public static class FailingHealthCheck implements HealthCheck {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "failing";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue