This is an automated email from the ASF dual-hosted git repository.

davsclaus 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 77cca66  CAMEL-17627: camel-health - ExposureLevel option to control 
the level of checks in the result
77cca66 is described below

commit 77cca6637e46a3df2cc852beadf6e6449fdcdc8a
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Feb 10 15:32:14 2022 +0100

    CAMEL-17627: camel-health - ExposureLevel option to control the level of 
checks in the result
---
 .../org/apache/camel/health/HealthCheckHelper.java | 33 +++++++++++++++++--
 .../apache/camel/health/HealthCheckRegistry.java   | 18 +++++++++++
 .../impl/health/DefaultHealthCheckRegistry.java    | 11 +++++++
 .../HealthConfigurationPropertiesConfigurer.java   |  6 ++++
 .../camel-main-configuration-metadata.json         |  1 +
 core/camel-main/src/main/docs/main.adoc            |  3 +-
 .../org/apache/camel/main/BaseMainSupport.java     |  3 ++
 .../camel/main/HealthConfigurationProperties.java  | 37 ++++++++++++++++++++++
 .../modules/ROOT/pages/health-check.adoc           | 30 ++++++++++++++----
 9 files changed, 132 insertions(+), 10 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckHelper.java 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckHelper.java
index 6336c1e..9e8adcd 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckHelper.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckHelper.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.health;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -98,9 +99,7 @@ public final class HealthCheckHelper {
         final HealthCheckRegistry registry = 
HealthCheckRegistry.get(camelContext);
 
         if (registry != null) {
-            // If no health check service is defined, this endpoint invokes the
-            // check one by one.
-            return registry.stream()
+            Collection<HealthCheck.Result> result = registry.stream()
                     
.collect(Collectors.groupingBy(HealthCheckHelper::getGroup))
                     .values().stream()
                     .flatMap(Collection::stream)
@@ -109,6 +108,34 @@ public final class HealthCheckHelper {
                     .distinct()
                     .map(check -> check.call(optionsSupplier.apply(check)))
                     .collect(Collectors.toList());
+
+            if (result.isEmpty()) {
+                return Collections.emptyList();
+            }
+
+            // the result includes all the details
+            if ("full".equals(registry.getExposureLevel())) {
+                return result;
+            } else {
+                // are there any downs?
+                Collection<HealthCheck.Result> downs = 
result.stream().filter(r -> r.getState().equals(HealthCheck.State.DOWN))
+                        .collect(Collectors.toCollection(ArrayList::new));
+
+                // default mode is to either be just UP or include all DOWNs
+                // oneline mode is either UP or DOWN
+                if (!downs.isEmpty()) {
+                    if ("oneline".equals(registry.getExposureLevel())) {
+                        // grab first down
+                        return Collections.singleton(downs.iterator().next());
+                    } else {
+                        return downs;
+                    }
+                } else {
+                    // all up so grab first
+                    HealthCheck.Result up = result.iterator().next();
+                    return Collections.singleton(up);
+                }
+            }
         }
 
         return Collections.emptyList();
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
index 8101c04..eeb152d 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java
@@ -138,4 +138,22 @@ public interface HealthCheckRegistry extends 
CamelContextAware, StaticService, I
      */
     boolean isExcluded(HealthCheck healthCheck);
 
+    /**
+     * Sets the level of details to exposure as result of invoking health 
checks. There are the following levels: full,
+     * default, oneline
+     *
+     * The full level will include all details and status from all the invoked 
health checks.
+     *
+     * The default level will report UP if everything is okay, and only 
include detailed information for health checks
+     * that was DOWN.
+     *
+     * The oneline level will only report either UP or DOWN.
+     */
+    void setExposureLevel(String exposureLevel);
+
+    /**
+     * The exposure level
+     */
+    String getExposureLevel();
+
 }
diff --git 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
index 0190494..dd572af 100644
--- 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
+++ 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
@@ -53,6 +53,7 @@ public class DefaultHealthCheckRegistry extends 
ServiceSupport implements Health
     private CamelContext camelContext;
     private boolean enabled = true;
     private String excludePattern;
+    private String exposureLevel = "default";
     private volatile boolean loadHealthChecksDone;
 
     public DefaultHealthCheckRegistry() {
@@ -97,6 +98,16 @@ public class DefaultHealthCheckRegistry extends 
ServiceSupport implements Health
     }
 
     @Override
+    public String getExposureLevel() {
+        return exposureLevel;
+    }
+
+    @Override
+    public void setExposureLevel(String exposureLevel) {
+        this.exposureLevel = exposureLevel;
+    }
+
+    @Override
     protected void doInit() throws Exception {
         super.doInit();
 
diff --git 
a/core/camel-main/src/generated/java/org/apache/camel/main/HealthConfigurationPropertiesConfigurer.java
 
b/core/camel-main/src/generated/java/org/apache/camel/main/HealthConfigurationPropertiesConfigurer.java
index 897ffc2..a09904a 100644
--- 
a/core/camel-main/src/generated/java/org/apache/camel/main/HealthConfigurationPropertiesConfigurer.java
+++ 
b/core/camel-main/src/generated/java/org/apache/camel/main/HealthConfigurationPropertiesConfigurer.java
@@ -29,6 +29,8 @@ public class HealthConfigurationPropertiesConfigurer extends 
org.apache.camel.su
         case "Enabled": target.setEnabled(property(camelContext, 
java.lang.Boolean.class, value)); return true;
         case "excludepattern":
         case "ExcludePattern": target.setExcludePattern(property(camelContext, 
java.lang.String.class, value)); return true;
+        case "exposurelevel":
+        case "ExposureLevel": target.setExposureLevel(property(camelContext, 
java.lang.String.class, value)); return true;
         case "registryenabled":
         case "RegistryEnabled": 
target.setRegistryEnabled(property(camelContext, java.lang.Boolean.class, 
value)); return true;
         case "routesenabled":
@@ -48,6 +50,8 @@ public class HealthConfigurationPropertiesConfigurer extends 
org.apache.camel.su
         case "Enabled": return java.lang.Boolean.class;
         case "excludepattern":
         case "ExcludePattern": return java.lang.String.class;
+        case "exposurelevel":
+        case "ExposureLevel": return java.lang.String.class;
         case "registryenabled":
         case "RegistryEnabled": return java.lang.Boolean.class;
         case "routesenabled":
@@ -68,6 +72,8 @@ public class HealthConfigurationPropertiesConfigurer extends 
org.apache.camel.su
         case "Enabled": return target.getEnabled();
         case "excludepattern":
         case "ExcludePattern": return target.getExcludePattern();
+        case "exposurelevel":
+        case "ExposureLevel": return target.getExposureLevel();
         case "registryenabled":
         case "RegistryEnabled": return target.getRegistryEnabled();
         case "routesenabled":
diff --git 
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
 
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index 1d8e638..6a52b32 100644
--- 
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ 
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -135,6 +135,7 @@
     { "name": "camel.health.contextEnabled", "description": "Whether context 
health check is enabled", "sourceType": 
"org.apache.camel.main.HealthConfigurationProperties", "type": "boolean", 
"javaType": "java.lang.Boolean", "defaultValue": true },
     { "name": "camel.health.enabled", "description": "Whether health check is 
enabled globally", "sourceType": 
"org.apache.camel.main.HealthConfigurationProperties", "type": "boolean", 
"javaType": "java.lang.Boolean", "defaultValue": true },
     { "name": "camel.health.excludePattern", "description": "Pattern to 
exclude health checks from being invoked by Camel when checking healths. 
Multiple patterns can be separated by comma.", "sourceType": 
"org.apache.camel.main.HealthConfigurationProperties", "type": "string", 
"javaType": "java.lang.String" },
+    { "name": "camel.health.exposureLevel", "description": "Sets the level of 
details to exposure as result of invoking health checks. There are the 
following levels: full, default, oneline The full level will include all 
details and status from all the invoked health checks. The default level will 
report UP if everything is okay, and only include detailed information for 
health checks that was DOWN. The oneline level will only report either UP or 
DOWN.", "sourceType": "org.apache.camel. [...]
     { "name": "camel.health.registryEnabled", "description": "Whether registry 
health check is enabled", "sourceType": 
"org.apache.camel.main.HealthConfigurationProperties", "type": "boolean", 
"javaType": "java.lang.Boolean", "defaultValue": true },
     { "name": "camel.health.routesEnabled", "description": "Whether routes 
health check is enabled", "sourceType": 
"org.apache.camel.main.HealthConfigurationProperties", "type": "boolean", 
"javaType": "java.lang.Boolean", "defaultValue": true },
     { "name": "camel.hystrix.allowMaximumSizeToDivergeFromCoreSize", 
"description": "Allows the configuration for maximumSize to take effect. That 
value can then be equal to, or higher, than coreSize", "sourceType": 
"org.apache.camel.main.HystrixConfigurationProperties", "type": "boolean", 
"javaType": "java.lang.Boolean", "defaultValue": "false", "deprecated": true },
diff --git a/core/camel-main/src/main/docs/main.adoc 
b/core/camel-main/src/main/docs/main.adoc
index 286680c..e2f080a 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -150,7 +150,7 @@ The camel.threadpool supports 8 options, which are listed 
below.
 |===
 
 === Camel Health Check configurations
-The camel.health supports 6 options, which are listed below.
+The camel.health supports 7 options, which are listed below.
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
@@ -159,6 +159,7 @@ The camel.health supports 6 options, which are listed below.
 | *camel.health.contextEnabled* | Whether context health check is enabled | 
true | Boolean
 | *camel.health.enabled* | Whether health check is enabled globally | true | 
Boolean
 | *camel.health.excludePattern* | Pattern to exclude health checks from being 
invoked by Camel when checking healths. Multiple patterns can be separated by 
comma. |  | String
+| *camel.health.exposureLevel* | Sets the level of details to exposure as 
result of invoking health checks. There are the following levels: full, 
default, oneline The full level will include all details and status from all 
the invoked health checks. The default level will report UP if everything is 
okay, and only include detailed information for health checks that was DOWN. 
The oneline level will only report either UP or DOWN. | default | String
 | *camel.health.registryEnabled* | Whether registry health check is enabled | 
true | Boolean
 | *camel.health.routesEnabled* | Whether routes health check is enabled | true 
| Boolean
 |===
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java 
b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 50158f6..ff649be 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -1001,6 +1001,9 @@ public abstract class BaseMainSupport extends BaseService 
{
         if (health.getExcludePattern() != null) {
             hcr.setExcludePattern(health.getExcludePattern());
         }
+        if (health.getExposureLevel() != null) {
+            hcr.setExposureLevel(health.getExposureLevel());
+        }
 
         // context is enabled by default
         if (hcr.isEnabled()) {
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/HealthConfigurationProperties.java
 
b/core/camel-main/src/main/java/org/apache/camel/main/HealthConfigurationProperties.java
index 69dc26c..04ea3af 100644
--- 
a/core/camel-main/src/main/java/org/apache/camel/main/HealthConfigurationProperties.java
+++ 
b/core/camel-main/src/main/java/org/apache/camel/main/HealthConfigurationProperties.java
@@ -40,6 +40,8 @@ public class HealthConfigurationProperties implements 
BootstrapCloseable {
     private Boolean registryEnabled;
     @Metadata
     private String excludePattern;
+    @Metadata(enums = "full,default,oneline", defaultValue = "default")
+    private String exposureLevel;
 
     public HealthConfigurationProperties(MainConfigurationProperties parent) {
         this.parent = parent;
@@ -121,6 +123,25 @@ public class HealthConfigurationProperties implements 
BootstrapCloseable {
         this.excludePattern = excludePattern;
     }
 
+    public String getExposureLevel() {
+        return exposureLevel;
+    }
+
+    /**
+     * Sets the level of details to exposure as result of invoking health 
checks. There are the following levels: full,
+     * default, oneline
+     *
+     * The full level will include all details and status from all the invoked 
health checks.
+     *
+     * The default level will report UP if everything is okay, and only 
include detailed information for health checks
+     * that was DOWN.
+     *
+     * The oneline level will only report either UP or DOWN.
+     */
+    public void setExposureLevel(String exposureLevel) {
+        this.exposureLevel = exposureLevel;
+    }
+
     /**
      * Whether health check is enabled globally
      */
@@ -162,4 +183,20 @@ public class HealthConfigurationProperties implements 
BootstrapCloseable {
         return this;
     }
 
+    /**
+     * Sets the level of details to exposure as result of invoking health 
checks. There are the following levels: full,
+     * default, oneline
+     *
+     * The full level will include all details and status from all the invoked 
health checks.
+     *
+     * The default level will report UP if everything is okay, and only 
include detailed information for health checks
+     * that was DOWN.
+     *
+     * The oneline level will only report either UP or DOWN.
+     */
+    public HealthConfigurationProperties withExposureLevel(String 
exposureLevel) {
+        this.exposureLevel = exposureLevel;
+        return this;
+    }
+
 }
diff --git a/docs/user-manual/modules/ROOT/pages/health-check.adoc 
b/docs/user-manual/modules/ROOT/pages/health-check.adoc
index a693b17..a8b07f0 100644
--- a/docs/user-manual/modules/ROOT/pages/health-check.adoc
+++ b/docs/user-manual/modules/ROOT/pages/health-check.adoc
@@ -120,15 +120,26 @@ This MBean allows at runtime to manage health-checks 
where you can enable and di
 As well have the latest status whether the overall health check is healthy or 
not.
 The MBean also allows invoking health checks based on IDs (or all of them).
 
-== Invoking health checks
+=== Configuring level of details in the health check responses
 
-You can invoke the health checks from Java by using the 
`org.apache.camel.health.HealthCheckHelper` which has APIs
-to easily invoke all the health checks and gather their results, or filter out 
unwanted checks, or invoke only
-the readiness or liveness checks.
+The option `exposureLevel` sets the level of details to exposure as result of 
invoking health checks.
 
-The health checks can also be invoked from JMX.
+There are the following levels:
+
+- _full_ - The full level will include all details and status from all the 
invoked health checks.
+- _default_ - The default level will report UP if everything is okay, and only 
include detailed information for health check that was DOWN.
+- _oneline_ - The oneline level will only report either UP or DOWN.
+
+You can specify multiple patterns (and use wildcards) such as:
+
+For example to only report either as UP or DOWN then use:
+
+[source,properties]
+----
+camel.health.exposure-level = oneline
+----
 
-== Enriching and controlling health check responses
+=== Enriching and controlling health check responses
 
 Each health check that is invoked will gather details about the result using 
`HealthCheckResultBuilder`.
 
@@ -139,6 +150,13 @@ etc. on the result builder.
 The custom bean should be registered to the Camel xref:registry.adoc[Registry] 
to be discovered
 by `camel-health`. Only one instance of the bean is allowed.
 
+== Invoking health checks
+
+You can invoke the health checks from Java by using the 
`org.apache.camel.health.HealthCheckHelper` which has APIs
+to easily invoke all the health checks and gather their results, or filter out 
unwanted checks, or invoke only
+the readiness or liveness checks.
+
+The health checks can also be invoked from JMX.
 
 == Writing a custom health check
 

Reply via email to