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

Migrate Metrics extension to a vert.x route #4174

Merged
merged 1 commit into from
Sep 25, 2019
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
2 changes: 1 addition & 1 deletion extensions/smallrye-metrics/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
<artifactId>quarkus-vertx-http-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import javax.enterprise.context.Dependent;

Expand All @@ -31,6 +32,7 @@
import io.quarkus.arc.deployment.AutoInjectAnnotationBuildItem;
import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -45,8 +47,8 @@
import io.quarkus.smallrye.metrics.deployment.jandex.JandexBeanInfoAdapter;
import io.quarkus.smallrye.metrics.deployment.jandex.JandexMemberInfoAdapter;
import io.quarkus.smallrye.metrics.runtime.SmallRyeMetricsRecorder;
import io.quarkus.smallrye.metrics.runtime.SmallRyeMetricsServlet;
import io.quarkus.undertow.deployment.ServletBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HandlerType;
import io.smallrye.metrics.MetricProducer;
import io.smallrye.metrics.MetricRegistries;
import io.smallrye.metrics.MetricsRequestHandler;
Expand All @@ -57,6 +59,8 @@
import io.smallrye.metrics.interceptors.MetricNameFactory;
import io.smallrye.metrics.interceptors.MetricsInterceptor;
import io.smallrye.metrics.interceptors.TimedInterceptor;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

public class SmallRyeMetricsProcessor {
private static final Logger LOGGER = Logger.getLogger("io.quarkus.smallrye.metrics.deployment.SmallRyeMetricsProcessor");
Expand All @@ -65,7 +69,7 @@ public class SmallRyeMetricsProcessor {
static final class SmallRyeMetricsConfig {

/**
* The path to the metrics Servlet.
* The path to the metrics handler.
*/
@ConfigItem(defaultValue = "/metrics")
String path;
Expand All @@ -74,16 +78,16 @@ static final class SmallRyeMetricsConfig {
SmallRyeMetricsConfig metrics;

@BuildStep
ServletBuildItem createServlet() {
ServletBuildItem servletBuildItem = ServletBuildItem.builder("metrics", SmallRyeMetricsServlet.class.getName())
.addMapping(metrics.path + (metrics.path.endsWith("/") ? "*" : "/*"))
.addInitParam("metrics.path", metrics.path)
.build();
return servletBuildItem;
@Record(STATIC_INIT)
void createRoute(BuildProducer<RouteBuildItem> routes,
SmallRyeMetricsRecorder recorder) {
Function<Router, Route> route = recorder.route(metrics.path + (metrics.path.endsWith("/") ? "*" : "/*"));
routes.produce(new RouteBuildItem(route, recorder.handler(metrics.path), HandlerType.BLOCKING));
}

@BuildStep
void beans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
void beans(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(MetricProducer.class,
MetricNameFactory.class,
MetricRegistries.class,
Expand All @@ -92,8 +96,9 @@ void beans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
ConcurrentGaugeInterceptor.class,
CountedInterceptor.class,
TimedInterceptor.class,
MetricsRequestHandler.class,
SmallRyeMetricsServlet.class));
MetricsRequestHandler.class));
unremovableBeans.produce(new UnremovableBeanBuildItem(
new UnremovableBeanBuildItem.BeanClassNameExclusion(MetricsRequestHandler.class.getName())));
}

@BuildStep
Expand Down
6 changes: 1 addition & 5 deletions extensions/smallrye-metrics/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
<artifactId>quarkus-vertx-http</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -35,10 +35,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.quarkus.smallrye.metrics.runtime;

import java.io.IOException;
import java.util.stream.Stream;

import javax.enterprise.inject.spi.CDI;

import org.jboss.logging.Logger;

import io.smallrye.metrics.MetricsRequestHandler;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

public class SmallRyeMetricsHandler implements Handler<RoutingContext> {

private String metricsPath;

private static final Logger LOGGER = Logger.getLogger(SmallRyeMetricsHandler.class.getName());

public void setMetricsPath(String metricsPath) {
this.metricsPath = metricsPath;
}

@Override
public void handle(RoutingContext routingContext) {
MetricsRequestHandler internalHandler = CDI.current().select(MetricsRequestHandler.class).get();
HttpServerResponse response = routingContext.response();
HttpServerRequest request = routingContext.request();
Stream<String> acceptHeaders = request.headers().getAll("Accept").stream();

try {
internalHandler.handleRequest(request.path(), metricsPath, request.rawMethod(), acceptHeaders,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the logic inside the internalHandler.handleRequest() non-blocking? If so then OK, otherwise you should use HandlerType#BLOCKING instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I added HandlerType#BLOCKING

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safer to use "blocking" for now. While it could be non-blocking, it the user declare a metric where the computation is blocking that could have a terrible impact.

(status, message, headers) -> {
response.setStatusCode(status);
headers.forEach(response::putHeader);
response.end(Buffer.buffer(message));
});
} catch (IOException e) {
response.setStatusCode(503);
response.end();
LOGGER.error(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
Expand All @@ -27,6 +28,8 @@
import io.smallrye.metrics.elementdesc.MemberInfo;
import io.smallrye.metrics.interceptors.MetricResolver;
import io.smallrye.metrics.setup.MetricsMetadata;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

@Recorder
public class SmallRyeMetricsRecorder {
Expand Down Expand Up @@ -63,6 +66,21 @@ public class SmallRyeMetricsRecorder {
private static final String MEMORY_USED_HEAP = "memory.usedHeap";
private static final String MEMORY_USED_NON_HEAP = "memory.usedNonHeap";

public Function<Router, Route> route(String name) {
return new Function<Router, Route>() {
@Override
public Route apply(Router router) {
return router.route(name);
}
};
}

public SmallRyeMetricsHandler handler(String metricsPath) {
SmallRyeMetricsHandler handler = new SmallRyeMetricsHandler();
handler.setMetricsPath(metricsPath);
return handler;
}

public void registerVendorMetrics(ShutdownContext shutdown) {
MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.VENDOR);
List<String> names = new ArrayList<>();
Expand Down

This file was deleted.