Skip to content

Commit

Permalink
Merge pull request #14750 from ebullient/http-server
Browse files Browse the repository at this point in the history
Micrometer: rename vertx options to http-server
  • Loading branch information
ebullient authored Feb 8, 2021
2 parents f4a1bce + 241c636 commit dac8d8b
Show file tree
Hide file tree
Showing 33 changed files with 576 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Set;
import java.util.function.BooleanSupplier;

import javax.inject.Singleton;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
Expand All @@ -23,6 +25,7 @@
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.DotNames;
Expand All @@ -47,7 +50,11 @@
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.MicrometerTimed;
import io.quarkus.micrometer.runtime.MicrometerTimedInterceptor;
import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.config.runtime.HttpClientConfig;
import io.quarkus.micrometer.runtime.config.runtime.HttpServerConfig;
import io.quarkus.micrometer.runtime.config.runtime.VertxConfig;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.metrics.MetricsFactory;

Expand All @@ -72,6 +79,31 @@ public boolean getAsBoolean() {
}
}

public static class HttpBinderEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

public boolean getAsBoolean() {
return mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpServer) ||
mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpClient);
}
}

public static class HttpServerBinderEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

public boolean getAsBoolean() {
return mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpServer);
}
}

public static class HttpClientBinderEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

public boolean getAsBoolean() {
return mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpClient);
}
}

MicrometerConfig mConfig;

@BuildStep(onlyIf = MicrometerEnabled.class)
Expand Down Expand Up @@ -185,6 +217,24 @@ void processAnnotatedMetrics(BuildProducer<AnnotationsTransformerBuildItem> anno
annotationsTransformers.produce(createAnnotationTransformer(TIMED_ANNOTATION, TIMED_BINDING));
}

@BuildStep(onlyIf = { MicrometerEnabled.class })
@Record(ExecutionTime.RUNTIME_INIT)
SyntheticBeanBuildItem enableHttpBinders(MicrometerRecorder recorder,
HttpServerConfig serverConfig,
HttpClientConfig clientConfig,
VertxConfig vertxConfig) {
return SyntheticBeanBuildItem
.configure(HttpBinderConfiguration.class)
.scope(Singleton.class)
.setRuntimeInit()
.unremovable()
.runtimeValue(recorder.configureHttpMetrics(
mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpServer),
mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpClient),
serverConfig, clientConfig, vertxConfig))
.done();
}

