Skip to content

Commit 26e1bf3

Browse files
committed
add explicit test for spans propagating through ParallelBatch
1 parent 093aeaa commit 26e1bf3

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

instrumentation/ratpack/ratpack-1.4/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,144 @@ package server
99
import io.opentelemetry.instrumentation.ratpack.server.AbstractRatpackHttpServerTest
1010
import io.opentelemetry.instrumentation.test.AgentTestTrait
1111
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
12+
import ratpack.exec.Blocking
13+
import ratpack.exec.util.ParallelBatch
14+
import ratpack.http.client.HttpClient
1215
import ratpack.server.RatpackServerSpec
1316

17+
import java.util.concurrent.CountDownLatch
18+
19+
import static io.opentelemetry.api.trace.SpanKind.CLIENT
20+
import static io.opentelemetry.api.trace.SpanKind.CLIENT
21+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
22+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
23+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
24+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
25+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
26+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
27+
import static io.opentelemetry.api.trace.SpanKind.SERVER
28+
import static io.opentelemetry.api.trace.SpanKind.SERVER
29+
import static io.opentelemetry.api.trace.SpanKind.SERVER
30+
1431
class RatpackHttpServerTest extends AbstractRatpackHttpServerTest implements AgentTestTrait {
32+
33+
static ServerEndpoint PARALLEL_BATCH = new ServerEndpoint("PARALLEL-BATCH", "parallel-batch", 200, "1,2")
34+
1535
@Override
1636
void configure(RatpackServerSpec serverSpec) {
37+
serverSpec.handlers {
38+
it.get("batch/:number") { context ->
39+
def number = context.getPathTokens().number
40+
controller(new ServerEndpoint("BATCH", "batch/${number}", 200, "")) {
41+
context.response.status(200).send(number)
42+
}
43+
}
44+
it.prefix(PARALLEL_BATCH.rawPath()) {
45+
it.all { context ->
46+
def httpClient = context.get(HttpClient)
47+
controller(PARALLEL_BATCH) {
48+
CountDownLatch requestLatch = new CountDownLatch(1)
49+
ParallelBatch.of(
50+
httpClient.get(
51+
resolveAddress(
52+
new ServerEndpoint("BATCH", "batch/1", 200, "")
53+
).replace("h1c:", "http:").toURI()
54+
).next { r -> requestLatch.countDown() }
55+
.map { it.body.text },
56+
Blocking.op {
57+
requestLatch.await()
58+
}.flatMap(
59+
httpClient.get(
60+
resolveAddress(
61+
new ServerEndpoint("BATCH", "batch/2", 200, "")
62+
).replace("h1c:", "http:").toURI()
63+
)
64+
).map { it.body.text }
65+
).yield().then { result ->
66+
context.response.status(200).send(result.sort().join(","))
67+
}
68+
}
69+
}
70+
}
71+
}
1772
}
1873

1974
@Override
2075
boolean hasResponseCustomizer(ServerEndpoint endpoint) {
2176
true
2277
}
78+
79+
def "test parallel batch propagation"() {
80+
given:
81+
def offset = 4
82+
83+
when:
84+
def response = client.get(resolveAddress(PARALLEL_BATCH)).aggregate().join()
85+
86+
then:
87+
response.status().code() == PARALLEL_BATCH.status
88+
response.contentUtf8() == PARALLEL_BATCH.body
89+
90+
assertTraces(1) {
91+
trace(0, 11) {
92+
93+
span(0) {
94+
name "GET /parallel-batch"
95+
kind SERVER
96+
hasNoParent()
97+
}
98+
span(1) {
99+
name "/parallel-batch"
100+
kind INTERNAL
101+
childOf span(0)
102+
}
103+
span(2) {
104+
name "controller"
105+
kind INTERNAL
106+
childOf span(1)
107+
}
108+
109+
span(3) {
110+
kind CLIENT
111+
childOf span(2)
112+
}
113+
span(4) {
114+
name "GET /batch/:number"
115+
kind SERVER
116+
childOf span(3)
117+
}
118+
span(5) {
119+
name "/batch/:number"
120+
kind INTERNAL
121+
childOf span(4)
122+
}
123+
span(6) {
124+
name "controller"
125+
kind INTERNAL
126+
childOf span(5)
127+
}
128+
129+
span(3+offset) {
130+
kind CLIENT
131+
childOf span(2)
132+
}
133+
span(4+offset) {
134+
name "GET /batch/:number"
135+
kind SERVER
136+
childOf span(3+offset)
137+
}
138+
span(5+offset) {
139+
name "/batch/:number"
140+
kind INTERNAL
141+
childOf span(4+offset)
142+
}
143+
span(6+offset) {
144+
name "controller"
145+
kind INTERNAL
146+
childOf span(5+offset)
147+
}
148+
149+
}
150+
}
151+
}
23152
}

