Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor health support #249

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-parent</artifactId>
<artifactId>camel-k-main</artifactId>
<version>1.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -45,7 +45,13 @@
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-servlet</artifactId>
<artifactId>camel-k-runtime-inspector</artifactId>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>

<!-- ****************************** -->
Expand All @@ -59,6 +65,16 @@
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-direct</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mock</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -72,12 +88,25 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand All @@ -91,6 +120,12 @@
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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.k.health;

import java.util.Collection;
import java.util.Map;

import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckFilter;
import org.apache.camel.health.HealthCheckHelper;
import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.apache.camel.impl.health.ContextHealthCheck;
import org.apache.camel.impl.health.RoutesHealthCheckRepository;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.inspector.InspectorCustomizer;

public class HealthContextCustomizer implements ContextCustomizer {
public static final String DEFAULT_PATH = "/health";

private String path;
private String healthGroupFilterExclude;
private boolean includeRoutes;
private boolean includeContext;

public HealthContextCustomizer() {
this.path = DEFAULT_PATH;
this.includeRoutes = true;
this.includeContext = true;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getHealthGroupFilterExclude() {
return healthGroupFilterExclude;
}

public void setHealthGroupFilterExclude(String healthGroupFilterExclude) {
this.healthGroupFilterExclude = healthGroupFilterExclude;
}

public boolean isIncludeRoutes() {
return includeRoutes;
}

public void setIncludeRoutes(boolean includeRoutes) {
this.includeRoutes = includeRoutes;
}

public boolean isIncludeContext() {
return includeContext;
}

public void setIncludeContext(boolean includeContext) {
this.includeContext = includeContext;
}

@Override
public int getOrder() {
return Ordered.HIGHEST;
}

@Override
public void apply(CamelContext camelContext) {
try {
HealthCheckRegistry reg = HealthCheckRegistry.get(camelContext);
if (includeRoutes) {
reg.addRepository(new RoutesHealthCheckRepository());
}
if (includeContext) {
ContextHealthCheck contextHealthCheck = new ContextHealthCheck();
contextHealthCheck.getConfiguration().setEnabled(true);

reg.register(contextHealthCheck);
}

camelContext.addService(reg);
} catch (Exception e) {
throw new RuntimeException(e);
}

camelContext.getRegistry().bind(
"health-route",
customizer(camelContext)
);
}

private InspectorCustomizer customizer(CamelContext camelContext) {
return router -> router.route(HttpMethod.GET, path).handler(routingContext -> {
int code = 200;

Collection<HealthCheck.Result> results = HealthCheckHelper.invoke(
camelContext,
(HealthCheckFilter) check -> check.getGroup() != null && check.getGroup().equals(getHealthGroupFilterExclude()));

JsonObject response = new JsonObject();
response.put("status", "UP");

JsonArray checks = new JsonArray();

for (HealthCheck.Result result: results) {
Map<String, Object> details = result.getDetails();
boolean enabled = true;

if (details.containsKey(AbstractHealthCheck.CHECK_ENABLED)) {
enabled = (boolean) details.get(AbstractHealthCheck.CHECK_ENABLED);
}

if (enabled) {
JsonObject check = new JsonObject();
check.put("name", result.getCheck().getId());
check.put("status", result.getState().name());

if (result.getState() == HealthCheck.State.DOWN) {
response.put("status", "DOWN");
code = 503;
}

checks.add(check);
}
}

if (!checks.isEmpty()) {
response.put("checks", checks);
}

routingContext.response()
.putHeader("content-type", "application/json")
.setStatusCode(code)
.end(Json.encodePrettily(response));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.k.health;

import java.net.URL;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.inspector.InspectorContextCustomizer;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.util.ObjectHelper;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;

public class HealthCustomizerTest {

@ParameterizedTest
@ValueSource(strings = { "", "/test", "/test/nested" })
public void testHealthConfigurer(String path) throws Exception {
Runtime runtime = Runtime.on(new DefaultCamelContext());
runtime.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.routeId("my-route")
.to("mock:end");
}
});

HealthContextCustomizer healthCustomizer = new HealthContextCustomizer();
healthCustomizer.apply(runtime.getCamelContext());

InspectorContextCustomizer inspectorCustomizer = new InspectorContextCustomizer();
inspectorCustomizer.setBindPort(AvailablePortFinder.getNextAvailable());

String url;
if (ObjectHelper.isEmpty(path)) {
url = "http://localhost:" + inspectorCustomizer.getBindPort() + HealthContextCustomizer.DEFAULT_PATH;
} else {
inspectorCustomizer.setPath(path);

url = "http://localhost:" + inspectorCustomizer.getBindPort() + path + HealthContextCustomizer.DEFAULT_PATH;
}

inspectorCustomizer.apply(runtime.getCamelContext());

try {
runtime.getCamelContext().start();

when()
.get(new URL(url))
.then()
.statusCode(200)
.body(
"status", equalTo("UP"),
"checks.name", hasItems("context", "route:my-route"),
"checks.status", hasItems("UP", "UP")
);
} finally {
runtime.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
</Appenders>

<Loggers>
<Logger name="io.netty" level="INFO"/>
<Logger name="io.netty.handler.logging" level="DEBUG"/>
<Logger name="io.vertx" level="INFO"/>
<Root level="INFO">
<!--<AppenderRef ref="STDOUT"/>-->
<!-- <AppenderRef ref="STDOUT"/> -->
<AppenderRef ref="NONE"/>
</Root>
</Loggers>
Expand Down
Loading