From 272893e95b40a593e0e9f455df56987bec3996ce Mon Sep 17 00:00:00 2001 From: xstefank Date: Fri, 7 Feb 2025 15:39:01 +0100 Subject: [PATCH] Provide a way to disable health checks with configuration --- .../asciidoc/protocol-wireformat.asciidoc | 15 ++- ...va => DefaultEmptyResponseConfigTest.java} | 6 +- .../tck/DisableHealthChecksConfigTest.java | 101 ++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) rename tck/src/main/java/org/eclipse/microprofile/health/tck/{ConfigTest.java => DefaultEmptyResponseConfigTest.java} (90%) create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/DisableHealthChecksConfigTest.java diff --git a/spec/src/main/asciidoc/protocol-wireformat.asciidoc b/spec/src/main/asciidoc/protocol-wireformat.asciidoc index 68b9fd2..e25e0d6 100644 --- a/spec/src/main/asciidoc/protocol-wireformat.asciidoc +++ b/spec/src/main/asciidoc/protocol-wireformat.asciidoc @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2021 Eclipse Microprofile Contributors: +// Copyright (c) 2016-2025 Eclipse Microprofile Contributors: // See overview.adoc // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -200,6 +200,19 @@ https://github.com/eclipse/microprofile-config[MicroProfile Config] configuratio `mp.health.disable-default-procedures` to `true`. This allows the application to process and display only the user-defined health check procedures. +=== Disabling individual health checks + +If you need to disable any of the included health checks procedures, you can include the `mp.health.disabled.checks` https://github.com/eclipse/microprofile-config[MicroProfile Config] configuration property which expects an array of the fully +qualified classnames of the `HealthCheck` implementations. For instance: + +```properties +mp.health.disabled.checks=org.acme.MyHealthCheck,org.myorg.CustomHealthCheck2 +``` + +This might be particularly useful if the same application deploys in different environments +with different requirements. The configuration can disable health checks which are not +or potentially even can cause problems dynamically, without the need to recompile the whole +application to remove the health check implementation. == Security diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/ConfigTest.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/DefaultEmptyResponseConfigTest.java similarity index 90% rename from tck/src/main/java/org/eclipse/microprofile/health/tck/ConfigTest.java rename to tck/src/main/java/org/eclipse/microprofile/health/tck/DefaultEmptyResponseConfigTest.java index 8413b1d..4afd384 100644 --- a/tck/src/main/java/org/eclipse/microprofile/health/tck/ConfigTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/DefaultEmptyResponseConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Contributors to the Eclipse Foundation + * Copyright (c) 2020-2025 Contributors to the Eclipse Foundation * * See the NOTICES file(s) distributed with this work for additional * information regarding copyright ownership. @@ -37,11 +37,11 @@ /** * @author Martin Stefanko */ -public class ConfigTest extends TCKBase { +public class DefaultEmptyResponseConfigTest extends TCKBase { @Deployment public static Archive getDeployment() { - return createWarFileWithClasses(ConfigTest.class.getSimpleName()) + return createWarFileWithClasses(DefaultEmptyResponseConfigTest.class.getSimpleName()) .addAsManifestResource(new StringAsset("mp.health.default.readiness.empty.response=UP"), "microprofile-config.properties"); } diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/DisableHealthChecksConfigTest.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/DisableHealthChecksConfigTest.java new file mode 100644 index 0000000..de6a258 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/DisableHealthChecksConfigTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.eclipse.microprofile.health.tck; + +import static org.eclipse.microprofile.health.tck.DeploymentUtils.createWarFileWithClasses; + +import org.eclipse.microprofile.health.tck.deployment.SuccessfulLiveness; +import org.eclipse.microprofile.health.tck.deployment.SuccessfulReadiness; +import org.eclipse.microprofile.health.tck.deployment.SuccessfulStartup; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.OperateOnDeployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.testng.Assert; +import org.testng.annotations.Test; + +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; + +/** + * @author Martin Stefanko + */ +public class DisableHealthChecksConfigTest extends TCKBase { + + private static final String SINGLE_CLASSNAME = "single-classname"; + private static final String MULTIPLE_CLASSNAME = "multiple-classname"; + + @Deployment(name = SINGLE_CLASSNAME) + public static Archive getDeploymentSingleClassnameCheck() { + return createWarFileWithClasses(SINGLE_CLASSNAME, SuccessfulLiveness.class) + .addAsManifestResource( + new StringAsset("mp.health.disabled.checks=" + SuccessfulLiveness.class.getName()), + "microprofile-config.properties"); + } + + /** + * Verifies that disabling single health check with full class name is respected. + */ + @Test + @OperateOnDeployment(SINGLE_CLASSNAME) + @RunAsClient + public void testSingleHealthCheckDisabledWithClassname() { + assertNumberOfChecksPresent(0, "Didn't expect any checks"); + } + + @Deployment(name = MULTIPLE_CLASSNAME) + public static Archive getDeploymentMultipleClassnameCheck() { + return createWarFileWithClasses(MULTIPLE_CLASSNAME, SuccessfulLiveness.class, SuccessfulReadiness.class, + SuccessfulStartup.class) + .addAsManifestResource( + new StringAsset("mp.health.disabled.checks=" + SuccessfulLiveness.class.getName() + "," + + SuccessfulReadiness.class.getName()), + "microprofile-config.properties"); + } + + /** + * Verifies that disabling multiple health checks with full class name is respected. + */ + @Test + @OperateOnDeployment(MULTIPLE_CLASSNAME) + @RunAsClient + public void testMultipleHealthCheckDisabledWithClassname() { + assertNumberOfChecksPresent(1, "Expected only one check"); + } + + private void assertNumberOfChecksPresent(int expected, String message) { + Response response = getUrlHealthContents(); + + // status code + Assert.assertEquals(response.getStatus(), 200); + + JsonObject json = readJson(response); + + // response size + JsonArray checks = json.getJsonArray("checks"); + Assert.assertEquals(checks.size(), expected, message); + + assertOverallSuccess(json); + } +}