Skip to content

Commit

Permalink
Allow disabling health extension with configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
xstefank committed Aug 8, 2024
1 parent fb0009e commit 71f02d7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

@ConfigRoot(name = "health")
public class HealthBuildTimeConfig {
/**
* Activate or disable this extension. Disabling this extension means that no health related information is exposed.
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Whether extensions published health check should be enabled.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.smallrye.health.deployment;

import java.util.function.BooleanSupplier;

public class SmallRyeHealthActive implements BooleanSupplier {

private final HealthBuildTimeConfig config;

SmallRyeHealthActive(HealthBuildTimeConfig config) {
this.config = config;
}

@Override
public boolean getAsBoolean() {
return config.enabled;
}

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

import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;

public class SmallRyeHealthFeatureProcessor {

@BuildStep
public void defineFeature(BuildProducer<FeatureBuildItem> feature) {
feature.produce(new FeatureBuildItem(Feature.SMALLRYE_HEALTH));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
Expand Down Expand Up @@ -80,6 +79,7 @@
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

@BuildSteps(onlyIf = SmallRyeHealthActive.class)
class SmallRyeHealthProcessor {
private static final Logger LOG = Logger.getLogger(SmallRyeHealthProcessor.class);

Expand Down Expand Up @@ -157,14 +157,11 @@ void healthCheck(BuildProducer<AdditionalBeanBuildItem> buildItemBuildProducer,
@Record(ExecutionTime.STATIC_INIT)
@SuppressWarnings("unchecked")
void build(SmallRyeHealthRecorder recorder,
BuildProducer<FeatureBuildItem> feature,
BuildProducer<ExcludedTypeBuildItem> excludedTypes,
BuildProducer<AdditionalBeanBuildItem> additionalBean,
BuildProducer<BeanDefiningAnnotationBuildItem> beanDefiningAnnotation)
throws IOException, ClassNotFoundException {

feature.produce(new FeatureBuildItem(Feature.SMALLRYE_HEALTH));

// Discover the beans annotated with @Health, @Liveness, @Readiness, @Startup, @HealthGroup,
// @HealthGroups and @Wellness even if no scope is defined
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(LIVENESS, BuiltinScope.SINGLETON.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.smallrye.health.test;

import org.jboss.shrinkwrap.api.asset.EmptyAsset;
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;

class DeactiveHealthWithConfigTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(BasicHealthCheck.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"))
.overrideConfigKey("quarkus.health.enabled", "false");

@Test
void testAdditionalJsonPropertyInclusions() {
try {
RestAssured.defaultParser = Parser.JSON;
RestAssured.when().get("/q/health").then()
.statusCode(404);
} finally {
RestAssured.reset();
}
}

}

0 comments on commit 71f02d7

Please sign in to comment.