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 2023/07/28 08:22:29 UTC

[camel] 01/13: CAMEL-19645: camel-aws - producer health check

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

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

commit 8284ce4aae976f940fc00241e6be213b21d9b39c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jul 28 09:33:01 2023 +0200

    CAMEL-19645: camel-aws - producer health check
---
 .../aws2/ec2/AWS2EC2ComponentConfigurer.java       | 18 ++++++++++++++
 .../apache/camel/component/aws2/ec2/aws2-ec2.json  |  5 +++-
 .../camel/component/aws2/ec2/AWS2EC2Component.java |  5 ++--
 .../camel/component/aws2/ec2/AWS2EC2Endpoint.java  | 22 ++++------------
 .../camel/component/aws2/ec2/AWS2EC2Producer.java  | 29 ++++++++++++++++++++++
 ...hCheck.java => AWS2EC2ProducerHealthCheck.java} |  6 ++---
 ...WS2EC2ProducerHealthCheckProfileCredsTest.java} | 10 ++------
 ...AWS2EC2ProducerHealthCheckStaticCredsTest.java} | 10 ++------
 8 files changed, 66 insertions(+), 39 deletions(-)

diff --git a/components/camel-aws/camel-aws2-ec2/src/generated/java/org/apache/camel/component/aws2/ec2/AWS2EC2ComponentConfigurer.java b/components/camel-aws/camel-aws2-ec2/src/generated/java/org/apache/camel/component/aws2/ec2/AWS2EC2ComponentConfigurer.java
index d650ea33c2f..8d6f97f0e79 100644
--- a/components/camel-aws/camel-aws2-ec2/src/generated/java/org/apache/camel/component/aws2/ec2/AWS2EC2ComponentConfigurer.java
+++ b/components/camel-aws/camel-aws2-ec2/src/generated/java/org/apache/camel/component/aws2/ec2/AWS2EC2ComponentConfigurer.java
@@ -35,6 +35,12 @@ public class AWS2EC2ComponentConfigurer extends PropertyConfigurerSupport implem
         case "autowiredenabled":
         case "autowiredEnabled": target.setAutowiredEnabled(property(camelContext, boolean.class, value)); return true;
         case "configuration": target.setConfiguration(property(camelContext, org.apache.camel.component.aws2.ec2.AWS2EC2Configuration.class, value)); return true;
+        case "healthcheckconsumerenabled":
+        case "healthCheckConsumerEnabled": target.setHealthCheckConsumerEnabled(property(camelContext, boolean.class, value)); return true;
+        case "healthcheckenabled":
+        case "healthCheckEnabled": target.setHealthCheckEnabled(property(camelContext, boolean.class, value)); return true;
+        case "healthcheckproducerenabled":
+        case "healthCheckProducerEnabled": target.setHealthCheckProducerEnabled(property(camelContext, boolean.class, value)); return true;
         case "lazystartproducer":
         case "lazyStartProducer": target.setLazyStartProducer(property(camelContext, boolean.class, value)); return true;
         case "operation": getOrCreateConfiguration(target).setOperation(property(camelContext, org.apache.camel.component.aws2.ec2.AWS2EC2Operations.class, value)); return true;
@@ -80,6 +86,12 @@ public class AWS2EC2ComponentConfigurer extends PropertyConfigurerSupport implem
         case "autowiredenabled":
         case "autowiredEnabled": return boolean.class;
         case "configuration": return org.apache.camel.component.aws2.ec2.AWS2EC2Configuration.class;
+        case "healthcheckconsumerenabled":
+        case "healthCheckConsumerEnabled": return boolean.class;
+        case "healthcheckenabled":
+        case "healthCheckEnabled": return boolean.class;
+        case "healthcheckproducerenabled":
+        case "healthCheckProducerEnabled": return boolean.class;
         case "lazystartproducer":
         case "lazyStartProducer": return boolean.class;
         case "operation": return org.apache.camel.component.aws2.ec2.AWS2EC2Operations.class;
