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-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 22a6e0429b9 CAMEL-18832: camel-spring-boot - Health Check output 
should include data in full exposure level. Aligned output to be similar to 
microprofile-health.
22a6e0429b9 is described below

commit 22a6e0429b9fc995594cf334298c7f617027d1e6
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Dec 21 09:10:49 2022 +0100

    CAMEL-18832: camel-spring-boot - Health Check output should include data in 
full exposure level. Aligned output to be similar to microprofile-health.
---
 .../health/CamelHealthCheckAutoConfiguration.java  |  2 +-
 .../actuate/health/CamelHealthCheckIndicator.java  |  8 ++-
 .../boot/actuate/health/CamelHealthHelper.java     | 76 ++++++++++++++++++++++
 3 files changed, 84 insertions(+), 2 deletions(-)

diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
index 5b982870d20..b13ff52d03c 100644
--- 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
@@ -135,7 +135,7 @@ public class CamelHealthCheckAutoConfiguration {
                 }
             }
 
-            return new CamelHealthCheckIndicator(applicationContext, 
camelContext);
+            return new CamelHealthCheckIndicator(applicationContext, 
camelContext, config.getExposureLevel());
         }
     }
 
diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckIndicator.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckIndicator.java
index 7e0563a3e55..d1a3bff893a 100644
--- 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckIndicator.java
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckIndicator.java
@@ -36,9 +36,12 @@ public class CamelHealthCheckIndicator extends 
AbstractHealthIndicator {
     private final ApplicationContext applicationContext;
     private final CamelContext camelContext;
 
-    public CamelHealthCheckIndicator(ApplicationContext applicationContext, 
CamelContext camelContext) {
+    private final String exposureLevel;
+
+    public CamelHealthCheckIndicator(ApplicationContext applicationContext, 
CamelContext camelContext, String exposureLevel) {
         this.applicationContext = applicationContext;
         this.camelContext = camelContext;
+        this.exposureLevel = exposureLevel;
     }
 
     @Override
@@ -58,6 +61,9 @@ public class CamelHealthCheckIndicator extends 
AbstractHealthIndicator {
 
             if (enabled) {
                 builder.withDetail(result.getCheck().getId(), 
result.getState().name());
+
+                CamelHealthHelper.applyHealthDetail(builder, result, 
exposureLevel);
+
                 if (result.getState() == HealthCheck.State.DOWN) {
                     builder.down();
                 }
diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthHelper.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthHelper.java
new file mode 100644
index 00000000000..a7ef0571397
--- /dev/null
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthHelper.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.spring.boot.actuate.health;
+
+import org.apache.camel.health.HealthCheck;
+import org.springframework.boot.actuate.health.Health;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+final class CamelHealthHelper {
+
+    private CamelHealthHelper() {
+    }
+
+    /**
+     * Propagates details from the Camel Health {@link HealthCheck.Result} to 
the Spring Boot {@link Health.Builder}.
+     *
+     * @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(Health.Builder builder, 
HealthCheck.Result result, String exposureLevel) {
+        if (!exposureLevel.equals("oneline")) {
+            HealthCheck check = result.getCheck();
+            Set<String> metaKeys = check.getMetaData().keySet();
+
+            final Map<String, String> data = new LinkedHashMap<>();
+            result.getDetails().forEach((key, value) -> {
+                if (value != null) {
+                    if (exposureLevel.equals("full")) {
+                        data.put(key, value.toString());
+                    } else {
+                        // Filter health check metadata to have a less verbose 
output
+                        if (!metaKeys.contains(key)) {
+                            data.put(key, value.toString());
+                        }
+                    }
+                }
+            });
+
+            result.getError().ifPresent(error -> {
+                builder.withDetail("error.message", error.getMessage());
+                final StringWriter stackTraceWriter = new StringWriter();
+                try (final PrintWriter pw = new PrintWriter(stackTraceWriter, 
true)) {
+                    error.printStackTrace(pw);
+                    data.put("error.stacktrace", stackTraceWriter.toString());
+                }
+            });
+
+            if (!data.isEmpty()) {
+                String id = result.getCheck().getId() + ".data";
+                builder.withDetail(id, data);
+            }
+        }
+    }
+
+}

Reply via email to