@BuildStep(onlyIf = MicrometerEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
RootMeterRegistryBuildItem createRootRegistry(MicrometerRecorder recorder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.micrometer.deployment.MicrometerProcessor;
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;

public class HttpClientProcessor {
public class RestClientProcessor {
// Avoid referencing optional dependencies

// Rest client listener SPI
Expand All @@ -23,15 +24,15 @@ public class HttpClientProcessor {
// Http Client runtime config (injected programmatically)
private static final String REST_CLIENT_HTTP_CONFIG = "io.quarkus.micrometer.runtime.config.runtime.HttpClientConfig";

static class HttpClientEnabled implements BooleanSupplier {
static class RestClientEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

public boolean getAsBoolean() {
return REST_CLIENT_LISTENER_CLASS != null && mConfig.checkBinderEnabledWithDefault(mConfig.binder.httpClient);
}
}

@BuildStep(onlyIf = HttpClientEnabled.class)
@BuildStep(onlyIf = { RestClientEnabled.class, MicrometerProcessor.HttpClientBinderEnabled.class })
UnremovableBeanBuildItem registerRestClientListener(BuildProducer<NativeImageResourceBuildItem> resource,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
resource.produce(new NativeImageResourceBuildItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
import javax.interceptor.Interceptor;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.micrometer.deployment.MicrometerProcessor;
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderRecorder;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterFilter;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.config.runtime.VertxConfig;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.reactive.spi.ContainerRequestFilterBuildItem;
import io.quarkus.resteasy.reactive.spi.CustomContainerRequestFilterBuildItem;
import io.quarkus.vertx.core.deployment.VertxOptionsConsumerBuildItem;
import io.quarkus.vertx.http.deployment.FilterBuildItem;
Expand Down Expand Up @@ -46,10 +48,9 @@ public boolean getAsBoolean() {
static final String RESTEASY_CONTAINER_FILTER_CLASS_NAME = "io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderRestEasyContainerFilter";
static final String QUARKUS_REST_CONTAINER_FILTER_CLASS_NAME = "io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderQuarkusRestContainerFilter";

@BuildStep(onlyIf = { VertxBinderEnabled.class })
@BuildStep(onlyIf = { VertxBinderEnabled.class, MicrometerProcessor.HttpServerBinderEnabled.class })
void enableJaxRsSupport(Capabilities capabilities,
BuildProducer<ResteasyJaxrsProviderBuildItem> resteasyJaxrsProviders,
BuildProducer<ContainerRequestFilterBuildItem> containerRequestFilter,
BuildProducer<CustomContainerRequestFilterBuildItem> customContainerRequestFilter,
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {

Expand Down Expand Up @@ -77,7 +78,7 @@ AdditionalBeanBuildItem createVertxAdapters() {
.setUnremovable().build();
}

@BuildStep(onlyIf = VertxBinderEnabled.class)
@BuildStep(onlyIf = { VertxBinderEnabled.class, MicrometerProcessor.HttpServerBinderEnabled.class })
FilterBuildItem addVertxMeterFilter() {
return new FilterBuildItem(new VertxMeterFilter(), Integer.MAX_VALUE);
}
Expand All @@ -90,7 +91,9 @@ VertxOptionsConsumerBuildItem build(VertxMeterBinderRecorder recorder) {

@BuildStep(onlyIf = VertxBinderEnabled.class)
@Record(value = ExecutionTime.RUNTIME_INIT)
void setVertxConfig(VertxMeterBinderRecorder recorder, VertxConfig config) {
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
void setVertxConfig(VertxMeterBinderRecorder recorder,
VertxConfig config) {
recorder.setVertxConfig(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.quarkus.micrometer.deployment.MicrometerRegistryProviderBuildItem;
import io.quarkus.micrometer.runtime.MicrometerRecorder;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.config.PrometheusConfig;
import io.quarkus.micrometer.runtime.config.PrometheusConfigGroup;
import io.quarkus.micrometer.runtime.export.PrometheusMeterRegistryProvider;
import io.quarkus.micrometer.runtime.export.PrometheusRecorder;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
Expand Down Expand Up @@ -55,7 +55,7 @@ void createPrometheusRoute(BuildProducer<RouteBuildItem> routes,
MicrometerConfig mConfig,
PrometheusRecorder recorder) {

PrometheusConfig pConfig = mConfig.export.prometheus;
PrometheusConfigGroup pConfig = mConfig.export.prometheus;
log.debug("PROMETHEUS CONFIG: " + pConfig);

// Exact match for resources matched to the root path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.quarkus.micrometer.deployment.binder;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter;
import io.quarkus.test.QuarkusUnitTest;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.net.SocketAddress;

public class VertxWithHttpDisabledTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.binder.vertx.enabled", "true")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Inject
Instance<VertxMeterBinderAdapter> vertxMeterBinderAdapterInstance;

@Inject
HttpBinderConfiguration httpBinderConfiguration;

@Test
public void testVertxMetricsWithoutHttp() throws Exception {
Assertions.assertTrue(vertxMeterBinderAdapterInstance.isResolvable());
VertxMeterBinderAdapter adapter = vertxMeterBinderAdapterInstance.get();

// HttpServerMetrics should not be created (null returned) because
// Http server metrics are disabled
Assertions.assertNull(adapter.createHttpServerMetrics(new HttpServerOptions(), new SocketAddress() {
@Override
public String host() {
return "a.b.c";
}

@Override
public int port() {
return 0;
}

@Override
public String path() {
return null;
}
}));

Assertions.assertFalse(httpBinderConfiguration.isServerEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.quarkus.micrometer.deployment.binder;

import java.util.Map;
import java.util.regex.Pattern;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter;
import io.quarkus.test.QuarkusUnitTest;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.HttpServerMetrics;

public class VertxWithHttpEnabledTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.binder.http-server.enabled", "true")
.overrideConfigKey("quarkus.micrometer.binder.http-server.ignore-patterns", "/http")
.overrideConfigKey("quarkus.micrometer.binder.vertx.enabled", "true")
.overrideConfigKey("quarkus.micrometer.binder.vertx.match-patterns", "/one=/two")
.overrideConfigKey("quarkus.micrometer.binder.vertx.ignore-patterns", "/two")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Inject
Instance<VertxMeterBinderAdapter> vertxMeterBinderAdapterInstance;

@Inject
HttpBinderConfiguration httpBinderConfiguration;

@Test
public void testMetricFactoryCreatedMetrics() throws Exception {
Assertions.assertTrue(vertxMeterBinderAdapterInstance.isResolvable());
VertxMeterBinderAdapter adapter = vertxMeterBinderAdapterInstance.get();

HttpServerMetrics metrics = adapter.createHttpServerMetrics(new HttpServerOptions(), new SocketAddress() {
@Override
public String host() {
return "a.b.c";
}

@Override
public int port() {
return 0;
}

@Override
public String path() {
return null;
}
});

Assertions.assertNotNull(metrics);
Assertions.assertTrue(httpBinderConfiguration.isServerEnabled());

// prefer http-server.ignore-patterns
Assertions.assertEquals(1, httpBinderConfiguration.getServerIgnorePatterns().size());
Pattern p = httpBinderConfiguration.getServerIgnorePatterns().get(0);
Assertions.assertTrue(p.matcher("/http").matches());

// Use vertx.match-patterns (http-server version is missing)
Assertions.assertEquals(1, httpBinderConfiguration.getServerMatchPatterns().size());
Map.Entry<Pattern, String> entry = httpBinderConfiguration.getServerMatchPatterns().entrySet().iterator().next();
Assertions.assertTrue(entry.getKey().matcher("/one").matches());
Assertions.assertEquals("/two", entry.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void testMeterRegistryPresent() {
@Test
public void testNoPrometheusEndpoint() {
// Micrometer is enabled, prometheus is not.
RestAssured.when().get("/prometheus").then().statusCode(404);
RestAssured.when().get("/q/metrics").then().statusCode(404);
}
}
4 changes: 4 additions & 0 deletions extensions/micrometer/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.slf4j</groupId>
<artifactId>slf4j-jboss-logmanager</artifactId>
</dependency>

<!-- Needed for the JSON meter registry -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
import io.micrometer.core.instrument.config.MeterFilter;
import io.quarkus.arc.Arc;
import io.quarkus.micrometer.runtime.binder.HttpBinderConfiguration;
import io.quarkus.micrometer.runtime.binder.JVMInfoBinder;
import io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter;
import io.quarkus.micrometer.runtime.config.MicrometerConfig;
import io.quarkus.micrometer.runtime.config.runtime.HttpClientConfig;
import io.quarkus.micrometer.runtime.config.runtime.HttpServerConfig;
import io.quarkus.micrometer.runtime.config.runtime.VertxConfig;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
Expand Down Expand Up @@ -171,4 +175,17 @@ static String getExceptionTag(Throwable throwable) {
}
return throwable.getCause().getClass().getSimpleName();
}

/* RUNTIME_INIT */
public RuntimeValue<HttpBinderConfiguration> configureHttpMetrics(
boolean httpServerMetricsEnabled,
boolean httpClientMetricsEnabled,
HttpServerConfig serverConfig,
HttpClientConfig clientConfig,
VertxConfig vertxConfig) {
return new RuntimeValue<HttpBinderConfiguration>(
new HttpBinderConfiguration(httpServerMetricsEnabled,
httpClientMetricsEnabled,
serverConfig, clientConfig, vertxConfig));
}
}
Loading

0 comments on commit dac8d8b

Please sign in to comment.