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

Build route name correctly #251

Merged
merged 1 commit into from
Jul 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ public String getTarget(final ContainerRequestContext request) {
public String getRoute(final ContainerRequestContext request) {
try {
// This can throw an IllegalArgumentException when determining the route for a subresource
Class<?> resourceClass = (Class<?>) request.getProperty("rest.resource.class");
Class<?> resource = (Class<?>) request.getProperty("rest.resource.class");
Method method = (Method) request.getProperty("rest.resource.method");

UriBuilder template = UriBuilder.fromResource(resourceClass);
UriBuilder uriBuilder = UriBuilder.newInstance();
String contextRoot = request.getUriInfo().getBaseUri().getPath();
if (contextRoot != null) {
template.path(contextRoot);
uriBuilder.path(contextRoot);
}

uriBuilder.path(resource);
if (method.isAnnotationPresent(Path.class)) {
template.path(method);
uriBuilder.path(method);
}

return template.toTemplate();
return uriBuilder.toTemplate();
} catch (IllegalArgumentException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.smallrye.opentelemetry.test.rest;

import static io.opentelemetry.api.trace.SpanKind.SERVER;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_ROUTE;
import static io.restassured.RestAssured.given;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URL;
import java.util.List;

import jakarta.inject.Inject;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import io.opentelemetry.sdk.trace.data.SpanData;
import io.smallrye.opentelemetry.test.InMemorySpanExporter;

@ExtendWith(ArquillianExtension.class)
class ContextRootTest {
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class);
}

@ArquillianResource
URL url;
@Inject
InMemorySpanExporter spanExporter;

@BeforeEach
void setUp() {
spanExporter.reset();
}

@Test
void route() {
given().get("/application/resource/span").then().statusCode(HTTP_OK);

List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(1, spanItems.size());
assertEquals(SERVER, spanItems.get(0).getKind());
assertEquals(url.getPath() + "application/resource/span", spanItems.get(0).getAttributes().get(HTTP_ROUTE));
}

@ApplicationPath("/application")
public static class RestApplication extends Application {

}

@Path("/resource")
public static class Resource {
@GET
@Path("/span")
public Response span() {
return Response.ok().build();
}
}
}
Loading