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

Fix nested http.route #8282

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void methodEnter(
Context parentContext = Context.current();

HttpRouteHolder.updateHttpRoute(
parentContext, HttpRouteSource.CONTROLLER, httpRouteGetter(), exchange);
parentContext, HttpRouteSource.NESTED_CONTROLLER, httpRouteGetter(), exchange);
heyams marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

can you add a brief comment here about why NESTED_CONTROLLER is needed?


if (handler == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ RouterFunction<ServerResponse> greetRouterFunction(GreetingHandler greetingHandl
.map(SpringWebFluxTestApplication::tracedMethod)));
}

@SuppressWarnings("deprecation") // testing instrumentation of deprecated class
@Component
public static class GreetingHandler {
public static final String DEFAULT_RESPONSE = "HELLO";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ protected void configure(HttpServerTestOptions options) {
// Mono that the controller returns completes) should end before the server span (which needs
// the result of the Mono)
options.setVerifyServerSpanEndTime(false);
options.setTestNestedPath(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public Mono<String> capture_headers(ServerHttpRequest request, ServerHttpRespons
});
}

@GetMapping("/nestedPath")
public Mono<String> nested_path(ServerHttpRequest request, ServerHttpResponse response) {
ServerEndpoint endpoint = ServerEndpoint.NESTED_PATH;

return wrapControllerMethod(
endpoint,
() -> {
setStatus(response, endpoint);
return endpoint.getBody();
});
}

protected abstract <T> Mono<T> wrapControllerMethod(ServerEndpoint endpoint, Supplier<T> handler);

private static void setStatus(ServerHttpResponse response, ServerEndpoint endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package server.base;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -98,9 +100,20 @@ public RouterFunction<ServerResponse> createRoutes() {
request.headers().asHttpHeaders().getFirst("X-Test-Request")),
null,
null);
});
})
.andNest(
path("/nestedPath"),
nest(
path("/hello"),
route(
path("/world"),
request -> {
ServerEndpoint endpoint = ServerEndpoint.NESTED_PATH;
return respond(endpoint, null, null, null);
})));
}

@SuppressWarnings("deprecation") // testing instrumentation of deprecated class
protected Mono<ServerResponse> respond(
ServerEndpoint endpoint, BodyBuilder bodyBuilder, String body, Runnable spanAction) {
if (bodyBuilder == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public String expectedHttpRoute(ServerEndpoint endpoint) {
return getContextPath() + "/path/{id}/param";
case NOT_FOUND:
return "/**";
case NESTED_PATH:
return "/nestedPath/hello/world";
default:
return super.expectedHttpRoute(endpoint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.NESTED_PATH;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.NOT_FOUND;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.PATH_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
Expand Down Expand Up @@ -299,6 +300,20 @@ void captureRequestParameters() {
assertTheTraces(1, null, null, spanId, "POST", CAPTURE_PARAMETERS, response);
}

@Test
void nestedPath() {
assumeTrue(Boolean.getBoolean("testLatestDeps"));
assumeTrue(options.testNestedPath);
String method = "GET";
AggregatedHttpRequest request = request(NESTED_PATH, method);
AggregatedHttpResponse response = client.execute(request).aggregate().join();
assertThat(response.status().code()).isEqualTo(NESTED_PATH.getStatus());
assertThat(response.contentUtf8()).isEqualTo(NESTED_PATH.getBody());
assertResponseHasCustomizedHeaders(response, NESTED_PATH, null);

assertTheTraces(1, null, null, null, method, NESTED_PATH, response);
}
heyams marked this conversation as resolved.
Show resolved Hide resolved

/**
* This test fires a bunch of parallel request to the fixed backend endpoint. That endpoint is
* supposed to create a new child span in the context of the SERVER span. That child span is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class HttpServerTestOptions {
boolean testNotFound = true;
boolean testPathParam = false;
boolean testCaptureHttpHeaders = true;
boolean testNestedPath = false;
boolean testCaptureRequestParameters = false;
boolean verifyServerSpanEndTime = true;

Expand Down Expand Up @@ -176,6 +177,12 @@ public HttpServerTestOptions setTestCaptureHttpHeaders(boolean testCaptureHttpHe
return this;
}

@CanIgnoreReturnValue
public HttpServerTestOptions setTestNestedPath(boolean testNestedPath) {
this.testNestedPath = testNestedPath;
return this;
}

@CanIgnoreReturnValue
public HttpServerTestOptions setTestCaptureRequestParameters(
boolean testCaptureRequestParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public enum ServerEndpoint {
EXCEPTION("exception", 500, "controller exception"),
NOT_FOUND("notFound", 404, "not found"),
CAPTURE_HEADERS("captureHeaders", 200, "headers captured"),
NESTED_PATH(
"nestedPath/hello/world",
200,
"nested path"), // TODO (heya) move it to webflux test module after this has been converted to
// a class
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

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

CAPTURE_PARAMETERS("captureParameters", 200, "parameters captured"),

// TODO: add tests for the following cases:
Expand Down