From a78ef27cb01bddcf5b4f1827304368511eb560f6 Mon Sep 17 00:00:00 2001 From: Helen <56097766+heyams@users.noreply.github.com> Date: Fri, 11 Oct 2024 11:50:55 -0700 Subject: [PATCH] Convert play-mvc-2.6 groovy to java (#12340) --- .../play-mvc-2.6/javaagent/build.gradle.kts | 12 +- .../groovy/server/PlayAsyncServerTest.groovy | 99 ------------- .../groovy/server/PlayServerTest.groovy | 118 --------------- .../play/v2_6/PlayAsyncServerTest.java | 127 ++++++++++++++++ .../play/v2_6/PlayServerTest.java | 123 ++++++++++++++++ .../groovy/server/PlayAsyncServerTest.groovy | 106 -------------- .../test/groovy/server/PlayServerTest.groovy | 114 --------------- .../play/v2_6/PlayAsyncServerTest.java | 136 ++++++++++++++++++ .../play/v2_6/PlayServerTest.java | 122 ++++++++++++++++ 9 files changed, 513 insertions(+), 444 deletions(-) delete mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayAsyncServerTest.groovy delete mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayServerTest.groovy create mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java create mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java delete mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayAsyncServerTest.groovy delete mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayServerTest.groovy create mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java create mode 100644 instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/build.gradle.kts b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/build.gradle.kts index b310b68ac5f5..a19052c386f7 100644 --- a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/build.gradle.kts +++ b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/build.gradle.kts @@ -60,19 +60,17 @@ testing { } } +val testLatestDeps = findProperty("testLatestDeps") as Boolean tasks { - if (findProperty("testLatestDeps") as Boolean) { + if (testLatestDeps) { // disable regular test running and compiling tasks when latest dep test task is run named("test") { enabled = false } - named("compileTestGroovy") { - enabled = false - } + } - check { - dependsOn(testing.suites) - } + check { + dependsOn(testing.suites) } } diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayAsyncServerTest.groovy b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayAsyncServerTest.groovy deleted file mode 100644 index 80a8e004b226..000000000000 --- a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayAsyncServerTest.groovy +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package server - -import play.BuiltInComponents -import play.Mode -import play.libs.concurrent.HttpExecution -import play.mvc.Results -import play.routing.RequestFunctions -import play.routing.RoutingDsl -import play.server.Server -import spock.lang.Shared - -import java.util.concurrent.CompletableFuture -import java.util.concurrent.Executors - -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS -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.QUERY_PARAM -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS - -class PlayAsyncServerTest extends PlayServerTest { - @Shared - def executor = Executors.newCachedThreadPool() - - def cleanupSpec() { - executor.shutdown() - } - - @Override - Server startServer(int port) { - def execContext = HttpExecution.fromThread(executor) - return Server.forRouter(Mode.TEST, port) { BuiltInComponents components -> - RoutingDsl.fromComponents(components) - .GET(SUCCESS.getPath()).routingAsync({ - CompletableFuture.supplyAsync({ - controller(SUCCESS) { - Results.status(SUCCESS.getStatus(), SUCCESS.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .GET(QUERY_PARAM.getPath()).routingAsync({ - CompletableFuture.supplyAsync({ - controller(QUERY_PARAM) { - Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .GET(REDIRECT.getPath()).routingAsync({ - CompletableFuture.supplyAsync({ - controller(REDIRECT) { - Results.found(REDIRECT.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .GET(ERROR.getPath()).routingAsync({ - CompletableFuture.supplyAsync({ - controller(ERROR) { - Results.status(ERROR.getStatus(), ERROR.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .GET(EXCEPTION.getPath()).routingAsync({ - CompletableFuture.supplyAsync({ - controller(EXCEPTION) { - throw new Exception(EXCEPTION.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .GET(CAPTURE_HEADERS.getPath()).routingAsync({request -> - CompletableFuture.supplyAsync({ - controller(CAPTURE_HEADERS) { - def result = Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()) - request.header("X-Test-Request").ifPresent({ value -> - result = result.withHeader("X-Test-Response", value) - }) - result - } - }, execContext) - } as RequestFunctions.Params0) - .GET(INDEXED_CHILD.getPath()).routingAsync({request -> - String id = request.queryString("id").get() - CompletableFuture.supplyAsync({ - controller(INDEXED_CHILD) { - INDEXED_CHILD.collectSpanAttributes { name -> name == "id" ? id : null } - Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()) - } - }, execContext) - } as RequestFunctions.Params0) - .build() - } - } -} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayServerTest.groovy b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayServerTest.groovy deleted file mode 100644 index 2119ec65217a..000000000000 --- a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/groovy/server/PlayServerTest.groovy +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package server - -import io.opentelemetry.api.common.AttributeKey -import io.opentelemetry.api.trace.StatusCode -import io.opentelemetry.instrumentation.test.AgentTestTrait -import io.opentelemetry.instrumentation.test.asserts.TraceAssert -import io.opentelemetry.instrumentation.test.base.HttpServerTest -import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint -import io.opentelemetry.sdk.trace.data.SpanData -import play.BuiltInComponents -import play.Mode -import play.mvc.Results -import play.routing.RequestFunctions -import play.routing.RoutingDsl -import play.server.Server - -import static io.opentelemetry.api.trace.SpanKind.INTERNAL -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS -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.QUERY_PARAM -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS - -class PlayServerTest extends HttpServerTest implements AgentTestTrait { - @Override - Server startServer(int port) { - return Server.forRouter(Mode.TEST, port) { BuiltInComponents components -> - RoutingDsl.fromComponents(components) - .GET(SUCCESS.getPath()).routingTo({ - controller(SUCCESS) { - Results.status(SUCCESS.getStatus(), SUCCESS.getBody()) - } - } as RequestFunctions.Params0) - .GET(QUERY_PARAM.getPath()).routingTo({ - controller(QUERY_PARAM) { - Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()) - } - } as RequestFunctions.Params0) - .GET(REDIRECT.getPath()).routingTo({ - controller(REDIRECT) { - Results.found(REDIRECT.getBody()) - } - } as RequestFunctions.Params0) - .GET(ERROR.getPath()).routingTo({ - controller(ERROR) { - Results.status(ERROR.getStatus(), ERROR.getBody()) - } - } as RequestFunctions.Params0) - .GET(EXCEPTION.getPath()).routingTo({ - controller(EXCEPTION) { - throw new Exception(EXCEPTION.getBody()) - } - } as RequestFunctions.Params0) - .GET(CAPTURE_HEADERS.getPath()).routingTo({request -> - controller(CAPTURE_HEADERS) { - def result = Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()) - request.header("X-Test-Request").ifPresent({ value -> - result = result.withHeader("X-Test-Response", value) - }) - result - } - } as RequestFunctions.Params0) - .GET(INDEXED_CHILD.getPath()).routingTo({request -> - controller(INDEXED_CHILD) { - INDEXED_CHILD.collectSpanAttributes { name -> request.queryString(name).get() } - Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()) - } - } as RequestFunctions.Params0) - .build() - } - } - - @Override - void stopServer(Server server) { - server.stop() - } - - @Override - boolean hasHandlerSpan(ServerEndpoint endpoint) { - true - } - - @Override - boolean testHttpPipelining() { - false - } - - // play does not emit a span at all when a non standard HTTP method is used - @Override - boolean testNonStandardHttpMethod() { - false - } - - @Override - void handlerSpan(TraceAssert trace, int index, Object parent, String method = "GET", ServerEndpoint endpoint = SUCCESS) { - trace.span(index) { - name "play.request" - kind INTERNAL - childOf((SpanData) parent) - if (endpoint == EXCEPTION) { - status StatusCode.ERROR - errorEvent(Exception, EXCEPTION.body) - } - } - } - - @Override - Set> httpAttributes(ServerEndpoint endpoint) { - [] - } -} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java new file mode 100644 index 000000000000..8ebe7e0d3567 --- /dev/null +++ b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java @@ -0,0 +1,127 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.play.v2_6; + +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; +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.QUERY_PARAM; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import play.Mode; +import play.libs.concurrent.HttpExecution; +import play.mvc.Result; +import play.mvc.Results; +import play.routing.RoutingDsl; +import play.server.Server; +import scala.concurrent.ExecutionContextExecutor; + +class PlayAsyncServerTest extends PlayServerTest { + + private static final ExecutorService executor = Executors.newCachedThreadPool(); + + @Override + protected Server setupServer() { + ExecutionContextExecutor executionContextExecutor = HttpExecution.fromThread(executor); + return Server.forRouter( + Mode.TEST, + port, + components -> + RoutingDsl.fromComponents(components) + .GET(SUCCESS.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> + controller( + SUCCESS, + () -> Results.status(SUCCESS.getStatus(), SUCCESS.getBody())), + executionContextExecutor)) + .GET(QUERY_PARAM.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> + controller( + QUERY_PARAM, + () -> + Results.status( + QUERY_PARAM.getStatus(), QUERY_PARAM.getBody())), + executionContextExecutor)) + .GET(REDIRECT.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> controller(REDIRECT, () -> Results.found(REDIRECT.getBody())), + executionContextExecutor)) + .GET(ERROR.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> + controller( + ERROR, + () -> Results.status(ERROR.getStatus(), ERROR.getBody())), + executionContextExecutor)) + .GET(EXCEPTION.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> + controller( + EXCEPTION, + () -> { + throw new IllegalArgumentException(EXCEPTION.getBody()); + }), + executionContextExecutor)) + .GET(CAPTURE_HEADERS.getPath()) + .routingAsync( + request -> + CompletableFuture.supplyAsync( + () -> + controller( + CAPTURE_HEADERS, + () -> { + Result result = + Results.status( + CAPTURE_HEADERS.getStatus(), + CAPTURE_HEADERS.getBody()); + return request + .header("X-Test-Request") + .map(value -> result.withHeader("X-Test-Response", value)) + .orElse(result); + }), + executionContextExecutor)) + .GET(INDEXED_CHILD.getPath()) + .routingAsync( + request -> { + String id = request.queryString("id").orElse(null); + return CompletableFuture.supplyAsync( + () -> + controller( + INDEXED_CHILD, + () -> { + INDEXED_CHILD.collectSpanAttributes( + name -> "id".equals(name) ? id : null); + return Results.status( + INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()); + }), + executionContextExecutor); + }) + .build()); + } + + @Override + protected void stopServer(Server server) { + server.stop(); + executor.shutdown(); + } +} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java new file mode 100644 index 000000000000..4567ee9d766c --- /dev/null +++ b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/latestDepTest/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java @@ -0,0 +1,123 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.play.v2_6; + +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; +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.QUERY_PARAM; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; +import static java.util.Collections.emptySet; + +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; +import io.opentelemetry.sdk.trace.data.StatusData; +import org.junit.jupiter.api.extension.RegisterExtension; +import play.Mode; +import play.mvc.Result; +import play.mvc.Results; +import play.routing.RoutingDsl; +import play.server.Server; + +abstract class PlayServerTest extends AbstractHttpServerTest { + + @RegisterExtension + static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent(); + + @Override + protected Server setupServer() { + return Server.forRouter( + Mode.TEST, + port, + components -> + RoutingDsl.fromComponents(components) + .GET(SUCCESS.getPath()) + .routingTo( + request -> + controller( + SUCCESS, () -> Results.status(SUCCESS.getStatus(), SUCCESS.getBody()))) + .GET(QUERY_PARAM.getPath()) + .routingTo( + request -> + controller( + QUERY_PARAM, + () -> Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()))) + .GET(REDIRECT.getPath()) + .routingTo(request -> controller(REDIRECT, () -> Results.found(REDIRECT.getBody()))) + .GET(ERROR.getPath()) + .routingTo( + request -> + controller(ERROR, () -> Results.status(ERROR.getStatus(), ERROR.getBody()))) + .GET(EXCEPTION.getPath()) + .routingTo( + request -> + controller( + EXCEPTION, + () -> { + throw new IllegalArgumentException(EXCEPTION.getBody()); + })) + .GET(CAPTURE_HEADERS.getPath()) + .routingTo( + request -> + controller( + CAPTURE_HEADERS, + () -> { + Result result = + Results.status( + CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()); + request + .header("X-Test-Request") + .ifPresent(value -> result.withHeader("X-Test-Response", value)); + return result; + })) + .GET(INDEXED_CHILD.getPath()) + .routingTo( + request -> + controller( + INDEXED_CHILD, + () -> { + INDEXED_CHILD.collectSpanAttributes( + name -> request.queryString(name).orElse(null)); + return Results.status( + INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()); + })) + .build()); + } + + @Override + protected void stopServer(Server server) { + server.stop(); + } + + @Override + protected void configure(HttpServerTestOptions options) { + super.configure(options); + options.setHasHandlerSpan(unused -> true); + options.setTestHttpPipelining(false); + options.setHttpAttributes(endpoint -> emptySet()); + + options.setExpectedException(new IllegalArgumentException(EXCEPTION.getBody())); + options.disableTestNonStandardHttpMethod(); + } + + @Override + protected SpanDataAssert assertHandlerSpan( + SpanDataAssert span, String method, ServerEndpoint endpoint) { + span.hasName("play.request").hasKind(INTERNAL); + if (endpoint == EXCEPTION) { + span.hasStatus(StatusData.error()); + span.hasException(new IllegalArgumentException(EXCEPTION.getBody())); + } + return span; + } +} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayAsyncServerTest.groovy b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayAsyncServerTest.groovy deleted file mode 100644 index c78c00540411..000000000000 --- a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayAsyncServerTest.groovy +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package server - -import play.BuiltInComponents -import play.Mode -import play.libs.concurrent.HttpExecution -import play.mvc.Controller -import play.mvc.Results -import play.routing.RoutingDsl -import play.server.Server -import spock.lang.Shared - -import java.util.concurrent.CompletableFuture -import java.util.concurrent.Executors -import java.util.function.Supplier - -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS -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.QUERY_PARAM -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS - -class PlayAsyncServerTest extends PlayServerTest { - @Shared - def executor = Executors.newCachedThreadPool() - - def cleanupSpec() { - executor.shutdown() - } - - @Override - Server startServer(int port) { - def execContext = HttpExecution.fromThread(executor) - return Server.forRouter(Mode.TEST, port) { BuiltInComponents components -> - RoutingDsl.fromComponents(components) - .GET(SUCCESS.getPath()).routeAsync({ - CompletableFuture.supplyAsync({ - controller(SUCCESS) { - Results.status(SUCCESS.getStatus(), SUCCESS.getBody()) - } - }, execContext) - } as Supplier) - .GET(QUERY_PARAM.getPath()).routeAsync({ - CompletableFuture.supplyAsync({ - controller(QUERY_PARAM) { - Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()) - } - }, execContext) - } as Supplier) - .GET(REDIRECT.getPath()).routeAsync({ - CompletableFuture.supplyAsync({ - controller(REDIRECT) { - Results.found(REDIRECT.getBody()) - } - }, execContext) - } as Supplier) - .GET(ERROR.getPath()).routeAsync({ - CompletableFuture.supplyAsync({ - controller(ERROR) { - Results.status(ERROR.getStatus(), ERROR.getBody()) - } - }, execContext) - } as Supplier) - .GET(EXCEPTION.getPath()).routeAsync({ - CompletableFuture.supplyAsync({ - controller(EXCEPTION) { - throw new Exception(EXCEPTION.getBody()) - } - }, execContext) - } as Supplier) - .GET(CAPTURE_HEADERS.getPath()).routeAsync({ - def request = Controller.request() - def response = Controller.response() - CompletableFuture.supplyAsync({ - controller(CAPTURE_HEADERS) { - request.header("X-Test-Request").ifPresent({ value -> - response.setHeader("X-Test-Response", value) - }) - Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()) - } - }, execContext) - } as Supplier) - .GET(INDEXED_CHILD.getPath()).routeAsync({ - String id = Controller.request().getQueryString("id") - CompletableFuture.supplyAsync({ - controller(INDEXED_CHILD) { - INDEXED_CHILD.collectSpanAttributes { name -> name == "id" ? id : null } - Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()) - } - }, execContext) - } as Supplier) - .build() - } - } - - @Override - boolean testHttpPipelining() { - false - } -} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayServerTest.groovy b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayServerTest.groovy deleted file mode 100644 index 9f3ab3cb7759..000000000000 --- a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/groovy/server/PlayServerTest.groovy +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package server - -import io.opentelemetry.api.common.AttributeKey -import io.opentelemetry.api.trace.StatusCode -import io.opentelemetry.instrumentation.test.AgentTestTrait -import io.opentelemetry.instrumentation.test.asserts.TraceAssert -import io.opentelemetry.instrumentation.test.base.HttpServerTest -import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint -import io.opentelemetry.sdk.trace.data.SpanData -import play.BuiltInComponents -import play.Mode -import play.mvc.Controller -import play.mvc.Results -import play.routing.RoutingDsl -import play.server.Server - -import java.util.function.Supplier - -import static io.opentelemetry.api.trace.SpanKind.INTERNAL -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS -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.QUERY_PARAM -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT -import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS - -class PlayServerTest extends HttpServerTest implements AgentTestTrait { - @Override - Server startServer(int port) { - return Server.forRouter(Mode.TEST, port) { BuiltInComponents components -> - RoutingDsl.fromComponents(components) - .GET(SUCCESS.getPath()).routeTo({ - controller(SUCCESS) { - Results.status(SUCCESS.getStatus(), SUCCESS.getBody()) - } - } as Supplier) - .GET(QUERY_PARAM.getPath()).routeTo({ - controller(QUERY_PARAM) { - Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()) - } - } as Supplier) - .GET(REDIRECT.getPath()).routeTo({ - controller(REDIRECT) { - Results.found(REDIRECT.getBody()) - } - } as Supplier) - .GET(ERROR.getPath()).routeTo({ - controller(ERROR) { - Results.status(ERROR.getStatus(), ERROR.getBody()) - } - } as Supplier) - .GET(EXCEPTION.getPath()).routeTo({ - controller(EXCEPTION) { - throw new Exception(EXCEPTION.getBody()) - } - } as Supplier) - .GET(CAPTURE_HEADERS.getPath()).routeTo({ - controller(CAPTURE_HEADERS) { - Controller.request().header("X-Test-Request").ifPresent({ value -> - Controller.response().setHeader("X-Test-Response", value) - }) - Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()) - } - } as Supplier) - .GET(INDEXED_CHILD.getPath()).routeTo({ - controller(INDEXED_CHILD) { - INDEXED_CHILD.collectSpanAttributes { name -> Controller.request().getQueryString(name) } - Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()) - } - } as Supplier) - .build() - } - } - - @Override - void stopServer(Server server) { - server.stop() - } - - @Override - boolean hasHandlerSpan(ServerEndpoint endpoint) { - true - } - - @Override - void handlerSpan(TraceAssert trace, int index, Object parent, String method = "GET", ServerEndpoint endpoint = SUCCESS) { - trace.span(index) { - name "play.request" - kind INTERNAL - childOf((SpanData) parent) - if (endpoint == EXCEPTION) { - status StatusCode.ERROR - errorEvent(Exception, EXCEPTION.body) - } - } - } - - @Override - Set> httpAttributes(ServerEndpoint endpoint) { - [] - } - - @Override - boolean testNonStandardHttpMethod() { - // instrumentation does not create a server span at all - false - } -} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java new file mode 100644 index 000000000000..9bf063fdc50c --- /dev/null +++ b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayAsyncServerTest.java @@ -0,0 +1,136 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.play.v2_6; + +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; +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.QUERY_PARAM; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; + +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import play.Mode; +import play.libs.concurrent.HttpExecution; +import play.mvc.Controller; +import play.mvc.Http; +import play.mvc.Results; +import play.routing.RoutingDsl; +import play.server.Server; +import scala.concurrent.ExecutionContextExecutor; + +class PlayAsyncServerTest extends PlayServerTest { + + private static final ExecutorService executor = Executors.newCachedThreadPool(); + + @Override + protected Server setupServer() { + ExecutionContextExecutor executionContextExecutor = HttpExecution.fromThread(executor); + return Server.forRouter( + Mode.TEST, + port, + components -> + RoutingDsl.fromComponents(components) + .GET(SUCCESS.getPath()) + .routeAsync( + () -> + CompletableFuture.supplyAsync( + () -> + controller( + SUCCESS, + () -> Results.status(SUCCESS.getStatus(), SUCCESS.getBody())), + executionContextExecutor)) + .GET(QUERY_PARAM.getPath()) + .routeAsync( + () -> + CompletableFuture.supplyAsync( + () -> + controller( + QUERY_PARAM, + () -> + Results.status( + QUERY_PARAM.getStatus(), QUERY_PARAM.getBody())), + executionContextExecutor)) + .GET(REDIRECT.getPath()) + .routeAsync( + () -> + CompletableFuture.supplyAsync( + () -> controller(REDIRECT, () -> Results.found(REDIRECT.getBody())), + executionContextExecutor)) + .GET(ERROR.getPath()) + .routeAsync( + () -> + CompletableFuture.supplyAsync( + () -> + controller( + ERROR, + () -> Results.status(ERROR.getStatus(), ERROR.getBody())), + executionContextExecutor)) + .GET(EXCEPTION.getPath()) + .routeAsync( + () -> + CompletableFuture.supplyAsync( + () -> + controller( + EXCEPTION, + () -> { + throw new IllegalArgumentException(EXCEPTION.getBody()); + }), + executionContextExecutor)) + .GET(CAPTURE_HEADERS.getPath()) + .routeAsync( + () -> { + Http.Request request = Controller.request(); + Http.Response response = Controller.response(); + return CompletableFuture.supplyAsync( + () -> + controller( + CAPTURE_HEADERS, + () -> { + request + .header("X-Test-Request") + .ifPresent( + value -> response.setHeader("X-Test-Response", value)); + return Results.status( + CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()); + }), + executionContextExecutor); + }) + .GET(INDEXED_CHILD.getPath()) + .routeAsync( + () -> { + String id = Controller.request().getQueryString("id"); + return CompletableFuture.supplyAsync( + () -> + controller( + INDEXED_CHILD, + () -> { + INDEXED_CHILD.collectSpanAttributes( + name -> "id".equals(name) ? id : null); + return Results.status( + INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()); + }), + executionContextExecutor); + }) + .build()); + } + + @Override + protected void stopServer(Server server) { + server.stop(); + executor.shutdown(); + } + + @Override + protected void configure(HttpServerTestOptions options) { + super.configure(options); + options.setTestHttpPipelining(false); + } +} diff --git a/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java new file mode 100644 index 000000000000..b1be98ba0674 --- /dev/null +++ b/instrumentation/play/play-mvc/play-mvc-2.6/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/play/v2_6/PlayServerTest.java @@ -0,0 +1,122 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.play.v2_6; + +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; +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.QUERY_PARAM; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; +import static java.util.Collections.emptySet; + +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; +import io.opentelemetry.sdk.trace.data.StatusData; +import org.junit.jupiter.api.extension.RegisterExtension; +import play.Mode; +import play.mvc.Controller; +import play.mvc.Results; +import play.routing.RoutingDsl; +import play.server.Server; + +abstract class PlayServerTest extends AbstractHttpServerTest { + + @RegisterExtension + static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent(); + + @Override + protected Server setupServer() { + return Server.forRouter( + Mode.TEST, + port, + components -> + RoutingDsl.fromComponents(components) + .GET(SUCCESS.getPath()) + .routeTo( + () -> + controller( + SUCCESS, () -> Results.status(SUCCESS.getStatus(), SUCCESS.getBody()))) + .GET(QUERY_PARAM.getPath()) + .routeTo( + () -> + controller( + QUERY_PARAM, + () -> Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()))) + .GET(REDIRECT.getPath()) + .routeTo(() -> controller(REDIRECT, () -> Results.found(REDIRECT.getBody()))) + .GET(ERROR.getPath()) + .routeTo( + () -> + controller(ERROR, () -> Results.status(ERROR.getStatus(), ERROR.getBody()))) + .GET(EXCEPTION.getPath()) + .routeTo( + () -> + controller( + EXCEPTION, + () -> { + throw new IllegalArgumentException(EXCEPTION.getBody()); + })) + .GET(CAPTURE_HEADERS.getPath()) + .routeTo( + () -> + controller( + CAPTURE_HEADERS, + () -> { + Controller.request() + .header("X-Test-Request") + .ifPresent( + value -> + Controller.response() + .setHeader("X-Test-Response", value)); + return Results.status( + CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()); + })) + .GET(INDEXED_CHILD.getPath()) + .routeTo( + () -> + controller( + INDEXED_CHILD, + () -> { + INDEXED_CHILD.collectSpanAttributes( + name -> Controller.request().getQueryString(name)); + return Results.status( + INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()); + })) + .build()); + } + + @Override + protected void stopServer(Server server) throws Exception { + server.stop(); + } + + @Override + protected void configure(HttpServerTestOptions options) { + super.configure(options); + options.setHasHandlerSpan(unused -> true); + options.setHttpAttributes(endpoint -> emptySet()); + options.setExpectedException(new IllegalArgumentException(EXCEPTION.getBody())); + options.disableTestNonStandardHttpMethod(); + } + + @Override + protected SpanDataAssert assertHandlerSpan( + SpanDataAssert span, String method, ServerEndpoint endpoint) { + span.hasName("play.request").hasKind(INTERNAL); + if (endpoint == EXCEPTION) { + span.hasStatus(StatusData.error()); + span.hasException(new IllegalArgumentException(EXCEPTION.getBody())); + } + return span; + } +}