Skip to content

Commit

Permalink
add explicit test for spans propagating through ParallelBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Nov 12, 2024
1 parent 093aeaa commit 26e1bf3
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
}
}
}

0 comments on commit 26e1bf3

Please sign in to comment.