Skip to content

Commit

Permalink
Make HTTP templates for observability work with subresources
Browse files Browse the repository at this point in the history
Fixes: quarkusio#31497
(cherry picked from commit 2cba375)
  • Loading branch information
geoand authored and gsmet committed Mar 15, 2024
1 parent f4a5778 commit 1a5fa15
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ public class ObservabilityCustomizer implements HandlerChainCustomizer {
public List<ServerRestHandler> handlers(Phase phase, ResourceClass resourceClass,
ServerResourceMethod serverResourceMethod) {
if (phase.equals(Phase.AFTER_MATCH)) {
String basePath = resourceClass.getPath();
boolean isSubResource = basePath == null;
ObservabilityHandler observabilityHandler = new ObservabilityHandler();
observabilityHandler
.setTemplatePath(resourceClass.getPath() + serverResourceMethod.getPath());
if (isSubResource) {
observabilityHandler.setTemplatePath(serverResourceMethod.getPath());
observabilityHandler.setSubResource(true);
} else {
observabilityHandler.setTemplatePath(basePath + serverResourceMethod.getPath());
observabilityHandler.setSubResource(false);
}

return Collections.singletonList(observabilityHandler);
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ObservabilityHandler implements ServerRestHandler {
// make mutable to allow for bytecode serialization
private String templatePath;

private boolean isSubResource;

public String getTemplatePath() {
return templatePath;
}
Expand All @@ -24,9 +26,26 @@ public void setTemplatePath(String templatePath) {
this.templatePath = MULTIPLE_SLASH_PATTERN.matcher(templatePath).replaceAll("/");
}

public boolean isSubResource() {
return isSubResource;
}

public void setSubResource(boolean subResource) {
isSubResource = subResource;
}

@Override
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
setUrlPathTemplate(requestContext.unwrap(RoutingContext.class), templatePath);
RoutingContext routingContext = requestContext.unwrap(RoutingContext.class);
if (isSubResource) {
var previous = getUrlPathTemplate(routingContext);
if (previous == null) {
previous = "";
}
setUrlPathTemplate(routingContext, previous + templatePath);
} else {
setUrlPathTemplate(routingContext, templatePath);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.resteasy.reactive.server.runtime.observability;

import io.vertx.core.Context;
import io.vertx.core.http.impl.HttpServerRequestInternal;
import io.vertx.ext.web.RoutingContext;

Expand All @@ -9,7 +10,14 @@ private ObservabilityUtil() {
}

static void setUrlPathTemplate(RoutingContext routingContext, String templatePath) {
((HttpServerRequestInternal) (routingContext.request())).context()
.putLocal("UrlPathTemplate", templatePath);
getRequestContext(routingContext).putLocal("UrlPathTemplate", templatePath);
}

static String getUrlPathTemplate(RoutingContext routingContext) {
return getRequestContext(routingContext).getLocal("UrlPathTemplate");
}

private static Context getRequestContext(RoutingContext routingContext) {
return ((HttpServerRequestInternal) (routingContext.request())).context();
}
}

0 comments on commit 1a5fa15

Please sign in to comment.