From abada149cde0eae8c83328a442a49c5c80e78140 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 8 Oct 2024 12:16:11 -0500 Subject: [PATCH 1/6] Add support for built-in health check config at health.checks while maintaining bw compat at helidon.health Signed-off-by: Tim Quinn --- docs/se/health.adoc | 9 ++-- .../health/checks/DiskSpaceHealthCheck.java | 43 ++++++++++----- .../helidon/health/checks/HealthChecks.java | 10 ++-- .../health/checks/HeapMemoryHealthCheck.java | 28 ++++++++-- .../io/helidon/health/checks/ConfigTest.java | 24 +++++++-- .../src/test/resources/testConfig.yaml | 16 ++++-- microprofile/health/pom.xml | 5 ++ .../microprofile/health/TestConfigPrefix.java | 43 +++++++++++++++ .../health/TestDeprecatedConfigPrefix.java | 43 +++++++++++++++ .../microprofile/health/TestUtils.java | 53 +++++++++++++++++++ 10 files changed, 239 insertions(+), 35 deletions(-) create mode 100644 microprofile/health/src/test/java/io/helidon/microprofile/health/TestConfigPrefix.java create mode 100644 microprofile/health/src/test/java/io/helidon/microprofile/health/TestDeprecatedConfigPrefix.java create mode 100644 microprofile/health/src/test/java/io/helidon/microprofile/health/TestUtils.java diff --git a/docs/se/health.adoc b/docs/se/health.adoc index 8c1b9257c8b..c1aa351eca5 100644 --- a/docs/se/health.adoc +++ b/docs/se/health.adoc @@ -23,6 +23,7 @@ :rootdir: {docdir}/.. include::{rootdir}/includes/se.adoc[] +:built-in-health-check-config-prefix: health.checks == Contents @@ -216,16 +217,14 @@ common health check statuses: |available disk space |`diskSpace` | link:{health-javadoc-base-url}/io/helidon/health/checks/DiskSpaceHealthCheck.html[`DiskSpaceHealthCheck`] -|`helidon.healthCheck.diskSpace.thresholdPercent` + -+ -`helidon.healthCheck.diskSpace.path` +|`{built-in-health-check-config-prefix}.diskSpace.thresholdPercent` + +`{built-in-health-check-config-prefix}.diskSpace.path` | `99.999` + -+ `/` |available heap memory | `heapMemory` | link:{health-javadoc-base-url}/io/helidon/health/checks/HeapMemoryHealthCheck.html[`HeapMemoryHealthCheck`] -|`helidon.healthCheck.heapMemory.thresholdPercent` +|`{built-in-health-check-config-prefix}.heapMemory.thresholdPercent` |`98` |======= diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index 7194c0521f2..5fc4ad0cee9 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -16,7 +16,6 @@ package io.helidon.health.checks; -import java.io.File; import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.Files; @@ -26,12 +25,12 @@ import java.util.Locale; import io.helidon.config.Config; +import io.helidon.config.DeprecatedConfig; import io.helidon.health.HealthCheckException; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Liveness; @@ -43,7 +42,8 @@ *

