Skip to content

Commit

Permalink
CAMEL-17436: Disabling health check for single route or consumer is n…
Browse files Browse the repository at this point in the history
…ot possible (#428)

Co-authored-by: Michał Ostrowski <michal.ostrowski@apdu.pl>
  • Loading branch information
2 people authored and davsclaus committed Jan 5, 2022
1 parent 6fb06a2 commit d3fdf4e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
Expand Up @@ -114,23 +114,23 @@ public HealthIndicator camelHealthCheckIndicator(CamelContext camelContext, Came
CamelHealthCheckConfigurationProperties.HealthCheckConfigurationProperties hcc = config.getConfig().get(id);
String parent = hcc.getParent();
// lookup health check by id
Object hc = hcr.getCheck(parent).orElse(null);
Object hc = hcr.getCheck(id).orElse(null);
if (hc == null) {
hc = hcr.resolveById(parent);
if (hc == null) {
LOG.warn("Cannot resolve HealthCheck with id: " + parent + " from classpath.");
continue;
}
hcr.register(hc);
if (hc instanceof HealthCheck) {
((HealthCheck) hc).getConfiguration().setParent(hcc.getParent());
((HealthCheck) hc).getConfiguration().setEnabled(hcc.getEnabled() != null ? hcc.getEnabled() : true);
((HealthCheck) hc).getConfiguration().setFailureThreshold(hcc.getFailureThreshold());
((HealthCheck) hc).getConfiguration().setInterval(hcc.getInterval());
} else if (hc instanceof HealthCheckRepository) {
((HealthCheckRepository) hc).setEnabled(hcc.getEnabled() != null ? hcc.getEnabled() : true);
((HealthCheckRepository) hc).addConfiguration(id, hcc.toHealthCheckConfiguration());
}
}

if (hc instanceof HealthCheck) {
((HealthCheck) hc).getConfiguration().setParent(hcc.getParent());
((HealthCheck) hc).getConfiguration().setEnabled(hcc.getEnabled() != null ? hcc.getEnabled() : true);
((HealthCheck) hc).getConfiguration().setFailureThreshold(hcc.getFailureThreshold());
((HealthCheck) hc).getConfiguration().setInterval(hcc.getInterval());
} else if (hc instanceof HealthCheckRepository) {
((HealthCheckRepository) hc).addConfiguration(id, hcc.toHealthCheckConfiguration());
}
}

Expand Down
@@ -0,0 +1,76 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;

import org.apache.camel.CamelContext;
import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

@CamelSpringBootTest
@EnableAutoConfiguration
@SpringBootApplication
@SpringBootTest(
classes = {CamelAutoConfiguration.class, CamelHealthCheckAutoConfiguration.class, DownRoute.class, MyCamelRoute.class},
properties = {"camel.health.config[consumer\\:down-route].parent=consumers",
"camel.health.config[consumer\\:down-route].enabled=false"})
class CamelHealthCheckConfigurationTest {

@Autowired
CamelHealthCheckIndicator indicator;

@Autowired
CamelContext camelContext;

@Test
void shouldBeHealth() throws Exception {
// 'down-route' is DOWN, but health check for this consumer should be disabled by configuration
final Health health = indicator.health();
assertThat(health)
.as("Has health")
.isNotNull()
.as("Should be UP")
.matches(h -> h.getStatus() == Status.UP);
}

@Test
void shouldNotDisableAllConsumersHealthChecks() {
@SuppressWarnings("resource")
final HealthCheckRegistry registry = camelContext.getExtension(HealthCheckRegistry.class);

assertThat(registry.getCheck("consumer:down-route"))
.as("'down-route' health check is disabled")
.isPresent()
.get()
.matches(hc -> !hc.getConfiguration().isEnabled());

assertThat(registry.getCheck("consumer:route1"))
.as("other route health check is enabled")
.isPresent()
.get()
.matches(hc -> hc.getConfiguration().isEnabled());
}
}
@@ -0,0 +1,29 @@
/*
* 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.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class DownRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
from("scheduler://foo?initialDelay=60000").routeId("down-route").to("log:bar");
}
}

0 comments on commit d3fdf4e

Please sign in to comment.