Skip to content

Commit

Permalink
Utility class and log message
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway committed Nov 17, 2023
1 parent c0757ef commit e5e8e47
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -47,9 +47,9 @@ public CamelLivenessStateHealthIndicator(
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -46,22 +47,9 @@ public CamelReadinessStateHealthIndicator(
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;
}
}
}

0 comments on commit e5e8e47

Please sign in to comment.