From c0801856c9ee58ada347b89a6854f20d7c5ac668 Mon Sep 17 00:00:00 2001 From: Croway Date: Fri, 17 Nov 2023 16:12:28 +0100 Subject: [PATCH] Utility class and log message --- ...melAvailabilityCheckAutoConfiguration.java | 10 ++-- .../actuate/health/CamelProbesHelper.java | 46 +++++++++++++++++++ .../CamelLivenessStateHealthIndicator.java | 6 +-- .../CamelReadinessStateHealthIndicator.java | 18 ++------ 4 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelProbesHelper.java 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 431bee40525c..758904783bc7 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.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 @@ 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 000000000000..81da7e4413bd --- /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 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 52c479668265..7154245b1bd1 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 @@ 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; @@ -47,9 +47,9 @@ public CamelLivenessStateHealthIndicator( protected AvailabilityState getState(ApplicationAvailability applicationAvailability) { Collection 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 8706093dfa51..e0e6da772b03 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 @@ 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; @@ -46,22 +47,9 @@ public CamelReadinessStateHealthIndicator( protected AvailabilityState getState(ApplicationAvailability applicationAvailability) { Collection 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 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 +}