You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/10/29 11:17:00 UTC

[camel] 02/16: CAMEL-15133: Adding fine-grained consumer health check to route checks

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

davsclaus pushed a commit to branch custom-health
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9260adbb0da1642e1161790a3ff76c21a3e177eb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 28 08:29:03 2021 +0200

    CAMEL-15133: Adding fine-grained consumer health check to route checks
---
 .../java/org/apache/camel/health/HealthCheck.java  |  6 ++--
 .../org/apache/camel/health/HealthCheckAware.java  | 27 ++++++++++++++++++
 .../apache/camel/impl/health/RouteHealthCheck.java | 33 ++++++++++++++++++++++
 .../camel/support/ScheduledPollConsumer.java       |  3 +-
 4 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java
index e323150..9d29173 100644
--- a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java
+++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheck.java
@@ -41,7 +41,7 @@ public interface HealthCheck extends HasGroup, HasId, Ordered {
     }
 
     /**
-     * Return meta data associated with this {@link HealthCheck}.
+     * Return metadata associated with this {@link HealthCheck}.
      */
     default Map<String, Object> getMetaData() {
         return Collections.emptyMap();
@@ -86,7 +86,7 @@ public interface HealthCheck extends HasGroup, HasId, Ordered {
     Result call(Map<String, Object> options);
 
     /**
-     * Response to an health check invocation.
+     * Response to a health check invocation.
      */
     interface Result {
 
@@ -111,7 +111,7 @@ public interface HealthCheck extends HasGroup, HasId, Ordered {
         Optional<Throwable> getError();
 
         /**
-         * An key/value combination of details.
+         * A key/value combination of details.
          *
          * @return a non null details map
          */
diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckAware.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckAware.java
new file mode 100644
index 0000000..8a7f025
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckAware.java
@@ -0,0 +1,27 @@
+/*
+ * 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.health;
+
+import org.apache.camel.health.HealthCheck;
+
+public interface HealthCheckAware {
+
+    HealthCheck getHealthCheck();
+
+    void setHealthCheck(HealthCheck healthCheck);
+
+}
diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
index 44cde6b..4206eb8 100644
--- a/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
+++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
@@ -19,15 +19,21 @@ package org.apache.camel.impl.health;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
 import org.apache.camel.Route;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.health.HealthCheckAware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * {@link org.apache.camel.health.HealthCheck} for a given route.
  */
 public class RouteHealthCheck extends AbstractHealthCheck {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(RouteHealthCheck.class);
+
     private final Route route;
 
     public RouteHealthCheck(Route route) {
@@ -75,6 +81,33 @@ public class RouteHealthCheck extends AbstractHealthCheck {
                     }
                 }
             }
+
+            // check fine-grained consumer health check if we are up as the route may be up and running
+            // but the consumer can be un-healthy
+            if (State.UP.equals(builder.state())) {
+                Consumer consumer = route.getConsumer();
+                if (consumer instanceof HealthCheckAware) {
+                    HealthCheckAware chc = (HealthCheckAware) consumer;
+                    if (chc.getHealthCheck() != null) {
+                        if (LOGGER.isTraceEnabled()) {
+                            LOGGER.trace("Calling HealthCheck on consumer route: {}", route.getId());
+                        }
+                        Result result = chc.getHealthCheck().call();
+                        if (LOGGER.isDebugEnabled()) {
+                            LOGGER.debug("HealthCheck consumer route: {} -> {}", route.getId(), result.getState());
+                        }
+
+                        builder.state(result.getState());
+                        if (result.getMessage().isPresent()) {
+                            builder.message(result.getMessage().get());
+                        }
+                        if (result.getError().isPresent()) {
+                            builder.error(result.getError().get());
+                        }
+                        builder.details(result.getDetails());
+                    }
+                }
+            }
         }
     }
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollConsumer.java b/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollConsumer.java
index 3dd81b1..6106c21 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollConsumer.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollConsumer.java
@@ -29,6 +29,7 @@ import org.apache.camel.LoggingLevel;
 import org.apache.camel.PollingConsumerPollingStrategy;
 import org.apache.camel.Processor;
 import org.apache.camel.Suspendable;
+import org.apache.camel.health.HealthCheckAware;
 import org.apache.camel.spi.PollingConsumerPollStrategy;
 import org.apache.camel.spi.ScheduledPollConsumerScheduler;
 import org.apache.camel.support.service.ServiceHelper;
@@ -41,7 +42,7 @@ import org.slf4j.LoggerFactory;
  * A useful base class for any consumer which is polling based
  */
 public abstract class ScheduledPollConsumer extends DefaultConsumer
-        implements Runnable, Suspendable, PollingConsumerPollingStrategy {
+        implements Runnable, Suspendable, PollingConsumerPollingStrategy, HealthCheckAware {
 
     private static final Logger LOG = LoggerFactory.getLogger(ScheduledPollConsumer.class);