This is an automated email from the ASF dual-hosted git repository. fmariani pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git
commit 93ea8957f38524b7ddf41f9895b538890651e747 Author: Croway <federico.mariani.1...@gmail.com> AuthorDate: Fri Nov 17 16:12:28 2023 +0100 Utility class and log message --- .../CamelAvailabilityCheckAutoConfiguration.java | 10 ++--- .../boot/actuate/health/CamelProbesHelper.java | 46 ++++++++++++++++++++++ .../CamelLivenessStateHealthIndicator.java | 8 ++-- .../CamelReadinessStateHealthIndicator.java | 20 ++-------- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelAvailabilityCheckAutoConfiguration.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelAvailabilityCheckAutoConfiguration.java index 431bee40525..758904783bc 100644 --- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelAvailabilityCheckAutoConfiguration.java +++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelAvailabilityCheckAutoConfiguration.java @@ -23,7 +23,6 @@ import org.apache.camel.spring.boot.actuate.health.readiness.CamelReadinessState import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.availability.ApplicationAvailability; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,17 +33,16 @@ import org.springframework.context.annotation.Configuration; public class CamelAvailabilityCheckAutoConfiguration { @Bean - @ConditionalOnMissingBean(CamelLivenessStateHealthIndicator.class) - public CamelLivenessStateHealthIndicator camelLivenessStateHealthIndicator(ApplicationAvailability applicationAvailability, - CamelContext camelContext) { + public CamelLivenessStateHealthIndicator camelLivenessStateHealthIndicator( + ApplicationAvailability applicationAvailability, + CamelContext camelContext) { return new CamelLivenessStateHealthIndicator(applicationAvailability, camelContext); } @Bean - @ConditionalOnMissingBean({CamelReadinessStateHealthIndicator.class}) public CamelReadinessStateHealthIndicator camelReadinessStateHealthIndicator( ApplicationAvailability applicationAvailability, CamelContext camelContext) { return new CamelReadinessStateHealthIndicator(applicationAvailability, camelContext); } -} \ No newline at end of file +} diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelProbesHelper.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelProbesHelper.java new file mode 100644 index 00000000000..81da7e4413b --- /dev/null +++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelProbesHelper.java @@ -0,0 +1,46 @@ +/* + * 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.slf4j.Logger; + +import java.util.Collection; + +public final class CamelProbesHelper { + + private CamelProbesHelper() { + } + + public static boolean checkProbeState(Collection<HealthCheck.Result> results, Logger log) { + boolean isUp = true; + for (HealthCheck.Result result : results) { + if (!HealthCheck.State.UP.equals(result.getState())) { + isUp = false; + + log.warn( + "Probe in group '{}', with id '{}' failed with message '{}'", + result.getCheck().getGroup(), + result.getCheck().getId(), + result.getMessage().orElse("")); + result.getError().ifPresent(error -> log.warn(error.getMessage(), error)); + } + } + + return isUp; + } +} diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/liveness/CamelLivenessStateHealthIndicator.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/liveness/CamelLivenessStateHealthIndicator.java index 52c47966826..afb8e1622a5 100644 --- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/liveness/CamelLivenessStateHealthIndicator.java +++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/liveness/CamelLivenessStateHealthIndicator.java @@ -19,7 +19,7 @@ package org.apache.camel.spring.boot.actuate.health.liveness; import org.apache.camel.CamelContext; import org.apache.camel.health.HealthCheck; import org.apache.camel.health.HealthCheckHelper; -import org.apache.camel.spring.boot.actuate.health.readiness.CamelReadinessStateHealthIndicator; +import org.apache.camel.spring.boot.actuate.health.CamelProbesHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.actuate.availability.LivenessStateHealthIndicator; @@ -33,7 +33,7 @@ public class CamelLivenessStateHealthIndicator extends LivenessStateHealthIndica private static final Logger LOG = LoggerFactory.getLogger(CamelLivenessStateHealthIndicator.class); - private CamelContext camelContext; + private final CamelContext camelContext; public CamelLivenessStateHealthIndicator( ApplicationAvailability availability, @@ -47,9 +47,9 @@ public class CamelLivenessStateHealthIndicator extends LivenessStateHealthIndica protected AvailabilityState getState(ApplicationAvailability applicationAvailability) { Collection<HealthCheck.Result> results = HealthCheckHelper.invokeLiveness(camelContext); - boolean isLive = CamelReadinessStateHealthIndicator.checkState(results, LOG); + boolean isLive = CamelProbesHelper.checkProbeState(results, LOG); return isLive ? LivenessState.CORRECT : LivenessState.BROKEN; } -} \ No newline at end of file +} diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/readiness/CamelReadinessStateHealthIndicator.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/readiness/CamelReadinessStateHealthIndicator.java index 8706093dfa5..dfb91b4eb6c 100644 --- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/readiness/CamelReadinessStateHealthIndicator.java +++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/readiness/CamelReadinessStateHealthIndicator.java @@ -19,6 +19,7 @@ package org.apache.camel.spring.boot.actuate.health.readiness; import org.apache.camel.CamelContext; import org.apache.camel.health.HealthCheck; import org.apache.camel.health.HealthCheckHelper; +import org.apache.camel.spring.boot.actuate.health.CamelProbesHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.actuate.availability.ReadinessStateHealthIndicator; @@ -32,7 +33,7 @@ public class CamelReadinessStateHealthIndicator extends ReadinessStateHealthIndi private static final Logger LOG = LoggerFactory.getLogger(CamelReadinessStateHealthIndicator.class); - private CamelContext camelContext; + private final CamelContext camelContext; public CamelReadinessStateHealthIndicator( ApplicationAvailability availability, @@ -46,22 +47,9 @@ public class CamelReadinessStateHealthIndicator extends ReadinessStateHealthIndi protected AvailabilityState getState(ApplicationAvailability applicationAvailability) { Collection<HealthCheck.Result> results = HealthCheckHelper.invokeReadiness(camelContext); - boolean isReady = checkState(results, LOG); + boolean isReady = CamelProbesHelper.checkProbeState(results, LOG); return isReady ? ReadinessState.ACCEPTING_TRAFFIC : ReadinessState.REFUSING_TRAFFIC; } - - public static boolean checkState(Collection<HealthCheck.Result> results, Logger log) { - boolean isUp = true; - for (HealthCheck.Result result : results) { - if (!HealthCheck.State.UP.equals(result.getState())) { - isUp = false; - - result.getError().ifPresent(error -> log.warn(result.getCheck().getId(), error)); - } - } - - return isUp; - } -} \ No newline at end of file +}