instrumentation/ratpack/ratpack-1.7/javaagent/src/test/groovy/server/RatpackHttpServerTest.groovy

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,136 @@ package server
88
import io.opentelemetry.instrumentation.ratpack.server.AbstractRatpackHttpServerTest
99
import io.opentelemetry.instrumentation.test.AgentTestTrait
1010
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
11+
import ratpack.exec.Blocking
12+
import ratpack.exec.util.ParallelBatch
13+
import ratpack.http.client.HttpClient
1114
import ratpack.server.RatpackServerSpec
1215

16+
import java.util.concurrent.CountDownLatch
17+
18+
import static io.opentelemetry.api.trace.SpanKind.CLIENT
19+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
20+
import static io.opentelemetry.api.trace.SpanKind.SERVER
21+
1322
class RatpackHttpServerTest extends AbstractRatpackHttpServerTest implements AgentTestTrait {
23+
24+
static ServerEndpoint PARALLEL_BATCH = new ServerEndpoint("PARALLEL-BATCH", "parallel-batch", 200, "1,2")
25+
1426
@Override
1527
void configure(RatpackServerSpec serverSpec) {
28+
serverSpec.handlers {
29+
it.get("batch/:number") { context ->
30+
def number = context.getPathTokens().number
31+
controller(new ServerEndpoint("BATCH", "batch/${number}", 200, "")) {
32+
context.response.status(200).send(number)
33+
}
34+
}
35+
it.prefix(PARALLEL_BATCH.rawPath()) {
36+
it.all { context ->
37+
def httpClient = context.get(HttpClient)
38+
controller(PARALLEL_BATCH) {
39+
CountDownLatch requestLatch = new CountDownLatch(1)
40+
ParallelBatch.of(
41+
httpClient.get(
42+
resolveAddress(
43+
new ServerEndpoint("BATCH", "batch/1", 200, "")
44+
).replace("h1c:", "http:").toURI()
45+
).next { r -> requestLatch.countDown() }
46+
.map { it.body.text },
47+
Blocking.op {
48+
requestLatch.await()
49+
}.flatMap(
50+
httpClient.get(
51+
resolveAddress(
52+
new ServerEndpoint("BATCH", "batch/2", 200, "")
53+
).replace("h1c:", "http:").toURI()
54+
)
55+
).map { it.body.text }
56+
).yield().then { result ->
57+
context.response.status(200).send(result.sort().join(","))
58+
}
59+
}
60+
}
61+
}
62+
}
1663
}
1764

1865
@Override
1966
boolean hasResponseCustomizer(ServerEndpoint endpoint) {
2067
true
2168
}
69+
70+
def "test parallel batch propagation"() {
71+
given:
72+
def offset = 4
73+
74+
when:
75+
def response = client.get(resolveAddress(PARALLEL_BATCH)).aggregate().join()
76+
77+
then:
78+
response.status().code() == PARALLEL_BATCH.status
79+
response.contentUtf8() == PARALLEL_BATCH.body
80+
81+
assertTraces(1) {
82+
trace(0, 11) {
83+
84+
span(0) {
85+
name "GET /parallel-batch"
86+
kind SERVER
87+
hasNoParent()
88+
}
89+
span(1) {
90+
name "/parallel-batch"
91+
kind INTERNAL
92+
childOf span(0)
93+
}
94+
span(2) {
95+
name "controller"
96+
kind INTERNAL
97+
childOf span(1)
98+
}
99+
100+
span(3) {
101+
kind CLIENT
102+
childOf span(2)
103+
}
104+
span(4) {
105+
name "GET /batch/:number"
106+
kind SERVER
107+
childOf span(3)
108+
}
109+
span(5) {
110+
name "/batch/:number"
111+
kind INTERNAL
112+
childOf span(4)
113+
}
114+
span(6) {
115+
name "controller"
116+
kind INTERNAL
117+
childOf span(5)
118+
}
119+
120+
span(3+offset) {
121+
kind CLIENT
122+
childOf span(2)
123+
}
124+
span(4+offset) {
125+
name "GET /batch/:number"
126+
kind SERVER
127+
childOf span(3+offset)
128+
}
129+
span(5+offset) {
130+
name "/batch/:number"
131+
kind INTERNAL
132+
childOf span(4+offset)
133+
}
134+
span(6+offset) {
135+
name "controller"
136+
kind INTERNAL
137+
childOf span(5+offset)
138+
}
139+
140+
}
141+
}
142+
}
22143
}

0 commit comments

Comments
 (0)