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 2022/10/21 12:00:26 UTC

[camel] 01/04: CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - AWS Kinesis

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

commit 9cc7e2514fb7ec11d88e931ba693c1b419be99cf
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Oct 21 13:28:17 2022 +0200

    CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - AWS Kinesis
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 components/camel-aws/camel-aws2-kinesis/pom.xml    |  4 +
 .../component/aws2/kinesis/Kinesis2Component.java  |  2 -
 .../component/aws2/kinesis/Kinesis2Consumer.java   | 19 +++++
 .../aws2/kinesis/Kinesis2ConsumerHealthCheck.java  | 89 +++++++++++++++++++++
 .../KinesisComponentVerifierExtensionTest.java     | 91 ----------------------
 5 files changed, 112 insertions(+), 93 deletions(-)

diff --git a/components/camel-aws/camel-aws2-kinesis/pom.xml b/components/camel-aws/camel-aws2-kinesis/pom.xml
index 8e9002e8acb..02635dffa8e 100644
--- a/components/camel-aws/camel-aws2-kinesis/pom.xml
+++ b/components/camel-aws/camel-aws2-kinesis/pom.xml
@@ -55,6 +55,10 @@
             <artifactId>apache-client</artifactId>
             <version>${aws-java-sdk2-version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-health</artifactId>
+        </dependency>
 
         <!-- for testing -->
         <dependency>
diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Component.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Component.java
index 6d8fd79379f..1bdf0e6ff9f 100644
--- a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Component.java
+++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Component.java
@@ -36,8 +36,6 @@ public class Kinesis2Component extends DefaultComponent {
 
     public Kinesis2Component(CamelContext context) {
         super(context);
-
-        registerExtension(new Kinesis2ComponentVerifierExtension());
     }
 
     @Override
diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
index 2ec8a11410a..85104c473b4 100644
--- a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
+++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
@@ -24,6 +24,8 @@ import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.aws2.kinesis.consumer.KinesisResumeAdapter;
+import org.apache.camel.health.HealthCheckHelper;
+import org.apache.camel.health.WritableHealthCheckRepository;
 import org.apache.camel.resume.ResumeAware;
 import org.apache.camel.resume.ResumeStrategy;
 import org.apache.camel.support.ScheduledBatchPollingConsumer;
@@ -50,6 +52,9 @@ public class Kinesis2Consumer extends ScheduledBatchPollingConsumer implements R
     private boolean isShardClosed;
     private ResumeStrategy resumeStrategy;
 
+    private WritableHealthCheckRepository healthCheckRepository;
+    private Kinesis2ConsumerHealthCheck consumerHealthCheck;
+
     public Kinesis2Consumer(Kinesis2Endpoint endpoint, Processor processor) {
         super(endpoint, processor);
     }
@@ -236,8 +241,22 @@ public class Kinesis2Consumer extends ScheduledBatchPollingConsumer implements R
     protected void doStart() throws Exception {
         super.doStart();
 
+        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(
+                getEndpoint().getCamelContext(),
+                "components",
+                WritableHealthCheckRepository.class);
+
+        if (healthCheckRepository != null) {
+            consumerHealthCheck = new Kinesis2ConsumerHealthCheck(this, getRouteId());
+            healthCheckRepository.addHealthCheck(consumerHealthCheck);
+        }
+
         if (resumeStrategy != null) {
             resumeStrategy.loadCache();
         }
     }
+
+    protected Kinesis2Configuration getConfiguration() {
+        return getEndpoint().getConfiguration();
+    }
 }
diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2ConsumerHealthCheck.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2ConsumerHealthCheck.java
new file mode 100644
index 00000000000..1a420defb94
--- /dev/null
+++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2ConsumerHealthCheck.java
@@ -0,0 +1,89 @@
+/*
+ * 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.aws2.kinesis;
+
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.impl.health.AbstractHealthCheck;
+import org.apache.camel.util.ObjectHelper;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.kinesis.KinesisClient;
+import software.amazon.awssdk.services.kinesis.KinesisClientBuilder;
+
+import java.util.Map;
+
+public class Kinesis2ConsumerHealthCheck extends AbstractHealthCheck {
+
+    private final Kinesis2Consumer kinesis2Consumer;
+    private final String routeId;
+
+    public Kinesis2ConsumerHealthCheck(Kinesis2Consumer kinesis2Consumer, String routeId) {
+        super("camel", "aws2-kinesis-consumer-" + routeId);
+        this.kinesis2Consumer = kinesis2Consumer;
+        this.routeId = routeId;
+    }
+
+    @Override
+    public boolean isLiveness() {
+        // this health check is only readiness
+        return false;
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+
+        try {
+            Kinesis2Configuration configuration = kinesis2Consumer.getConfiguration();
+            if (!KinesisClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) {
+                builder.message("The service is not supported in this region");
+                builder.down();
+                return;
+            }
+            KinesisClient client;
+            if (!configuration.isUseDefaultCredentialsProvider()) {
+                AwsBasicCredentials cred
+                        = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey());
+                KinesisClientBuilder clientBuilder = KinesisClient.builder();
+                client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred))
+                        .region(Region.of(configuration.getRegion())).build();
+            } else if (ObjectHelper.isNotEmpty(configuration.getAmazonKinesisClient())) {
+                client = configuration.getAmazonKinesisClient();
+            } else {
+                KinesisClientBuilder clientBuilder = KinesisClient.builder();
+                client = clientBuilder.region(Region.of(configuration.getRegion())).build();
+            }
+            client.listStreams();
+        } catch (SdkClientException e) {
+            builder.message(e.getMessage());
+            builder.error(e);
+            builder.down();
+            return;
+
+        } catch (Exception e) {
+            builder.error(e);
+            builder.down();
+            return;
+        }
+
+        builder.up();
+
+    }
+
+
+}
diff --git a/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisComponentVerifierExtensionTest.java b/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisComponentVerifierExtensionTest.java
deleted file mode 100644
index 4c6aac74959..00000000000
--- a/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisComponentVerifierExtensionTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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.aws2.kinesis;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.camel.Component;
-import org.apache.camel.component.extension.ComponentVerifierExtension;
-import org.apache.camel.test.junit5.CamelTestSupport;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-public class KinesisComponentVerifierExtensionTest extends CamelTestSupport {
-
-    // *************************************************
-    // Tests (parameters)
-    // *************************************************
-    @Override
-    public boolean isUseRouteBuilder() {
-        return false;
-    }
-
-    @Test
-    public void testParameters() {
-        Component component = context().getComponent("aws2-kinesis");
-
-        ComponentVerifierExtension verifier
-                = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
-
-        Map<String, Object> parameters = new HashMap<>();
-        parameters.put("secretKey", "l");
-        parameters.put("accessKey", "k");
-        parameters.put("region", "l");
-        parameters.put("streamName", "test");
-
-        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
-
-        assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
-    }
-
-    @Test
-    public void testConnectivity() {
-        Component component = context().getComponent("aws2-kinesis");
-        ComponentVerifierExtension verifier
-                = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
-
-        Map<String, Object> parameters = new HashMap<>();
-        parameters.put("secretKey", "l");
-        parameters.put("accessKey", "k");
-        parameters.put("region", "US_EAST_1");
-        parameters.put("streamName", "test");
-
-        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
-
-        assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
-    }
-
-    @Test
-    public void testConnectivityAndRegion() {
-        Component component = context().getComponent("aws2-kinesis");
-        ComponentVerifierExtension verifier
-                = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
-
-        Map<String, Object> parameters = new HashMap<>();
-        parameters.put("secretKey", "l");
-        parameters.put("accessKey", "k");
-        parameters.put("region", "l");
-        parameters.put("streamName", "test");
-
-        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
-
-        assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
-    }
-
-}