diff --git a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy index e21d36c4dc7b..76987ad3b863 100644 --- a/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy +++ b/instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy @@ -9,15 +9,144 @@ package server import io.opentelemetry.instrumentation.ratpack.server.AbstractRatpackHttpServerTest import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint +import ratpack.exec.Blocking +import ratpack.exec.util.ParallelBatch +import ratpack.http.client.HttpClient import ratpack.server.RatpackServerSpec +import java.util.concurrent.CountDownLatch + +import static io.opentelemetry.api.trace.SpanKind.CLIENT +import static io.opentelemetry.api.trace.SpanKind.CLIENT +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.SERVER +import static io.opentelemetry.api.trace.SpanKind.SERVER +import static io.opentelemetry.api.trace.SpanKind.SERVER + class RatpackHttpServerTest extends AbstractRatpackHttpServerTest implements AgentTestTrait { + + static ServerEndpoint PARALLEL_BATCH = new ServerEndpoint("PARALLEL-BATCH", "parallel-batch", 200, "1,2") + @Override void configure(RatpackServerSpec serverSpec) { + serverSpec.handlers { + it.get("batch/:number") { context -> + def number = context.getPathTokens().number + controller(new ServerEndpoint("BATCH", "batch/${number}", 200, "")) { + context.response.status(200).send(number) + } + } + it.prefix(PARALLEL_BATCH.rawPath()) { + it.all { context -> + def httpClient = context.get(HttpClient) + controller(PARALLEL_BATCH) { + CountDownLatch requestLatch = new CountDownLatch(1) + ParallelBatch.of( + httpClient.get( + resolveAddress( + new ServerEndpoint("BATCH", "batch/1", 200, "") + ).replace("h1c:", "http:").toURI() + ).next { r -> requestLatch.countDown() } + .map { it.body.text }, + Blocking.op { + requestLatch.await() + }.flatMap( + httpClient.get( + resolveAddress( + new ServerEndpoint("BATCH", "batch/2", 200, "") + ).replace("h1c:", "http:").toURI() + ) + ).map { it.body.text } + ).yield().then { result -> + context.response.status(200).send(result.sort().join(",")) + } + } + } + } + } } @Override boolean hasResponseCustomizer(ServerEndpoint endpoint) { true } + + def "test parallel batch propagation"() { + given: + def offset = 4 + + when: + def response = client.get(resolveAddress(PARALLEL_BATCH)).aggregate().join() + + then: + response.status().code() == PARALLEL_BATCH.status + response.contentUtf8() == PARALLEL_BATCH.body + + assertTraces(1) { + trace(0, 11) { + + span(0) { + name "GET /parallel-batch" + kind SERVER + hasNoParent() + } + span(1) { + name "/parallel-batch" + kind INTERNAL + childOf span(0) + } + span(2) { + name "controller" + kind INTERNAL + childOf span(1) + } + + span(3) { + kind CLIENT + childOf span(2) + } + span(4) { + name "GET /batch/:number" + kind SERVER + childOf span(3) + } + span(5) { + name "/batch/:number" + kind INTERNAL + childOf span(4) + } + span(6) { + name "controller" + kind INTERNAL + childOf span(5) + } + + span(3+offset) { + kind CLIENT + childOf span(2) + } + span(4+offset) { + name "GET /batch/:number" + kind SERVER + childOf span(3+offset) + } + span(5+offset) { + name "/batch/:number" + kind INTERNAL + childOf span(4+offset) + } + span(6+offset) { + name "controller" + kind INTERNAL + childOf span(5+offset) + } + + } + } + } } diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy b/instrumentation/ratpack/ratpack-1.7/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy index 3fd085dde585..f62995652400 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy @@ -8,15 +8,136 @@ package server import io.opentelemetry.instrumentation.ratpack.server.AbstractRatpackHttpServerTest import io.opentelemetry.instrumentation.test.AgentTestTrait import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint +import ratpack.exec.Blocking +import ratpack.exec.util.ParallelBatch +import ratpack.http.client.HttpClient import ratpack.server.RatpackServerSpec +import java.util.concurrent.CountDownLatch + +import static io.opentelemetry.api.trace.SpanKind.CLIENT +import static io.opentelemetry.api.trace.SpanKind.INTERNAL +import static io.opentelemetry.api.trace.SpanKind.SERVER + class RatpackHttpServerTest extends AbstractRatpackHttpServerTest implements AgentTestTrait { + + static ServerEndpoint PARALLEL_BATCH = new ServerEndpoint("PARALLEL-BATCH", "parallel-batch", 200, "1,2") + @Override void configure(RatpackServerSpec serverSpec) { + serverSpec.handlers { + it.get("batch/:number") { context -> + def number = context.getPathTokens().number + controller(new ServerEndpoint("BATCH", "batch/${number}", 200, "")) { + context.response.status(200).send(number) + } + } + it.prefix(PARALLEL_BATCH.rawPath()) { + it.all { context -> + def httpClient = context.get(HttpClient) + controller(PARALLEL_BATCH) { + CountDownLatch requestLatch = new CountDownLatch(1) + ParallelBatch.of( + httpClient.get( + resolveAddress( + new ServerEndpoint("BATCH", "batch/1", 200, "") + ).replace("h1c:", "http:").toURI() + ).next { r -> requestLatch.countDown() } + .map { it.body.text }, + Blocking.op { + requestLatch.await() + }.flatMap( + httpClient.get( + resolveAddress( + new ServerEndpoint("BATCH", "batch/2", 200, "") + ).replace("h1c:", "http:").toURI() + ) + ).map { it.body.text } + ).yield().then { result -> + context.response.status(200).send(result.sort().join(",")) + } + } + } + } + } } @Override boolean hasResponseCustomizer(ServerEndpoint endpoint) { true } + + def "test parallel batch propagation"() { + given: + def offset = 4 + + when: + def response = client.get(resolveAddress(PARALLEL_BATCH)).aggregate().join() + + then: + response.status().code() == PARALLEL_BATCH.status + response.contentUtf8() == PARALLEL_BATCH.body + + assertTraces(1) { + trace(0, 11) { + + span(0) { + name "GET /parallel-batch" + kind SERVER + hasNoParent() + } + span(1) { + name "/parallel-batch" + kind INTERNAL + childOf span(0) + } + span(2) { + name "controller" + kind INTERNAL + childOf span(1) + } + + span(3) { + kind CLIENT + childOf span(2) + } + span(4) { + name "GET /batch/:number" + kind SERVER + childOf span(3) + } + span(5) { + name "/batch/:number" + kind INTERNAL + childOf span(4) + } + span(6) { + name "controller" + kind INTERNAL + childOf span(5) + } + + span(3+offset) { + kind CLIENT + childOf span(2) + } + span(4+offset) { + name "GET /batch/:number" + kind SERVER + childOf span(3+offset) + } + span(5+offset) { + name "/batch/:number" + kind INTERNAL + childOf span(4+offset) + } + span(6+offset) { + name "controller" + kind INTERNAL + childOf span(5+offset) + } + + } + } + } }