* By default, this health check has a threshold of 100%, meaning that it will never fail the threshold check. * Also, by default, it will check the root path {@code /}. These defaults can be modified using the - * {@value CONFIG_KEY_PATH} property (default {@value DEFAULT_PATH}), and the {@value CONFIG_KEY_THRESHOLD_PERCENT} + * {@value CURRENT_CONFIG_KEY_PATH} property (default {@value DEFAULT_PATH}), and the + * {@value CURRENT_CONFIG_KEY_THRESHOLD_PERCENT} * property (default {@value DEFAULT_THRESHOLD}, virtually 100). The threshold should be set to a percent, such as 50 for 50% or * 99 for 99%. If disk usage * exceeds this threshold, then the health check will fail. @@ -69,7 +69,7 @@ public class DiskSpaceHealthCheck implements HealthCheck { * directory as application path), use * {@link io.helidon.health.checks.DiskSpaceHealthCheck.Builder#path(java.nio.file.Path)}. * When running within a MicroProfile server, you can configure path using a configuration key - * {@value #CONFIG_KEY_PATH} + * {@value #CURRENT_CONFIG_KEY_PATH} * Defaults to {@value} */ public static final String DEFAULT_PATH = "."; @@ -87,14 +87,25 @@ public class DiskSpaceHealthCheck implements HealthCheck { /** * Full configuration key for path, when configured through MicroProfile config. */ - public static final String CONFIG_KEY_PATH = HealthChecks.CONFIG_KEY_HEALTH_PREFIX + public static final String CONFIG_KEY_PATH = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_PATH_SUFFIX; /** * Full configuration key for threshold percent, when configured through Microprofile config. */ - public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.CONFIG_KEY_HEALTH_PREFIX + public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + + "." + CONFIG_KEY_DISKSPACE_PREFIX + + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; + + // The following two constants are used in the Javadoc to nudge users toward using the config key prefix "health.checks" + // rather than the obsolete "helidon.health". Because the original public constants above have always referred to the + // now-deprecated config prefixes, those values are unchanged to preserve backward compatibility. + private static final String CURRENT_CONFIG_KEY_PATH = HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + + "." + CONFIG_KEY_DISKSPACE_PREFIX + + "." + CONFIG_KEY_PATH_SUFFIX; + + private static final String CURRENT_CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; @@ -114,16 +125,22 @@ public class DiskSpaceHealthCheck implements HealthCheck { } @Inject - DiskSpaceHealthCheck( - @ConfigProperty(name = CONFIG_KEY_PATH, defaultValue = DEFAULT_PATH) File path, - @ConfigProperty(name = CONFIG_KEY_THRESHOLD_PERCENT, defaultValue = "99.999") double thresholdPercent - ) { + DiskSpaceHealthCheck(Config rootConfig) { + Config diskSpaceConfig = DeprecatedConfig.get(rootConfig, + HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) + .get(CONFIG_KEY_DISKSPACE_PREFIX); + Path path = diskSpaceConfig.get(CONFIG_KEY_PATH_SUFFIX) + .as(Path.class) + .orElse(Paths.get(".")); + thresholdPercent = diskSpaceConfig.get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) + .asDouble() + .orElse(99.999d); try { - this.fileStore = Files.getFileStore(path.toPath()); + this.fileStore = Files.getFileStore(path); } catch (IOException e) { - throw new HealthCheckException("Failed to obtain file store for path " + path.getAbsolutePath(), e); + throw new HealthCheckException("Failed to obtain file store for path " + path, e); } - this.thresholdPercent = thresholdPercent; } private DiskSpaceHealthCheck(Builder builder) { diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HealthChecks.java b/health/health-checks/src/main/java/io/helidon/health/checks/HealthChecks.java index 87493c2fc14..d9e99bb9684 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HealthChecks.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HealthChecks.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021 Oracle and/or its affiliates. + * Copyright (c) 2018, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,8 @@ */ public final class HealthChecks { - static final String CONFIG_KEY_HEALTH_PREFIX = "helidon.health"; + static final String CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX = "health.checks"; + static final String DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX = "helidon.health"; private HealthChecks() { } @@ -114,9 +115,10 @@ public static HealthCheck[] healthChecks() { } /** - * Built-in health checks, set up using "helidon.health" configuration. + * Built-in health checks, set up using configuration at {@value CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX} or the deprecated + * {@value DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX}. * - * @param config configuration rooted at "helidon.health" + * @param config configuration at the node containing health checks config * @return built-in health checks, set up using the provided configuration * @see io.helidon.health.HealthSupport.Builder#addLiveness(org.eclipse.microprofile.health.HealthCheck...) */ diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index dedecaafa60..a9c155f6e3f 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -20,11 +20,11 @@ import java.util.Locale; import io.helidon.config.Config; +import io.helidon.config.DeprecatedConfig; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Liveness; @@ -36,7 +36,7 @@ * By default, this health check has a threshold of {@value DEFAULT_THRESHOLD} ({@value DEFAULT_THRESHOLD}%). * If heap usage exceeds this level, then the server * is considered to be unhealthy. This default can be modified using the - * {@value CONFIG_KEY_THRESHOLD_PERCENT} property. The threshold should be set as a percent, such as + * {@value CURRENT_CONFIG_KEY_THRESHOLD_PERCENT} property. The threshold should be set as a percent, such as * 50 for 50% or 99 for 99%. *

*

@@ -63,10 +63,18 @@ public class HeapMemoryHealthCheck implements HealthCheck { /** * Config property key for heap memory threshold. */ - public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.CONFIG_KEY_HEALTH_PREFIX + public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_HEAP_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; + // The following constant is used in the Javadoc to nudge users toward using the current config key prefix "health.checks" + // rather than the deprecated "helidon.health". The public constant above has always used the now-deprecated prefix so that + // value is unchanged to preserve backward compatibility. + private static final String CURRENT_CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + + "." + CONFIG_KEY_HEAP_PREFIX + + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; + + private final Runtime rt; private final double thresholdPercent; @@ -74,9 +82,19 @@ public class HeapMemoryHealthCheck implements HealthCheck { @Inject HeapMemoryHealthCheck( Runtime runtime, - @ConfigProperty(name = CONFIG_KEY_THRESHOLD_PERCENT, defaultValue = "98") double threshold) { - this.thresholdPercent = threshold; + Config rootConfig) { + this(runtime, DeprecatedConfig.get(rootConfig, + HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) + .get(CONFIG_KEY_HEAP_PREFIX) + .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) + .asDouble() + .orElse(98d)); + } + + HeapMemoryHealthCheck(Runtime runtime, double thresholdPercent) { this.rt = runtime; + this.thresholdPercent = thresholdPercent; } private HeapMemoryHealthCheck(Builder builder) { diff --git a/health/health-checks/src/test/java/io/helidon/health/checks/ConfigTest.java b/health/health-checks/src/test/java/io/helidon/health/checks/ConfigTest.java index ff7d1054ced..39015adb15c 100644 --- a/health/health-checks/src/test/java/io/helidon/health/checks/ConfigTest.java +++ b/health/health-checks/src/test/java/io/helidon/health/checks/ConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import io.helidon.common.http.MediaType; import io.helidon.config.Config; import io.helidon.config.ConfigSources; +import io.helidon.config.DeprecatedConfig; import io.helidon.health.HealthSupport; import io.helidon.media.jsonp.JsonpSupport; import io.helidon.webclient.WebClient; @@ -69,7 +70,7 @@ static void shutdownServer(WebServer server) { @Test void bothFail() throws InterruptedException, ExecutionException, TimeoutException { - JsonObject health = runWithConfig("bothFail", Http.Status.SERVICE_UNAVAILABLE_503.code()); + JsonObject health = runWithConfig("bothFailWithDeprecatedPrefix.helidon.health", Http.Status.SERVICE_UNAVAILABLE_503.code()); JsonObject diskSpace = getLivenessCheck(health, "diskSpace"); assertThat("Disk space liveness return data", diskSpace, is(notNullValue())); @@ -83,7 +84,7 @@ void bothFail() throws InterruptedException, ExecutionException, TimeoutExceptio @Test void bothPass() throws InterruptedException, ExecutionException, TimeoutException { - JsonObject health = runWithConfig("bothPass", Http.Status.OK_200.code()); + JsonObject health = runWithConfig("bothPassWithDeprecatedPrefix.helidon.health", Http.Status.OK_200.code()); JsonObject diskSpace = getLivenessCheck(health, "diskSpace"); assertThat("Disk space liveness return data", diskSpace, is(notNullValue())); @@ -95,10 +96,25 @@ void bothPass() throws InterruptedException, ExecutionException, TimeoutExceptio assertThat("Heap memory liveness check status", heapMemory.getString("status"), is("UP")); } + @Test + void testWithoutHelidonPrefix() throws ExecutionException, InterruptedException, TimeoutException { + JsonObject health = runWithConfig("bothFailWithoutDeprecatedHelidonPrefix.health.checks", + Http.Status.SERVICE_UNAVAILABLE_503.code()); + JsonObject diskSpace = getLivenessCheck(health, "diskSpace"); + + assertThat("Disk space liveness return data", diskSpace, is(notNullValue())); + assertThat("Disk space liveness check status", diskSpace.getString("status"), is("DOWN")); + + JsonObject heapMemory = getLivenessCheck(health, "heapMemory"); + + assertThat("Heap memory liveness return data", heapMemory, is(notNullValue())); + assertThat("Heap memory liveness check status", heapMemory.getString("status"), is("DOWN")); + } + private JsonObject runWithConfig(String configKey, int expectedStatus) throws InterruptedException, ExecutionException, TimeoutException { HealthSupport healthSupport = HealthSupport.builder() - .addLiveness(HealthChecks.healthChecks(testConfig.get(configKey + ".helidon.health"))) + .addLiveness(HealthChecks.healthChecks(testConfig.get(configKey))) .build(); WebServer webServer = null; try { diff --git a/health/health-checks/src/test/resources/testConfig.yaml b/health/health-checks/src/test/resources/testConfig.yaml index b32513a91d4..46847147070 100644 --- a/health/health-checks/src/test/resources/testConfig.yaml +++ b/health/health-checks/src/test/resources/testConfig.yaml @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Oracle and/or its affiliates. +# Copyright (c) 2021, 2024 Oracle and/or its affiliates. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,17 +13,25 @@ # See the License for the specific language governing permissions and # limitations under the License. # -bothFail: +bothFailWithDeprecatedPrefix: helidon: health: diskSpace: thresholdPercent: 0.0 heapMemory: thresholdPercent: 0.0 -bothPass: +bothPassWithDeprecatedPrefix: helidon: health: diskSpace: thresholdPercent: 98.1 heapMemory: - thresholdPercent: 98.1 \ No newline at end of file + thresholdPercent: 98.1 +# Following are set to zero to avoid the defaults and force a DOWN state the test can easily detect. +bothFailWithoutDeprecatedHelidonPrefix: + health: + checks: + diskSpace: + thresholdPercent: 0.0 + heapMemory: + thresholdPercent: 0.0 \ No newline at end of file diff --git a/microprofile/health/pom.xml b/microprofile/health/pom.xml index 198e85c53ea..e3355347e1b 100644 --- a/microprofile/health/pom.xml +++ b/microprofile/health/pom.xml @@ -60,6 +60,11 @@ io.helidon.service-common helidon-service-common-rest-cdi + + io.helidon.health + helidon-health-checks + test + org.junit.jupiter junit-jupiter-api diff --git a/microprofile/health/src/test/java/io/helidon/microprofile/health/TestConfigPrefix.java b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestConfigPrefix.java new file mode 100644 index 00000000000..5b548729f76 --- /dev/null +++ b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestConfigPrefix.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * 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. + */ +package io.helidon.microprofile.health; + +import io.helidon.microprofile.tests.junit5.AddConfig; +import io.helidon.microprofile.tests.junit5.HelidonTest; + +import jakarta.inject.Inject; +import jakarta.ws.rs.client.WebTarget; +import org.junit.jupiter.api.Test; + +/** + * Tests the config prefix. + *

+ * We specify 0 values to force the statuses to DOWN. The default config settings cause the checks to return UP, so if we + * detect DOWN statuses we know the config has been found and applied correctly. + */ +@HelidonTest +@AddConfig(key = "health.checks.diskSpace.thresholdPercent", value = "0.0") +@AddConfig(key = "health.checks.heapMemory.thresholdPercent", value = "0.0") +class TestConfigPrefix { + + @Inject + private WebTarget webTarget; + + @Test + void testConfig() { + TestUtils.checkForFailure(webTarget); + } +} diff --git a/microprofile/health/src/test/java/io/helidon/microprofile/health/TestDeprecatedConfigPrefix.java b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestDeprecatedConfigPrefix.java new file mode 100644 index 00000000000..9ca5d09a0d3 --- /dev/null +++ b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestDeprecatedConfigPrefix.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * 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. + */ +package io.helidon.microprofile.health; + +import io.helidon.microprofile.tests.junit5.AddConfig; +import io.helidon.microprofile.tests.junit5.HelidonTest; + +import jakarta.inject.Inject; +import jakarta.ws.rs.client.WebTarget; +import org.junit.jupiter.api.Test; + +/** + * Tests the deprecated config prefix. + *

+ * We specify 0 values to force the statuses to DOWN. The default config settings cause the checks to return UP, so if we + * detect DOWN statuses we know the config has been found and applied correctly. + */ +@HelidonTest +@AddConfig(key = "helidon.health.diskSpace.thresholdPercent", value = "0.0") +@AddConfig(key = "helidon.health.heapMemory.thresholdPercent", value = "0.0") +class TestDeprecatedConfigPrefix { + + @Inject + private WebTarget webTarget; + + @Test + void testConfig() { + TestUtils.checkForFailure(webTarget); + } +} diff --git a/microprofile/health/src/test/java/io/helidon/microprofile/health/TestUtils.java b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestUtils.java new file mode 100644 index 00000000000..f46976a335c --- /dev/null +++ b/microprofile/health/src/test/java/io/helidon/microprofile/health/TestUtils.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * 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. + */ +package io.helidon.microprofile.health; + +import io.helidon.common.http.Http; + +import jakarta.json.JsonObject; +import jakarta.json.JsonValue; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +class TestUtils { + static JsonObject getLivenessCheck(JsonObject health, String checkName) { + return health.getJsonArray("checks").stream() + .map(JsonValue::asJsonObject) + .filter(jo -> jo.getString("name").equals(checkName)) + .findFirst() + .orElse(null); + } + + static void checkForFailure(WebTarget webTarget) { + Response response = webTarget.path("/health") + .request() + .get(); + + assertThat("Health endpoint status", response.getStatus(), is(Http.Status.SERVICE_UNAVAILABLE_503.code())); + + JsonObject healthJson = response.readEntity(JsonObject.class); + JsonObject diskSpace = TestUtils.getLivenessCheck(healthJson, "diskSpace"); + JsonObject heapMemory = TestUtils.getLivenessCheck(healthJson, "heapMemory"); + assertThat("Disk space liveness return data", diskSpace, is(notNullValue())); + assertThat("Disk space liveness check status", diskSpace.getString("status"), is("DOWN")); + assertThat("Heap memory liveness return data", heapMemory, is(notNullValue())); + assertThat("Heap memory liveness check status", heapMemory.getString("status"), is("DOWN")); + } +} From 57777ec47b7193f064dff0c9a291551b53bd8541 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 8 Oct 2024 12:25:21 -0500 Subject: [PATCH 2/6] Add deprecated annotations to some constants to warn users of an eventual change in value --- .../java/io/helidon/health/checks/DiskSpaceHealthCheck.java | 6 ++++++ .../io/helidon/health/checks/HeapMemoryHealthCheck.java | 3 +++ 2 files changed, 9 insertions(+) diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index 5fc4ad0cee9..f5d771e2327 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -86,14 +86,20 @@ public class DiskSpaceHealthCheck implements HealthCheck { /** * Full configuration key for path, when configured through MicroProfile config. + * + * @deprecated The value will change to {@value CURRENT_CONFIG_KEY_PATH} in a future release */ + @Deprecated(since = "3.2.11") public static final String CONFIG_KEY_PATH = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_PATH_SUFFIX; /** * Full configuration key for threshold percent, when configured through Microprofile config. + * + * @deprecated The value will change to {@value CURRENT_CONFIG_KEY_THRESHOLD_PERCENT} in future release */ + @Deprecated(since = "3.2.11") public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index a9c155f6e3f..0968bb1f2e3 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -62,7 +62,10 @@ public class HeapMemoryHealthCheck implements HealthCheck { /** * Config property key for heap memory threshold. + * + * @deprecated The value will change to {@value #CURRENT_CONFIG_KEY_THRESHOLD_PERCENT} in a future release */ + @Deprecated(since = "3.2.11") public static final String CONFIG_KEY_THRESHOLD_PERCENT = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_HEAP_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; From 0c7acee372e59f0b442eeb251e3f296c2a07be6e Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 8 Oct 2024 16:39:08 -0500 Subject: [PATCH 3/6] Avoid injecting Config --- .../io/helidon/health/checks/DiskSpaceHealthCheck.java | 9 +++++++-- .../helidon/health/checks/HeapMemoryHealthCheck.java | 10 +++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index f5d771e2327..26f5e65b274 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -30,6 +30,7 @@ import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; @@ -131,8 +132,8 @@ public class DiskSpaceHealthCheck implements HealthCheck { } @Inject - DiskSpaceHealthCheck(Config rootConfig) { - Config diskSpaceConfig = DeprecatedConfig.get(rootConfig, + DiskSpaceHealthCheck() { + Config diskSpaceConfig = DeprecatedConfig.get(config(), HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) .get(CONFIG_KEY_DISKSPACE_PREFIX); @@ -314,4 +315,8 @@ public Builder config(Config config) { return this; } } + + private static Config config() { + return CDI.current().select(Config.class).get(); + } } diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index 0968bb1f2e3..1bf651808c9 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -24,6 +24,7 @@ import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; @@ -84,9 +85,8 @@ public class HeapMemoryHealthCheck implements HealthCheck { // this will be ignored if not within CDI @Inject HeapMemoryHealthCheck( - Runtime runtime, - Config rootConfig) { - this(runtime, DeprecatedConfig.get(rootConfig, + Runtime runtime) { + this(runtime, DeprecatedConfig.get(config(), HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) .get(CONFIG_KEY_HEAP_PREFIX) @@ -100,6 +100,10 @@ public class HeapMemoryHealthCheck implements HealthCheck { this.thresholdPercent = thresholdPercent; } + private static Config config() { + return CDI.current().select(Config.class).get(); + } + private HeapMemoryHealthCheck(Builder builder) { this.thresholdPercent = builder.threshold; this.rt = Runtime.getRuntime(); From 6ba516f582e7a16b7f43d078a13a7fac092f1bd9 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Mon, 4 Nov 2024 16:40:21 -0600 Subject: [PATCH 4/6] Redo config handling --- health/health-checks/pom.xml | 4 ++ .../health/checks/DiskSpaceHealthCheck.java | 51 +++++++++++-------- .../health/checks/HeapMemoryHealthCheck.java | 13 ++--- .../src/main/java/module-info.java | 3 +- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/health/health-checks/pom.xml b/health/health-checks/pom.xml index 688d552ec43..4de73a3bcf4 100644 --- a/health/health-checks/pom.xml +++ b/health/health-checks/pom.xml @@ -49,6 +49,10 @@ io.helidon.config helidon-config + + io.helidon.config + helidon-config-mp + jakarta.enterprise diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index 26f5e65b274..478d276484f 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -26,11 +26,11 @@ import io.helidon.config.Config; import io.helidon.config.DeprecatedConfig; +import io.helidon.config.mp.MpConfig; import io.helidon.health.HealthCheckException; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; @@ -132,22 +132,10 @@ public class DiskSpaceHealthCheck implements HealthCheck { } @Inject - DiskSpaceHealthCheck() { - Config diskSpaceConfig = DeprecatedConfig.get(config(), - HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, - HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) - .get(CONFIG_KEY_DISKSPACE_PREFIX); - Path path = diskSpaceConfig.get(CONFIG_KEY_PATH_SUFFIX) - .as(Path.class) - .orElse(Paths.get(".")); - thresholdPercent = diskSpaceConfig.get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) - .asDouble() - .orElse(99.999d); - try { - this.fileStore = Files.getFileStore(path); - } catch (IOException e) { - throw new HealthCheckException("Failed to obtain file store for path " + path, e); - } + DiskSpaceHealthCheck(org.eclipse.microprofile.config.Config mpConfig) { + this(builder().config(DeprecatedConfig.get(MpConfig.toHelidonConfig(mpConfig), + HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX))); } private DiskSpaceHealthCheck(Builder builder) { @@ -198,6 +186,31 @@ public static DiskSpaceHealthCheck create() { return builder().build(); } +// private static FileStore path(Config config) { +// try { +// return Files.getFileStore( +// Path.of(DeprecatedConfig.get(MpConfig.toHelidonConfig(config), +// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, +// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) +// .get(CONFIG_KEY_DISKSPACE_PREFIX) +// .get(CONFIG_KEY_PATH_SUFFIX) +// .asString() +// .orElse(DEFAULT_PATH))); +// } catch (IOException ex) { +// throw new RuntimeException(ex); +// } +// } +// +// private static double thresholdPercent(Config config) { +// return DeprecatedConfig.get(MpConfig.toHelidonConfig(config), +// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, +// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) +// .get(CONFIG_KEY_DISKSPACE_PREFIX) +// .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) +// .asDouble() +// .orElse(DEFAULT_THRESHOLD); +// } + @Override public HealthCheckResponse call() { long diskFreeInBytes; @@ -315,8 +328,4 @@ public Builder config(Config config) { return this; } } - - private static Config config() { - return CDI.current().select(Config.class).get(); - } } diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index 1bf651808c9..a589deb4e92 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -19,13 +19,13 @@ import java.util.Formatter; import java.util.Locale; -import io.helidon.config.Config; import io.helidon.config.DeprecatedConfig; +import io.helidon.config.mp.MpConfig; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.spi.CDI; import jakarta.inject.Inject; +import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Liveness; @@ -85,8 +85,9 @@ public class HeapMemoryHealthCheck implements HealthCheck { // this will be ignored if not within CDI @Inject HeapMemoryHealthCheck( + Config config, Runtime runtime) { - this(runtime, DeprecatedConfig.get(config(), + this(runtime, DeprecatedConfig.get(MpConfig.toHelidonConfig(config), HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) .get(CONFIG_KEY_HEAP_PREFIX) @@ -100,10 +101,6 @@ public class HeapMemoryHealthCheck implements HealthCheck { this.thresholdPercent = thresholdPercent; } - private static Config config() { - return CDI.current().select(Config.class).get(); - } - private HeapMemoryHealthCheck(Builder builder) { this.thresholdPercent = builder.threshold; this.rt = Runtime.getRuntime(); @@ -199,7 +196,7 @@ public Builder thresholdPercent(double threshold) { * @param config {@code Config} node for heap memory * @return updated builder instance */ - public Builder config(Config config) { + public Builder config(io.helidon.config.Config config) { config.get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) .asDouble() .ifPresent(this::thresholdPercent); diff --git a/health/health-checks/src/main/java/module-info.java b/health/health-checks/src/main/java/module-info.java index bc7a53253fc..575bc592b33 100644 --- a/health/health-checks/src/main/java/module-info.java +++ b/health/health-checks/src/main/java/module-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021 Oracle and/or its affiliates. + * Copyright (c) 2018, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ requires io.helidon.common; requires io.helidon.config; + requires io.helidon.config.mp; requires io.helidon.health; requires io.helidon.health.common; requires static microprofile.config.api; From 286d4bbaea5fd0adaeb5c0b21e3d908ac1ec498f Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 5 Nov 2024 15:58:55 -0600 Subject: [PATCH 5/6] Rework deprecated config look-up to avoid issue with SE DeprecatedConfig with proxied config objects --- .../helidon/config/mp/DeprecatedMpConfig.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java new file mode 100644 index 00000000000..08b4b0e0793 --- /dev/null +++ b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * 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. + */ +package io.helidon.config.mp;public class DeprecatedMpConfig { +} From 512b8883c44bb77354f36fdbc7932eb710f006e9 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 5 Nov 2024 16:08:14 -0600 Subject: [PATCH 6/6] Proper commit of recent changes --- .../helidon/config/mp/DeprecatedMpConfig.java | 59 ++++++++++++++++++- .../health/checks/DiskSpaceHealthCheck.java | 58 ++++++++---------- .../health/checks/HeapMemoryHealthCheck.java | 28 ++++----- 3 files changed, 98 insertions(+), 47 deletions(-) diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java index 08b4b0e0793..f20978cb7df 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java @@ -13,5 +13,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.config.mp;public class DeprecatedMpConfig { +package io.helidon.config.mp; + +import java.util.Optional; +import java.util.logging.Logger; + +import org.eclipse.microprofile.config.Config; + +/** + * A utility class to handle MicroProfile configuration properties that should no longer be used. + *

+ * For one major release, the property is retrieved through this class, to warn about the usage. + * In next major release, the deprecated property is removed (as is use of this class). + *

+ * This class is closely patterned after {@code io.helidon.config.DeprecatedConfig} and works whether the MP config object + * is proxied by CDI or not. That other class can be used in conjunction with + * {@link io.helidon.config.mp.MpConfig#toHelidonConfig(org.eclipse.microprofile.config.Config)} for MP + * config objects that are not injected but that does work for MP config objects proxied by CDI. This one does work for + * those use cases. + */ +public final class DeprecatedMpConfig { + private static final Logger LOGGER = Logger.getLogger(DeprecatedMpConfig.class.getName()); + + private DeprecatedMpConfig() { + } + + /** + * Get a value from config, attempting to read both the keys. + * Warning is logged if either the current key is not defined, or both the keys are defined. + * + * @param config configuration instance + * @param type type of the retrieved value + * @param currentKey key that should be used + * @param deprecatedKey key that should not be used + * @param type of the retrieved value + * @return config value of the current key if exists, or the deprecated key if it does not, an empty {@code Optional} + * otherwise + */ + public static Optional getConfigValue(Config config, Class type, String currentKey, String deprecatedKey) { + Optional deprecatedConfig = config.getOptionalValue(deprecatedKey, type); + Optional currentConfig = config.getOptionalValue(currentKey, type); + + if (deprecatedConfig.isPresent()) { + if (currentConfig.isPresent()) { + LOGGER.warning("You are using both a deprecated configuration and a current one. " + + "Deprecated key: \"" + deprecatedKey + "\", " + + "current key: \"" + currentKey + "\", " + + "only the current key will be used, and deprecated will be ignored."); + return currentConfig; + } else { + LOGGER.warning("You are using a deprecated configuration key. " + + "Deprecated key: \"" + deprecatedKey + "\", " + + "current key: \"" + currentKey + "\"."); + return deprecatedConfig; + } + } else { + return currentConfig; + } + } } diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index 478d276484f..7fecf2a9c50 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -23,10 +23,10 @@ import java.nio.file.Paths; import java.util.Formatter; import java.util.Locale; +import java.util.function.Consumer; import io.helidon.config.Config; -import io.helidon.config.DeprecatedConfig; -import io.helidon.config.mp.MpConfig; +import io.helidon.config.mp.DeprecatedMpConfig; import io.helidon.health.HealthCheckException; import io.helidon.health.common.BuiltInHealthCheck; @@ -83,8 +83,6 @@ public class DiskSpaceHealthCheck implements HealthCheck { static final String CONFIG_KEY_DISKSPACE_PREFIX = "diskSpace"; static final String CONFIG_KEY_PATH_SUFFIX = "path"; - static final String CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX = "thresholdPercent"; - /** * Full configuration key for path, when configured through MicroProfile config. * @@ -94,7 +92,7 @@ public class DiskSpaceHealthCheck implements HealthCheck { public static final String CONFIG_KEY_PATH = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_PATH_SUFFIX; - + static final String CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX = "thresholdPercent"; /** * Full configuration key for threshold percent, when configured through Microprofile config. * @@ -133,9 +131,7 @@ public class DiskSpaceHealthCheck implements HealthCheck { @Inject DiskSpaceHealthCheck(org.eclipse.microprofile.config.Config mpConfig) { - this(builder().config(DeprecatedConfig.get(MpConfig.toHelidonConfig(mpConfig), - HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, - HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX))); + this(builder().update(applyConfig(mpConfig))); } private DiskSpaceHealthCheck(Builder builder) { @@ -186,30 +182,28 @@ public static DiskSpaceHealthCheck create() { return builder().build(); } -// private static FileStore path(Config config) { -// try { -// return Files.getFileStore( -// Path.of(DeprecatedConfig.get(MpConfig.toHelidonConfig(config), -// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, -// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) -// .get(CONFIG_KEY_DISKSPACE_PREFIX) -// .get(CONFIG_KEY_PATH_SUFFIX) -// .asString() -// .orElse(DEFAULT_PATH))); -// } catch (IOException ex) { -// throw new RuntimeException(ex); -// } -// } -// -// private static double thresholdPercent(Config config) { -// return DeprecatedConfig.get(MpConfig.toHelidonConfig(config), -// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, -// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) -// .get(CONFIG_KEY_DISKSPACE_PREFIX) -// .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) -// .asDouble() -// .orElse(DEFAULT_THRESHOLD); -// } + private static Consumer applyConfig(org.eclipse.microprofile.config.Config mpConfig) { + return builder -> { + DeprecatedMpConfig.getConfigValue(mpConfig, + Path.class, + configKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_PATH_SUFFIX), + configKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_PATH_SUFFIX)) + .ifPresent(builder::path); + DeprecatedMpConfig.getConfigValue(mpConfig, + Double.class, + configKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX), + configKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX)) + .ifPresent(builder::thresholdPercent); + }; + } + + private static String configKey(String prefix, String suffix) { + return prefix + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + suffix; + } @Override public HealthCheckResponse call() { diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index a589deb4e92..603dc4295a7 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -19,13 +19,11 @@ import java.util.Formatter; import java.util.Locale; -import io.helidon.config.DeprecatedConfig; -import io.helidon.config.mp.MpConfig; +import io.helidon.config.mp.DeprecatedMpConfig; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Liveness; @@ -78,22 +76,18 @@ public class HeapMemoryHealthCheck implements HealthCheck { + "." + CONFIG_KEY_HEAP_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; - private final Runtime rt; private final double thresholdPercent; // this will be ignored if not within CDI @Inject - HeapMemoryHealthCheck( - Config config, - Runtime runtime) { - this(runtime, DeprecatedConfig.get(MpConfig.toHelidonConfig(config), - HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, - HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) - .get(CONFIG_KEY_HEAP_PREFIX) - .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) - .asDouble() - .orElse(98d)); + HeapMemoryHealthCheck(Runtime runtime, org.eclipse.microprofile.config.Config mpConfig) { + // Cannot use + this(runtime, DeprecatedMpConfig.getConfigValue(mpConfig, + Double.class, + thresholdPercentKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX), + thresholdPercentKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX)) + .orElse(DEFAULT_THRESHOLD)); } HeapMemoryHealthCheck(Runtime runtime, double thresholdPercent) { @@ -126,6 +120,12 @@ public static HeapMemoryHealthCheck create() { return builder().build(); } + private static String thresholdPercentKey(String prefix) { + return prefix + "." + + CONFIG_KEY_HEAP_PREFIX + "." + + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; + } + @Override public HealthCheckResponse call() { //Formatter ensures that returned delimiter will be always the same