You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2023/02/01 09:52:14 UTC

[camel] branch main updated: CAMEL-18991 - Camel-Google-Storage: Health Check for consumer

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

acosentino 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 01f1f221c3e CAMEL-18991 - Camel-Google-Storage: Health Check for consumer
01f1f221c3e is described below

commit 01f1f221c3ebed443d3915db4518acd61496aa28
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Feb 1 10:49:57 2023 +0100

    CAMEL-18991 - Camel-Google-Storage: Health Check for consumer
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../camel-google/camel-google-storage/pom.xml      |  4 ++
 .../google/storage/GoogleCloudStorageConsumer.java | 15 +++++
 .../GoogleCloudStorageConsumerHealthCheck.java     | 73 ++++++++++++++++++++++
 3 files changed, 92 insertions(+)

diff --git a/components/camel-google/camel-google-storage/pom.xml b/components/camel-google/camel-google-storage/pom.xml
index 1f958ca5d32..4310895e5bd 100644
--- a/components/camel-google/camel-google-storage/pom.xml
+++ b/components/camel-google/camel-google-storage/pom.xml
@@ -56,6 +56,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-support</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-health</artifactId>
+        </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
diff --git a/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumer.java b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumer.java
index b2a754d5c62..6f839702b55 100644
--- a/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumer.java
+++ b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumer.java
@@ -37,6 +37,8 @@ import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.health.HealthCheckHelper;
+import org.apache.camel.health.WritableHealthCheckRepository;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.Synchronization;
 import org.apache.camel.support.EmptyAsyncCallback;
@@ -53,6 +55,9 @@ public class GoogleCloudStorageConsumer extends ScheduledBatchPollingConsumer {
 
     private final Language language;
 
+    private WritableHealthCheckRepository healthCheckRepository;
+    private GoogleCloudStorageConsumerHealthCheck consumerHealthCheck;
+
     public GoogleCloudStorageConsumer(GoogleCloudStorageEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
         this.language = getEndpoint().getCamelContext().resolveLanguage("file");
@@ -62,6 +67,16 @@ public class GoogleCloudStorageConsumer extends ScheduledBatchPollingConsumer {
     protected void doStart() throws Exception {
         super.doStart();
 
+        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(
+                getEndpoint().getCamelContext(),
+                "components",
+                WritableHealthCheckRepository.class);
+
+        if (healthCheckRepository != null) {
+            consumerHealthCheck = new GoogleCloudStorageConsumerHealthCheck(this, getRouteId());
+            healthCheckRepository.addHealthCheck(consumerHealthCheck);
+        }
+
         if (getConfiguration().isMoveAfterRead()) {
             Bucket bucket = getStorageClient().get(getConfiguration().getDestinationBucket());
             if (bucket != null) {
diff --git a/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumerHealthCheck.java b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumerHealthCheck.java
new file mode 100644
index 00000000000..d4942c119ff
--- /dev/null
+++ b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStorageConsumerHealthCheck.java
@@ -0,0 +1,73 @@
+/*
+ * 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.component.google.storage;
+
+import java.util.Map;
+
+import com.google.api.gax.rpc.ApiException;
+import com.google.cloud.storage.Storage;
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.impl.health.AbstractHealthCheck;
+import org.apache.camel.util.ObjectHelper;
+
+public class GoogleCloudStorageConsumerHealthCheck extends AbstractHealthCheck {
+
+    private final GoogleCloudStorageConsumer googleCloudStorageConsumer;
+
+    public GoogleCloudStorageConsumerHealthCheck(GoogleCloudStorageConsumer googleCloudStorageConsumer, String routeId) {
+        super("camel", "google-cloud-storage-consumer-" + routeId);
+        this.googleCloudStorageConsumer = googleCloudStorageConsumer;
+    }
+
+    @Override
+    public boolean isLiveness() {
+        // this health check is only readiness
+        return false;
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+        Storage client;
+        try {
+            GoogleCloudStorageConfiguration configuration = googleCloudStorageConsumer.getConfiguration();
+            if (ObjectHelper.isNotEmpty(configuration.getStorageClient())) {
+                client = configuration.getStorageClient();
+            } else {
+                client = googleCloudStorageConsumer.getStorageClient();
+            }
+            client.list();
+        } catch (ApiException e) {
+            builder.message(e.getMessage());
+            builder.error(e);
+            if (ObjectHelper.isNotEmpty(e.getStatusCode())) {
+                builder.detail(SERVICE_STATUS_CODE, e.getStatusCode());
+            }
+            if (ObjectHelper.isNotEmpty(e.getStatusCode().getCode())) {
+                builder.detail(SERVICE_ERROR_CODE, e.getStatusCode().getCode());
+            }
+            builder.down();
+            return;
+
+        } catch (Exception e) {
+            builder.error(e);
+            builder.down();
+            return;
+        }
+        builder.up();
+
+    }
+}