-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from lburgazzoli/health-refactor
Refactor health support
- Loading branch information
Showing
64 changed files
with
790 additions
and
721 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...mel-k-runtime-health/src/main/java/org/apache/camel/k/health/HealthContextCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} | ||
} |
File renamed without changes.
82 changes: 82 additions & 0 deletions
82
.../camel-k-runtime-health/src/test/java/org/apache/camel/k/health/HealthCustomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.