@@ -121,6 +133,12 @@ public class AWS2EC2ComponentConfigurer extends PropertyConfigurerSupport implem
         case "autowiredenabled":
         case "autowiredEnabled": return target.isAutowiredEnabled();
         case "configuration": return target.getConfiguration();
+        case "healthcheckconsumerenabled":
+        case "healthCheckConsumerEnabled": return target.isHealthCheckConsumerEnabled();
+        case "healthcheckenabled":
+        case "healthCheckEnabled": return target.isHealthCheckEnabled();
+        case "healthcheckproducerenabled":
+        case "healthCheckProducerEnabled": return target.isHealthCheckProducerEnabled();
         case "lazystartproducer":
         case "lazyStartProducer": return target.isLazyStartProducer();
         case "operation": return getOrCreateConfiguration(target).getOperation();
diff --git a/components/camel-aws/camel-aws2-ec2/src/generated/resources/org/apache/camel/component/aws2/ec2/aws2-ec2.json b/components/camel-aws/camel-aws2-ec2/src/generated/resources/org/apache/camel/component/aws2/ec2/aws2-ec2.json
index d85692988d8..9e4ed24b199 100644
--- a/components/camel-aws/camel-aws2-ec2/src/generated/resources/org/apache/camel/component/aws2/ec2/aws2-ec2.json
+++ b/components/camel-aws/camel-aws2-ec2/src/generated/resources/org/apache/camel/component/aws2/ec2/aws2-ec2.json
@@ -39,7 +39,10 @@
     "uriEndpointOverride": { "index": 14, "kind": "property", "displayName": "Uri Endpoint Override", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.aws2.ec2.AWS2EC2Configuration", "configurationField": "configuration", "description": "Set the overriding uri endpoint. This option needs to be used in combination with overrideEn [...]
     "useDefaultCredentialsProvider": { "index": 15, "kind": "property", "displayName": "Use Default Credentials Provider", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.aws2.ec2.AWS2EC2Configuration", "configurationField": "configuration", "description": "Set whether the EC2 client should expect to load credent [...]
     "useProfileCredentialsProvider": { "index": 16, "kind": "property", "displayName": "Use Profile Credentials Provider", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.aws2.ec2.AWS2EC2Configuration", "configurationField": "configuration", "description": "Set whether the EC2 client should expect to load credent [...]
-    "autowiredEnabled": { "index": 17, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching  [...]
+    "autowiredEnabled": { "index": 17, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching  [...]
+    "healthCheckConsumerEnabled": { "index": 18, "kind": "property", "displayName": "Health Check Consumer Enabled", "group": "health", "label": "health", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Used for enabling or disabling all consumer based health checks from this component" },
+    "healthCheckEnabled": { "index": 19, "kind": "property", "displayName": "Health Check Enabled", "group": "health", "label": "health", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Used for enabling or disabling all health checks from this component" },
+    "healthCheckProducerEnabled": { "index": 20, "kind": "property", "displayName": "Health Check Producer Enabled", "group": "health", "label": "health", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Used for enabling or disabling all producer based health checks from this component" }
   },
   "headers": {
     "CamelAwsEC2ImageId": { "index": 0, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "An image ID of the AWS marketplace", "constantName": "org.apache.camel.component.aws2.ec2.AWS2EC2Constants#IMAGE_ID" },
diff --git a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Component.java b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Component.java
index 70e1937fb67..1bff721da4d 100644
--- a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Component.java
+++ b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Component.java
@@ -22,13 +22,14 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
-import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.HealthCheckComponent;
 
 /**
  * For working with Amazon's Elastic Compute Cloud (EC2) SDK v2.
  */
 @Component("aws2-ec2")
-public class AWS2EC2Component extends DefaultComponent {
+public class AWS2EC2Component extends HealthCheckComponent {
+
     @Metadata
     private AWS2EC2Configuration configuration = new AWS2EC2Configuration();
 
diff --git a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Endpoint.java b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Endpoint.java
index db01be8f392..f667171d164 100644
--- a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Endpoint.java
+++ b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Endpoint.java
@@ -22,8 +22,6 @@ import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.aws2.ec2.client.AWS2EC2ClientFactory;
-import org.apache.camel.health.HealthCheckHelper;
-import org.apache.camel.impl.health.ComponentsHealthCheckRepository;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.support.DefaultEndpoint;
@@ -39,8 +37,6 @@ import software.amazon.awssdk.services.ec2.Ec2Client;
 public class AWS2EC2Endpoint extends DefaultEndpoint {
 
     private Ec2Client ec2Client;
-    private ComponentsHealthCheckRepository healthCheckRepository;
-    private AWS2EC2HealthCheck clientHealthCheck;
 
     @UriParam
     private AWS2EC2Configuration configuration;
@@ -66,15 +62,6 @@ public class AWS2EC2Endpoint extends DefaultEndpoint {
 
         ec2Client = configuration.getAmazonEc2Client() != null
                 ? configuration.getAmazonEc2Client() : AWS2EC2ClientFactory.getEc2Client(configuration).getEc2Client();
-
-        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getCamelContext(),
-                ComponentsHealthCheckRepository.REPOSITORY_ID, ComponentsHealthCheckRepository.class);
-
-        if (healthCheckRepository != null) {
-            // Do not register the health check until we resolve CAMEL-18992
-            //clientHealthCheck = new AWS2EC2HealthCheck(this, getId());
-            //healthCheckRepository.addHealthCheck(clientHealthCheck);
-        }
     }
 
     @Override
@@ -85,10 +72,6 @@ public class AWS2EC2Endpoint extends DefaultEndpoint {
             }
         }
 
-        if (healthCheckRepository != null && clientHealthCheck != null) {
-            healthCheckRepository.removeHealthCheck(clientHealthCheck);
-            clientHealthCheck = null;
-        }
         super.doStop();
     }
 
@@ -99,4 +82,9 @@ public class AWS2EC2Endpoint extends DefaultEndpoint {
     public Ec2Client getEc2Client() {
         return ec2Client;
     }
+
+    @Override
+    public AWS2EC2Component getComponent() {
+        return (AWS2EC2Component) super.getComponent();
+    }
 }
diff --git a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
index 194f3ae0b85..6eca5006d59 100644
--- a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
+++ b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
@@ -23,6 +23,8 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Message;
+import org.apache.camel.health.HealthCheckHelper;
+import org.apache.camel.health.WritableHealthCheckRepository;
 import org.apache.camel.support.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
@@ -63,6 +65,8 @@ public class AWS2EC2Producer extends DefaultProducer {
 
     private static final Logger LOG = LoggerFactory.getLogger(AWS2EC2Producer.class);
 
+    private AWS2EC2ProducerHealthCheck producerHealthCheck;
+    private WritableHealthCheckRepository healthCheckRepository;
     private transient String ec2ProducerToString;
 
     public AWS2EC2Producer(Endpoint endpoint) {
@@ -642,4 +646,29 @@ public class AWS2EC2Producer extends DefaultProducer {
     public static Message getMessageForResponse(final Exchange exchange) {
         return exchange.getMessage();
     }
+
+    @Override
+    protected void doStart() throws Exception {
+        // health-check is optional so discover and resolve
+        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(
+                getEndpoint().getCamelContext(),
+                "components",
+                WritableHealthCheckRepository.class);
+
+        if (healthCheckRepository != null) {
+            String id = getEndpoint().getId();
+            producerHealthCheck = new AWS2EC2ProducerHealthCheck(getEndpoint(), id);
+            producerHealthCheck.setEnabled(getEndpoint().getComponent().isHealthCheckEnabled()
+                    && getEndpoint().getComponent().isHealthCheckProducerEnabled());
+            healthCheckRepository.addHealthCheck(producerHealthCheck);
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (healthCheckRepository != null && producerHealthCheck != null) {
+            healthCheckRepository.removeHealthCheck(producerHealthCheck);
+            producerHealthCheck = null;
+        }
+    }
 }
diff --git a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheck.java b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheck.java
similarity index 92%
rename from components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheck.java
rename to components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheck.java
index e4e1d463c6a..ea078fc9ca1 100644
--- a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheck.java
+++ b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheck.java
@@ -26,12 +26,12 @@ import software.amazon.awssdk.awscore.exception.AwsServiceException;
 import software.amazon.awssdk.regions.Region;
 import software.amazon.awssdk.services.ec2.Ec2Client;
 
-public class AWS2EC2HealthCheck extends AbstractHealthCheck {
+public class AWS2EC2ProducerHealthCheck extends AbstractHealthCheck {
 
     private final AWS2EC2Endpoint aws2EC2Endpoint;
 
-    public AWS2EC2HealthCheck(AWS2EC2Endpoint aws2EC2Endpoint, String clientId) {
-        super("camel", "aws2-ec2-client-" + clientId);
+    public AWS2EC2ProducerHealthCheck(AWS2EC2Endpoint aws2EC2Endpoint, String clientId) {
+        super("camel", "aws2-ec2-producer-" + clientId);
         this.aws2EC2Endpoint = aws2EC2Endpoint;
     }
 
diff --git a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckProfileCredsTest.java b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckProfileCredsTest.java
similarity index 90%
rename from components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckProfileCredsTest.java
rename to components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckProfileCredsTest.java
index c72f79e20fc..49ffb4ce094 100644
--- a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckProfileCredsTest.java
+++ b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckProfileCredsTest.java
@@ -29,14 +29,10 @@ import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
 
-public class AWS2EC2HealthCheckProfileCredsTest extends CamelTestSupport {
-
-    private static final Logger LOG = LoggerFactory.getLogger(AWS2EC2HealthCheckProfileCredsTest.class);
+public class AWS2EC2ProducerHealthCheckProfileCredsTest extends CamelTestSupport {
 
     CamelContext context;
 
@@ -84,9 +80,7 @@ public class AWS2EC2HealthCheckProfileCredsTest extends CamelTestSupport {
             Collection<HealthCheck.Result> res2 = HealthCheckHelper.invokeReadiness(context);
             boolean down = res2.stream().allMatch(r -> r.getState().equals(HealthCheck.State.DOWN));
             boolean containsAws2AthenaHealthCheck = res2.stream()
-                    .filter(result -> result.getCheck().getId().startsWith("aws2-ec2-client"))
-                    .findAny()
-                    .isPresent();
+                    .anyMatch(result -> result.getCheck().getId().startsWith("aws2-ec2-producer"));
             boolean hasRegionMessage = res2.stream()
                     .anyMatch(r -> r.getMessage().stream().anyMatch(msg -> msg.contains("region")));
             Assertions.assertTrue(down, "liveness check");
diff --git a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckStaticCredsTest.java b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckStaticCredsTest.java
similarity index 90%
rename from components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckStaticCredsTest.java
rename to components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckStaticCredsTest.java
index ad815bc8d60..304247663de 100644
--- a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2HealthCheckStaticCredsTest.java
+++ b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/AWS2EC2ProducerHealthCheckStaticCredsTest.java
@@ -29,14 +29,10 @@ import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
 
-public class AWS2EC2HealthCheckStaticCredsTest extends CamelTestSupport {
-
-    private static final Logger LOG = LoggerFactory.getLogger(AWS2EC2HealthCheckStaticCredsTest.class);
+public class AWS2EC2ProducerHealthCheckStaticCredsTest extends CamelTestSupport {
 
     CamelContext context;
 
@@ -84,9 +80,7 @@ public class AWS2EC2HealthCheckStaticCredsTest extends CamelTestSupport {
             Collection<HealthCheck.Result> res2 = HealthCheckHelper.invokeReadiness(context);
             boolean down = res2.stream().allMatch(r -> r.getState().equals(HealthCheck.State.DOWN));
             boolean containsAws2AthenaHealthCheck = res2.stream()
-                    .filter(result -> result.getCheck().getId().startsWith("aws2-ec2-client"))
-                    .findAny()
-                    .isPresent();
+                    .anyMatch(result -> result.getCheck().getId().startsWith("aws2-ec2-producer"));
             boolean hasRegionMessage = res2.stream()
                     .anyMatch(r -> r.getMessage().stream().anyMatch(msg -> msg.contains("region")));
             Assertions.assertTrue(down, "liveness check");