Skip to content

Commit

Permalink
Fix on RestPathAnnotationProcessor to ensure that the correct url tem…
Browse files Browse the repository at this point in the history
…plate is computed for metrics
  • Loading branch information
souchel committed Mar 24, 2023
1 parent 34accfc commit cfae234
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.micrometer.deployment.binder;

import static io.restassured.RestAssured.when;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.micrometer.core.instrument.MeterRegistry;
import io.quarkus.micrometer.test.Util;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.restassured.RestAssured;
import io.vertx.core.http.impl.HttpServerRequestInternal;

class UriTagWithInterfaceResourceTest {
@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.vertx.enabled", "true")
.overrideConfigKey("quarkus.redis.devservices.enabled", "false")
.withApplicationRoot((jar) -> jar
.addClasses(Util.class,
InterfaceResourceReturningUrlTemplate.class,
InterfaceResourceReturningUrlTemplateImpl.class));

@Inject
MeterRegistry registry;

@Test
void testRequestUris() throws Exception {
RestAssured.basePath = "/";

when().get("/class/path/method/path/test").then()
.statusCode(200)
.body(Matchers.is("/class/path/method/path/{param}"));

Util.waitForMeters(registry.find("http.server.requests").timers(), 1);

System.out.println("Server paths\n" + Util.listMeters(registry, "http.server.requests"));

Assertions.assertEquals(1,
registry.find("http.server.requests").tag("uri", "/class/path/method/path/{param}").timers().size(),
Util.foundServerRequests(registry, "Servlet path (/servlet) should be returned by JAX-RS."));
}

@Path("/class/path")
public interface InterfaceResourceReturningUrlTemplate {

@Path("method/path/{param}")
@GET
@Produces(MediaType.TEXT_PLAIN)
String getTemplatizedPath(@PathParam("param") String param);

}

public static class InterfaceResourceReturningUrlTemplateImpl implements InterfaceResourceReturningUrlTemplate {

private final CurrentVertxRequest request;

public InterfaceResourceReturningUrlTemplateImpl(CurrentVertxRequest request) {
this.request = request;
}

@Override
public String getTemplatizedPath(String param) {
return ((HttpServerRequestInternal) request.getCurrent().request()).context().getLocal("UrlPathTemplate");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public void transform(TransformationContext ctx) {
} else {
// Fallback: look for @Path on interfaces
getAllClassInterfaces(index, List.of(classInfo), new ArrayList<>()).stream()
.filter(interfaceClassInfo -> interfaceClassInfo.hasAnnotation(REST_PATH))
.filter(interfaceClassInfo -> interfaceClassInfo.hasDeclaredAnnotation(REST_PATH))
.findFirst()
.map(interfaceClassInfo -> interfaceClassInfo.annotation(REST_PATH).value())
.map(interfaceClassInfo -> interfaceClassInfo.declaredAnnotation(REST_PATH).value())
.ifPresent(annotationValue -> stringBuilder.insert(0, slashify(annotationValue.asString())));
}

Expand Down

0 comments on commit cfae234

Please sign in to comment.