This is an automated email from the ASF dual-hosted git repository.
jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new f86cc72 CAMEL-17787: Take exposure level configuration into
consideration in camel-microprofile-health
f86cc72 is described below
commit f86cc7207d024ecbfe699b9139d675ddc6b5515a
Author: James Netherton <[email protected]>
AuthorDate: Thu Mar 17 15:26:34 2022 +0000
CAMEL-17787: Take exposure level configuration into consideration in
camel-microprofile-health
---
.../health/CamelMicroProfileHealthCheck.java | 10 +-
.../CamelMicroProfileHealthCheckRegistry.java | 56 ++--
.../health/CamelMicroProfileHealthHelper.java | 51 ++--
.../CamelMicroProfileRepositoryHealthCheck.java | 9 +-
...CamelMicroProfileHealthCheckRepositoryTest.java | 322 ++++++++++++++++++++-
.../health/CamelMicroProfileHealthCheckTest.java | 127 ++++++++
.../CamelMicroProfileHealthConsumerTest.java | 7 +-
7 files changed, 526 insertions(+), 56 deletions(-)
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheck.java
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheck.java
index efa22c9..5d8469c 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheck.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheck.java
@@ -18,6 +18,8 @@ package org.apache.camel.microprofile.health;
import java.util.Map;
+import org.apache.camel.CamelContext;
+import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
@@ -31,9 +33,11 @@ import static org.apache.camel.health.HealthCheck.*;
*/
final class CamelMicroProfileHealthCheck implements HealthCheck {
+ private final CamelContext camelContext;
private final org.apache.camel.health.HealthCheck camelHealthCheck;
- CamelMicroProfileHealthCheck(org.apache.camel.health.HealthCheck
camelHealthCheck) {
+ CamelMicroProfileHealthCheck(CamelContext camelContext,
org.apache.camel.health.HealthCheck camelHealthCheck) {
+ this.camelContext = camelContext;
this.camelHealthCheck = camelHealthCheck;
}
@@ -52,7 +56,9 @@ final class CamelMicroProfileHealthCheck implements
HealthCheck {
}
if (enabled) {
- CamelMicroProfileHealthHelper.applyHealthDetail(builder, result);
+ HealthCheckRegistry registry =
HealthCheckRegistry.get(camelContext);
+
+ CamelMicroProfileHealthHelper.applyHealthDetail(builder, result,
registry.getExposureLevel());
if (result.getState() == State.DOWN) {
builder.down();
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRegistry.java
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRegistry.java
index a7fcbdd..84e8882 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRegistry.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRegistry.java
@@ -27,9 +27,8 @@ import org.apache.camel.StartupListener;
import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.health.HealthCheckRepository;
-import org.apache.camel.impl.health.ConsumersHealthCheckRepository;
import org.apache.camel.impl.health.DefaultHealthCheckRegistry;
-import org.apache.camel.impl.health.RoutesHealthCheckRepository;
+import org.apache.camel.impl.health.HealthCheckRegistryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -39,8 +38,6 @@ import org.slf4j.LoggerFactory;
*/
public class CamelMicroProfileHealthCheckRegistry extends
DefaultHealthCheckRegistry implements StartupListener {
- public static final String CONSUMERS_CHECK_NAME = "camel-consumers";
- public static final String ROUTES_CHECK_NAME = "camel-routes";
private static final Logger LOG =
LoggerFactory.getLogger(CamelMicroProfileHealthCheckRegistry.class);
private final Set<HealthCheckRepository> repositories = new
CopyOnWriteArraySet<>();
@@ -87,9 +84,18 @@ public class CamelMicroProfileHealthCheckRegistry extends
DefaultHealthCheckRegi
removeMicroProfileHealthCheck(check);
} else {
HealthCheckRepository repository = (HealthCheckRepository) obj;
- if (repository instanceof ConsumersHealthCheckRepository ||
repository instanceof RoutesHealthCheckRepository) {
+ boolean isAllChecksLiveness =
repository.stream().allMatch(HealthCheck::isLiveness);
+ boolean isAllChecksReadiness =
repository.stream().allMatch(HealthCheck::isReadiness);
+
+ if (!(repository instanceof HealthCheckRegistryRepository) &&
(isAllChecksLiveness || isAllChecksReadiness)) {
try {
- getReadinessRegistry().remove(repository.getId());
+ if (isAllChecksLiveness) {
+ getLivenessRegistry().remove(repository.getId());
+ }
+
+ if (isAllChecksReadiness) {
+ getReadinessRegistry().remove(repository.getId());
+ }
} catch (IllegalStateException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to remove repository readiness
health {} check due to: {}", repository.getId(),
@@ -121,28 +127,40 @@ public class CamelMicroProfileHealthCheckRegistry extends
DefaultHealthCheckRegi
protected void registerRepositoryChecks(HealthCheckRepository repository) {
if (repository.isEnabled()) {
- // Since the number of potential checks for consumers / routes is
non-deterministic
- // avoid registering each one with SmallRye health and instead
aggregate the results so
- // that we avoid highly verbose health output
- if (repository instanceof ConsumersHealthCheckRepository) {
- CamelMicroProfileRepositoryHealthCheck repositoryHealthCheck
- = new
CamelMicroProfileRepositoryHealthCheck(repository, CONSUMERS_CHECK_NAME);
- getReadinessRegistry().register(repository.getId(),
repositoryHealthCheck);
- } else if (repository instanceof RoutesHealthCheckRepository) {
- CamelMicroProfileRepositoryHealthCheck repositoryHealthCheck
- = new
CamelMicroProfileRepositoryHealthCheck(repository, ROUTES_CHECK_NAME);
- getReadinessRegistry().register(repository.getId(),
repositoryHealthCheck);
- } else {
+ boolean isAllChecksLiveness =
repository.stream().allMatch(HealthCheck::isLiveness);
+ boolean isAllChecksReadiness =
repository.stream().allMatch(HealthCheck::isReadiness);
+
+ if (repository instanceof HealthCheckRegistryRepository ||
!isAllChecksLiveness && !isAllChecksReadiness) {
+ // Register each check individually for
HealthCheckRegistryRepository or where the repository contains
+ // a mix or readiness and liveness checks
repository.stream()
.filter(healthCheck -> healthCheck.isEnabled())
.forEach(this::registerMicroProfileHealthCheck);
+ } else {
+ // Since the number of potential checks for consumers / routes
etc is non-deterministic
+ // avoid registering each one with SmallRye health and instead
aggregate the results so
+ // that we avoid highly verbose health output
+ String healthCheckName = repository.getId();
+ if
(repository.getClass().getName().startsWith("org.apache.camel") &&
!healthCheckName.startsWith("camel-")) {
+ healthCheckName = "camel-" + healthCheckName;
+ }
+
+ CamelMicroProfileRepositoryHealthCheck repositoryHealthCheck
+ = new
CamelMicroProfileRepositoryHealthCheck(getCamelContext(), repository,
healthCheckName);
+ if (isAllChecksLiveness) {
+ getLivenessRegistry().register(repository.getId(),
repositoryHealthCheck);
+ }
+
+ if (isAllChecksReadiness) {
+ getReadinessRegistry().register(repository.getId(),
repositoryHealthCheck);
+ }
}
}
}
protected void registerMicroProfileHealthCheck(HealthCheck
camelHealthCheck) {
org.eclipse.microprofile.health.HealthCheck microProfileHealthCheck
- = new CamelMicroProfileHealthCheck(camelHealthCheck);
+ = new CamelMicroProfileHealthCheck(getCamelContext(),
camelHealthCheck);
if (camelHealthCheck.isReadiness()) {
getReadinessRegistry().register(camelHealthCheck.getId(),
microProfileHealthCheck);
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthHelper.java
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthHelper.java
index 4571c52..819cbce 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthHelper.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthHelper.java
@@ -36,28 +36,35 @@ final class CamelMicroProfileHealthHelper {
/**
* Propagates details from the Camel Health {@link Result} to the
MicroProfile {@link HealthCheckResponseBuilder}.
*
- * @param builder The health check response builder
- * @param result The Camel health check result
+ * @param builder The health check response builder
+ * @param result The Camel health check result
+ * @param exposureLevel The level at which to expose details from the
health check result
*/
- public static void applyHealthDetail(HealthCheckResponseBuilder builder,
Result result) {
- HealthCheck check = result.getCheck();
- Set<String> metaKeys = check.getMetaData().keySet();
-
- result.getDetails().forEach((key, value) -> {
- // Filter health check metadata to have a less verbose output
- if (!metaKeys.contains(key)) {
- builder.withData(key, value.toString());
- }
- });
-
- result.getError().ifPresent(error -> {
- builder.withData("error.message", error.getMessage());
-
- final StringWriter stackTraceWriter = new StringWriter();
- try (final PrintWriter pw = new PrintWriter(stackTraceWriter,
true)) {
- error.printStackTrace(pw);
- builder.withData("error.stacktrace",
stackTraceWriter.toString());
- }
- });
+ public static void applyHealthDetail(HealthCheckResponseBuilder builder,
Result result, String exposureLevel) {
+ if (!exposureLevel.equals("oneline")) {
+ HealthCheck check = result.getCheck();
+ Set<String> metaKeys = check.getMetaData().keySet();
+
+ result.getDetails().forEach((key, value) -> {
+ if (exposureLevel.equals("full")) {
+ builder.withData(key, value.toString());
+ } else {
+ // Filter health check metadata to have a less verbose
output
+ if (!metaKeys.contains(key)) {
+ builder.withData(key, value.toString());
+ }
+ }
+ });
+
+ result.getError().ifPresent(error -> {
+ builder.withData("error.message", error.getMessage());
+
+ final StringWriter stackTraceWriter = new StringWriter();
+ try (final PrintWriter pw = new PrintWriter(stackTraceWriter,
true)) {
+ error.printStackTrace(pw);
+ builder.withData("error.stacktrace",
stackTraceWriter.toString());
+ }
+ });
+ }
}
}
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileRepositoryHealthCheck.java
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileRepositoryHealthCheck.java
index ef614d5..90700e7 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileRepositoryHealthCheck.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileRepositoryHealthCheck.java
@@ -20,8 +20,10 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
+import org.apache.camel.CamelContext;
import org.apache.camel.health.HealthCheck.Result;
import org.apache.camel.health.HealthCheck.State;
+import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.eclipse.microprofile.health.HealthCheck;
@@ -34,10 +36,12 @@ import
org.eclipse.microprofile.health.HealthCheckResponseBuilder;
*/
final class CamelMicroProfileRepositoryHealthCheck implements HealthCheck {
+ private final CamelContext camelContext;
private final HealthCheckRepository repository;
private final String name;
- CamelMicroProfileRepositoryHealthCheck(HealthCheckRepository repository,
String name) {
+ CamelMicroProfileRepositoryHealthCheck(CamelContext camelContext,
HealthCheckRepository repository, String name) {
+ this.camelContext = camelContext;
this.repository = repository;
this.name = name;
}
@@ -56,11 +60,12 @@ final class CamelMicroProfileRepositoryHealthCheck
implements HealthCheck {
.collect(Collectors.toList());
// If any of the result statuses is DOWN, find the first one and
report any error details
+ HealthCheckRegistry registry =
HealthCheckRegistry.get(camelContext);
results.stream()
.filter(result -> result.getState().equals(State.DOWN))
.findFirst()
.ifPresent(result -> {
-
CamelMicroProfileHealthHelper.applyHealthDetail(builder, result);
+
CamelMicroProfileHealthHelper.applyHealthDetail(builder, result,
registry.getExposureLevel());
builder.down();
});
} else {
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java
index 41afc21..428639d 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java
@@ -33,7 +33,6 @@ import org.apache.camel.health.HealthCheckRepository;
import org.eclipse.microprofile.health.HealthCheckResponse.Status;
import org.junit.jupiter.api.Test;
-import static
org.apache.camel.microprofile.health.CamelMicroProfileHealthCheckRegistry.ROUTES_CHECK_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CamelMicroProfileHealthCheckRepositoryTest extends
CamelMicroProfileHealthTestSupport {
@@ -53,7 +52,7 @@ public class CamelMicroProfileHealthCheckRepositoryTest
extends CamelMicroProfil
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(ROUTES_CHECK_NAME, Status.UP,
checks.getJsonObject(0));
+ assertHealthCheckOutput("camel-routes", Status.UP,
checks.getJsonObject(0));
}
@Test
@@ -73,7 +72,7 @@ public class CamelMicroProfileHealthCheckRepositoryTest
extends CamelMicroProfil
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(ROUTES_CHECK_NAME, Status.DOWN,
checks.getJsonObject(0), jsonObject -> {
+ assertHealthCheckOutput("camel-routes", Status.DOWN,
checks.getJsonObject(0), jsonObject -> {
assertEquals("healthyRoute", jsonObject.getString("route.id"));
assertEquals(ServiceStatus.Stopped.name(),
jsonObject.getString("route.status"));
});
@@ -136,11 +135,150 @@ public class CamelMicroProfileHealthCheckRepositoryTest
extends CamelMicroProfil
assertEquals(Status.DOWN.name(), healthObject.getString("status"));
JsonArray checks = healthObject.getJsonArray("checks");
- assertEquals(2, checks.size());
+ assertEquals(1, checks.size());
+
+ assertHealthCheckOutput("camel-custom-repository", Status.DOWN,
checks.getJsonObject(0));
+ }
+
+ @Test
+ public void testRegisterHealthCheckRepositoryAllLiveness() {
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createLivenessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createLivenessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
- assertHealthCheckOutput("check-3", Status.DOWN,
checks.getJsonObject(0));
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
- assertHealthCheckOutput("check-1", Status.UP, checks.getJsonObject(1));
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(1, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(0, readinessChecks.size());
+ }
+
+ @Test
+ public void testRegisterHealthCheckRepositoryAllReadiness() {
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createReadinessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createReadinessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
+
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
+
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(0, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(1, readinessChecks.size());
+ }
+
+ @Test
+ public void testRegisterHealthCheckRepositoryMixedLivenessAndReadiness() {
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createLivenessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createReadinessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
+
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
+
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(1, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(1, readinessChecks.size());
}
@Test
@@ -157,7 +295,7 @@ public class CamelMicroProfileHealthCheckRepositoryTest
extends CamelMicroProfil
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(ROUTES_CHECK_NAME, Status.UP,
checks.getJsonObject(0));
+ assertHealthCheckOutput("camel-routes", Status.UP,
checks.getJsonObject(0));
healthCheckRegistry.unregister(hc);
@@ -170,6 +308,176 @@ public class CamelMicroProfileHealthCheckRepositoryTest
extends CamelMicroProfil
assertEquals(0, checks.size());
}
+ @Test
+ public void testUnregisterHealthCheckRepositoryAllLiveness() {
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createLivenessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createLivenessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
+
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
+
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(1, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(0, readinessChecks.size());
+
+ healthCheckRegistry.unregister(repository);
+
+ liveness = reporter.getLiveness();
+
+ livenessHealthObject = getHealthJson(liveness);
+ livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(0, livenessChecks.size());
+ }
+
+ @Test
+ public void testUnregisterHealthCheckRepositoryAllReadiness() {
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createReadinessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createReadinessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
+
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
+
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(0, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(1, readinessChecks.size());
+
+ healthCheckRegistry.unregister(repository);
+
+ readiness = reporter.getReadiness();
+
+ readinessHealthObject = getHealthJson(readiness);
+ readinessChecks = readinessHealthObject.getJsonArray("checks");
+ assertEquals(0, readinessChecks.size());
+ }
+
+ @Test
+ public void testUnregisterHealthCheckRepositoryMixedLivenessAndReadiness()
{
+ List<HealthCheck> repositoryChecks = new ArrayList<>();
+ repositoryChecks.add(createLivenessCheck("check-1", true, builder ->
builder.up()));
+ repositoryChecks.add(createReadinessCheck("check-2", true, builder ->
builder.up()));
+
+ HealthCheckRepository repository = new HealthCheckRepository() {
+ @Override
+ public boolean isEnabled() {
+ return true;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // Noop
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return repositoryChecks.stream();
+ }
+
+ @Override
+ public String getId() {
+ return "custom-repository";
+ }
+ };
+
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.register(repository);
+
+ SmallRyeHealth liveness = reporter.getLiveness();
+ SmallRyeHealth readiness = reporter.getReadiness();
+
+ JsonObject livenessHealthObject = getHealthJson(liveness);
+ assertEquals(Status.UP.name(),
livenessHealthObject.getString("status"));
+
+ JsonArray livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(1, livenessChecks.size());
+
+ JsonObject readinessHealthObject = getHealthJson(readiness);
+ assertEquals(Status.UP.name(),
readinessHealthObject.getString("status"));
+
+ JsonArray readinessChecks =
readinessHealthObject.getJsonArray("checks");
+ assertEquals(1, readinessChecks.size());
+
+ healthCheckRegistry.unregister(repository);
+
+ liveness = reporter.getLiveness();
+ readiness = reporter.getReadiness();
+
+ livenessHealthObject = getHealthJson(liveness);
+ livenessChecks = livenessHealthObject.getJsonArray("checks");
+ assertEquals(0, livenessChecks.size());
+
+ readinessHealthObject = getHealthJson(readiness);
+ readinessChecks = readinessHealthObject.getJsonArray("checks");
+ assertEquals(0, readinessChecks.size());
+ }
+
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java
index 6c8b5bf..a5d4e3d 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java
@@ -29,12 +29,14 @@ import org.apache.camel.health.HealthCheckResultBuilder;
import org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.apache.camel.impl.health.ContextHealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponse.Status;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
public class CamelMicroProfileHealthCheckTest extends
CamelMicroProfileHealthTestSupport {
@@ -323,4 +325,129 @@ public class CamelMicroProfileHealthCheckTest extends
CamelMicroProfileHealthTes
assertNotNull(jsonObject.getString("error.stacktrace"));
});
}
+
+ @Test
+ public void testExposureLevelDefault() {
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+
+ HealthCheck failingCheck = new AbstractHealthCheck("failing-check") {
+ @Override
+ public boolean isLiveness() {
+ return false;
+ }
+
+ @Override
+ protected void doCall(HealthCheckResultBuilder builder,
Map<String, Object> options) {
+ builder.error(new Exception("Forced exception"));
+ builder.down();
+ }
+ };
+
+ healthCheckRegistry.register(failingCheck);
+ healthCheckRegistry.register(createLivenessCheck("liveness-1", true,
builder -> builder.detail("test", "test").up()));
+
+ SmallRyeHealth health = reporter.getHealth();
+
+ JsonObject healthObject = getHealthJson(health);
+ assertEquals(HealthCheckResponse.Status.DOWN.name(),
healthObject.getString("status"));
+
+ JsonArray checks = healthObject.getJsonArray("checks");
+ assertEquals(2, checks.size());
+
+ JsonObject check = checks.getJsonObject(0);
+ assertHealthCheckOutput("liveness-1", HealthCheckResponse.Status.UP,
check, result -> {
+ assertNotNull(result);
+ assertEquals("test", result.getString("test"));
+ });
+
+ JsonObject failedCheck = checks.getJsonObject(1);
+ assertHealthCheckOutput("failing-check",
HealthCheckResponse.Status.DOWN, failedCheck, result -> {
+ assertNotNull(result);
+ assertEquals("Forced exception",
result.getString("error.message"));
+ assertNotNull(result.getString("error.stacktrace"));
+ });
+ }
+
+ @Test
+ public void testExposureLevelFull() {
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.setExposureLevel("full");
+
+ HealthCheck failingCheck = new AbstractHealthCheck("failing-check") {
+ @Override
+ public boolean isLiveness() {
+ return false;
+ }
+
+ @Override
+ protected void doCall(HealthCheckResultBuilder builder,
Map<String, Object> options) {
+ builder.error(new Exception("Forced exception"));
+ builder.down();
+ }
+ };
+
+ healthCheckRegistry.register(failingCheck);
+ healthCheckRegistry.register(createLivenessCheck("liveness-1", true,
builder -> builder.detail("test", "test").up()));
+
+ SmallRyeHealth health = reporter.getHealth();
+
+ JsonObject healthObject = getHealthJson(health);
+ assertEquals(HealthCheckResponse.Status.DOWN.name(),
healthObject.getString("status"));
+
+ JsonArray checks = healthObject.getJsonArray("checks");
+ assertEquals(2, checks.size());
+
+ JsonObject livenessCheck = checks.getJsonObject(0);
+ assertHealthCheckOutput("liveness-1", HealthCheckResponse.Status.UP,
livenessCheck, result -> {
+ assertNotNull(result);
+ assertEquals("test", result.getString("test"));
+ });
+
+ JsonObject failedCheck = checks.getJsonObject(1);
+ assertHealthCheckOutput("failing-check",
HealthCheckResponse.Status.DOWN, failedCheck, result -> {
+ assertNotNull(result);
+ assertEquals("Forced exception",
result.getString("error.message"));
+ assertNotNull(result.getString("error.stacktrace"));
+ });
+ }
+
+ @Test
+ public void testExposureLevelOneline() {
+ HealthCheckRegistry healthCheckRegistry =
HealthCheckRegistry.get(context);
+ healthCheckRegistry.setExposureLevel("oneline");
+
+ HealthCheck failingCheck = new AbstractHealthCheck("failing-check") {
+ @Override
+ public boolean isLiveness() {
+ return false;
+ }
+
+ @Override
+ protected void doCall(HealthCheckResultBuilder builder,
Map<String, Object> options) {
+ builder.error(new Exception("Forced exception"));
+ builder.down();
+ }
+ };
+
+ healthCheckRegistry.register(failingCheck);
+ healthCheckRegistry.register(createLivenessCheck("liveness-1", true,
builder -> builder.detail("test", "test").up()));
+
+ SmallRyeHealth health = reporter.getHealth();
+
+ JsonObject healthObject = getHealthJson(health);
+ assertEquals(HealthCheckResponse.Status.DOWN.name(),
healthObject.getString("status"));
+
+ JsonArray checks = healthObject.getJsonArray("checks");
+ assertEquals(2, checks.size());
+
+ JsonObject livenessCheck = checks.getJsonObject(0);
+ assertHealthCheckOutput("liveness-1", HealthCheckResponse.Status.UP,
livenessCheck, result -> {
+ assertNull(result);
+ });
+
+ JsonObject failedCheck = checks.getJsonObject(1);
+ assertHealthCheckOutput("failing-check",
HealthCheckResponse.Status.DOWN, failedCheck, result -> {
+ assertNull(result);
+ });
+ }
}
diff --git
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthConsumerTest.java
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthConsumerTest.java
index 09e2d0d..97c0b6a 100644
---
a/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthConsumerTest.java
+++
b/components/camel-microprofile/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthConsumerTest.java
@@ -27,7 +27,6 @@ import org.apache.camel.health.HealthCheckRegistry;
import org.eclipse.microprofile.health.HealthCheckResponse.Status;
import org.junit.jupiter.api.Test;
-import static
org.apache.camel.microprofile.health.CamelMicroProfileHealthCheckRegistry.CONSUMERS_CHECK_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CamelMicroProfileHealthConsumerTest extends
CamelMicroProfileHealthTestSupport {
@@ -47,7 +46,7 @@ public class CamelMicroProfileHealthConsumerTest extends
CamelMicroProfileHealth
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(CONSUMERS_CHECK_NAME, Status.UP,
checks.getJsonObject(0));
+ assertHealthCheckOutput("camel-consumers", Status.UP,
checks.getJsonObject(0));
}
@Test
@@ -67,7 +66,7 @@ public class CamelMicroProfileHealthConsumerTest extends
CamelMicroProfileHealth
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(CONSUMERS_CHECK_NAME, Status.DOWN,
checks.getJsonObject(0), jsonObject -> {
+ assertHealthCheckOutput("camel-consumers", Status.DOWN,
checks.getJsonObject(0), jsonObject -> {
assertEquals("healthyRoute", jsonObject.getString("route.id"));
assertEquals(ServiceStatus.Stopped.name(),
jsonObject.getString("route.status"));
});
@@ -87,7 +86,7 @@ public class CamelMicroProfileHealthConsumerTest extends
CamelMicroProfileHealth
JsonArray checks = healthObject.getJsonArray("checks");
assertEquals(1, checks.size());
- assertHealthCheckOutput(CONSUMERS_CHECK_NAME, Status.UP,
checks.getJsonObject(0));
+ assertHealthCheckOutput("camel-consumers", Status.UP,
checks.getJsonObject(0));
healthCheckRegistry.unregister(hc);