Skip to content

Commit

Permalink
Merge pull request #12073 from xstefank/well-endpoint
Browse files Browse the repository at this point in the history
Add wellness health handling
  • Loading branch information
jmartisk authored Sep 15, 2020
2 parents 82a9291 + 40d7fe2 commit b9582fa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class SmallRyeHealthConfig {
@ConfigItem(defaultValue = "/group")
String groupPath;

/**
* The relative path of the wellness health-checking endpoint.
*/
@ConfigItem(defaultValue = "/well")
String wellnessPath;

/**
* UI configuration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import io.quarkus.smallrye.health.runtime.SmallRyeIndividualHealthGroupHandler;
import io.quarkus.smallrye.health.runtime.SmallRyeLivenessHandler;
import io.quarkus.smallrye.health.runtime.SmallRyeReadinessHandler;
import io.quarkus.smallrye.health.runtime.SmallRyeWellnessHandler;
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem;
Expand All @@ -78,6 +79,7 @@
import io.smallrye.health.SmallRyeHealthReporter;
import io.smallrye.health.api.HealthGroup;
import io.smallrye.health.api.HealthGroups;
import io.smallrye.health.api.Wellness;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

Expand All @@ -89,6 +91,7 @@ class SmallRyeHealthProcessor {
private static final DotName READINESS = DotName.createSimple(Readiness.class.getName());
private static final DotName HEALTH_GROUP = DotName.createSimple(HealthGroup.class.getName());
private static final DotName HEALTH_GROUPS = DotName.createSimple(HealthGroups.class.getName());
private static final DotName WELLNESS = DotName.createSimple(Wellness.class.getName());
private static final DotName JAX_RS_PATH = DotName.createSimple("javax.ws.rs.Path");

// For the UI
Expand Down Expand Up @@ -141,14 +144,17 @@ void build(SmallRyeHealthRecorder recorder,
displayableEndpoints
.produce(new NotFoundPageDisplayableEndpointBuildItem(health.rootPath + health.readinessPath));
displayableEndpoints.produce(new NotFoundPageDisplayableEndpointBuildItem(health.rootPath + health.groupPath));
displayableEndpoints.produce(new NotFoundPageDisplayableEndpointBuildItem(health.rootPath + health.wellnessPath));
}

// Discover the beans annotated with @Health, @Liveness, @Readiness, @HealthGroup and @HealthGroups even if no scope is defined
// Discover the beans annotated with @Health, @Liveness, @Readiness, @HealthGroup,
// @HealthGroups and @Wellness even if no scope is defined
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(HEALTH));
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(LIVENESS));
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(READINESS));
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(HEALTH_GROUP));
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(HEALTH_GROUPS));
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(WELLNESS));

// Add additional beans
additionalBean.produce(new AdditionalBeanBuildItem(SmallRyeHealthReporter.class));
Expand Down Expand Up @@ -182,6 +188,7 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
warnIfJaxRsPathUsed(index, LIVENESS);
warnIfJaxRsPathUsed(index, READINESS);
warnIfJaxRsPathUsed(index, HEALTH);
warnIfJaxRsPathUsed(index, WELLNESS);

// Register the health handler
routes.produce(new RouteBuildItem(health.rootPath, new SmallRyeHealthHandler(), HandlerType.BLOCKING));
Expand Down Expand Up @@ -220,6 +227,12 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
new RouteBuildItem(health.rootPath + health.groupPath + "/" + healthGroup,
handler, HandlerType.BLOCKING));
}

// Register the wellness handler
routes.produce(
new RouteBuildItem(health.rootPath + health.wellnessPath, new SmallRyeWellnessHandler(),
HandlerType.BLOCKING));

}

private void warnIfJaxRsPathUsed(IndexView index, DotName healthAnnotation) {
Expand Down Expand Up @@ -277,6 +290,7 @@ AnnotationsTransformerBuildItem annotationTransformer(BeanArchiveIndexBuildItem
healthAnnotations.add(READINESS);
healthAnnotations.add(HEALTH_GROUP);
healthAnnotations.add(HEALTH_GROUPS);
healthAnnotations.add(WELLNESS);

return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.smallrye.health.test;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import io.smallrye.health.api.Wellness;

public class WellnessHealthCheckTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(WellnessHC.class));

@Test
public void testWellness() {
try {
RestAssured.defaultParser = Parser.JSON;
when().get("/health/well").then()
.body("status", is("UP"),
"checks.status", contains("UP"),
"checks.name", contains(WellnessHC.class.getName()));
} finally {
RestAssured.reset();
}
}

@Wellness
static class WellnessHC implements HealthCheck {

@Override
public HealthCheckResponse call() {
return HealthCheckResponse.up(WellnessHC.class.getName());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.quarkus.smallrye.health.runtime;

import io.smallrye.health.SmallRyeHealth;
import io.smallrye.health.SmallRyeHealthReporter;
import io.vertx.ext.web.RoutingContext;

public class SmallRyeWellnessHandler extends SmallRyeHealthHandlerBase {

@Override
protected SmallRyeHealth getHealth(SmallRyeHealthReporter reporter, RoutingContext routingContext) {
return reporter.getWellness();
}
}

0 comments on commit b9582fa

Please sign in to comment.