-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathVertxReactivePropagationTest.groovy
219 lines (201 loc) · 7.98 KB
/
VertxReactivePropagationTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.trace.SpanKind
import io.opentelemetry.context.Context
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.instrumentation.test.utils.PortUtils
import io.opentelemetry.semconv.ClientAttributes
import io.opentelemetry.semconv.HttpAttributes
import io.opentelemetry.semconv.NetworkAttributes
import io.opentelemetry.semconv.ServerAttributes
import io.opentelemetry.semconv.UrlAttributes
import io.opentelemetry.semconv.UserAgentAttributes
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes
import io.opentelemetry.testing.internal.armeria.client.WebClient
import io.opentelemetry.testing.internal.armeria.common.HttpRequest
import io.opentelemetry.testing.internal.armeria.common.HttpRequestBuilder
import io.vertx.reactivex.core.Vertx
import spock.lang.Shared
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import static VertxReactiveWebServer.TEST_REQUEST_ID_ATTRIBUTE
import static VertxReactiveWebServer.TEST_REQUEST_ID_PARAMETER
import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.SpanKind.SERVER
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS
class VertxReactivePropagationTest extends AgentInstrumentationSpecification {
@Shared
WebClient client
@Shared
int port
@Shared
Vertx server
def setupSpec() {
port = PortUtils.findOpenPort()
server = VertxReactiveWebServer.start(port)
client = WebClient.of("h1c://localhost:${port}")
}
def cleanupSpec() {
server.close()
}
//Verifies that context is correctly propagated and sql query span has correct parent.
//Tests io.opentelemetry.javaagent.instrumentation.vertx.reactive.VertxRxInstrumentation
@SuppressWarnings("deprecation") // TODO DbIncubatingAttributes.DB_CONNECTION_STRING deprecation
def "should propagate context over vert.x rx-java framework"() {
setup:
def response = client.get("/listProducts").aggregate().join()
expect:
response.status().code() == SUCCESS.status
and:
assertTraces(1) {
trace(0, 4) {
span(0) {
name "GET /listProducts"
kind SERVER
hasNoParent()
attributes {
"$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
"$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1"
"$NetworkAttributes.NETWORK_PEER_PORT" Long
"$ServerAttributes.SERVER_ADDRESS" "localhost"
"$ServerAttributes.SERVER_PORT" Long
"$ClientAttributes.CLIENT_ADDRESS" "127.0.0.1"
"$UrlAttributes.URL_PATH" "/listProducts"
"$HttpAttributes.HTTP_REQUEST_METHOD" "GET"
"$HttpAttributes.HTTP_RESPONSE_STATUS_CODE" 200
"$UrlAttributes.URL_SCHEME" "http"
"$UserAgentAttributes.USER_AGENT_ORIGINAL" String
"$HttpAttributes.HTTP_ROUTE" "/listProducts"
}
}
span(1) {
name "handleListProducts"
kind SpanKind.INTERNAL
childOf span(0)
}
span(2) {
name "listProducts"
kind SpanKind.INTERNAL
childOf span(1)
}
span(3) {
name "SELECT test.products"
kind CLIENT
childOf span(2)
attributes {
"$DbIncubatingAttributes.DB_SYSTEM" "hsqldb"
"${maybeStable(DbIncubatingAttributes.DB_NAME)}" "test"
"$DbIncubatingAttributes.DB_USER" emitStableDatabaseSemconv() ? null : "SA"
"$DbIncubatingAttributes.DB_CONNECTION_STRING" emitStableDatabaseSemconv() ? null : "hsqldb:mem:"
"${maybeStable(DbIncubatingAttributes.DB_STATEMENT)}" "SELECT id, name, price, weight FROM products"
"${maybeStable(DbIncubatingAttributes.DB_OPERATION)}" "SELECT"
"${maybeStable(DbIncubatingAttributes.DB_SQL_TABLE)}" "products"
}
}
}
}
}
@SuppressWarnings("deprecation") // TODO DbIncubatingAttributes.DB_CONNECTION_STRING deprecation
def "should propagate context correctly over vert.x rx-java framework with high concurrency"() {
setup:
int count = 100
def baseUrl = "/listProducts"
def latch = new CountDownLatch(1)
def pool = Executors.newFixedThreadPool(8)
def propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator()
def setter = { HttpRequestBuilder carrier, String name, String value ->
carrier.header(name, value)
}
when:
count.times { index ->
def job = {
latch.await()
runWithSpan("client " + index) {
HttpRequestBuilder builder = HttpRequest.builder()
.get("${baseUrl}?${TEST_REQUEST_ID_PARAMETER}=${index}")
Span.current().setAttribute(TEST_REQUEST_ID_ATTRIBUTE, index)
propagator.inject(Context.current(), builder, setter)
client.execute(builder.build()).aggregate().join()
}
}
pool.submit(job)
}
latch.countDown()
then:
assertTraces(count) {
(0..count - 1).each {
trace(it, 5) {
def rootSpan = it.span(0)
def requestId = Long.valueOf(rootSpan.name.substring("client ".length()))
span(0) {
name "client $requestId"
kind SpanKind.INTERNAL
hasNoParent()
attributes {
"${TEST_REQUEST_ID_ATTRIBUTE}" requestId
}
}
span(1) {
name "GET /listProducts"
kind SERVER
childOf(span(0))
attributes {
"$NetworkAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
"$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1"
"$NetworkAttributes.NETWORK_PEER_PORT" Long
"$ServerAttributes.SERVER_ADDRESS" "localhost"
"$ServerAttributes.SERVER_PORT" Long
"$ClientAttributes.CLIENT_ADDRESS" "127.0.0.1"
"$UrlAttributes.URL_PATH" baseUrl
"$UrlAttributes.URL_QUERY" "$TEST_REQUEST_ID_PARAMETER=$requestId"
"$HttpAttributes.HTTP_REQUEST_METHOD" "GET"
"$HttpAttributes.HTTP_RESPONSE_STATUS_CODE" 200
"$UrlAttributes.URL_SCHEME" "http"
"$UserAgentAttributes.USER_AGENT_ORIGINAL" String
"$HttpAttributes.HTTP_ROUTE" "/listProducts"
"${TEST_REQUEST_ID_ATTRIBUTE}" requestId
}
}
span(2) {
name "handleListProducts"
kind SpanKind.INTERNAL
childOf(span(1))
attributes {
"${TEST_REQUEST_ID_ATTRIBUTE}" requestId
}
}
span(3) {
name "listProducts"
kind SpanKind.INTERNAL
childOf(span(2))
attributes {
"${TEST_REQUEST_ID_ATTRIBUTE}" requestId
}
}
span(4) {
name "SELECT test.products"
kind CLIENT
childOf(span(3))
attributes {
"$DbIncubatingAttributes.DB_SYSTEM" "hsqldb"
"${maybeStable(DbIncubatingAttributes.DB_NAME)}" "test"
"$DbIncubatingAttributes.DB_USER" emitStableDatabaseSemconv() ? null : "SA"
"$DbIncubatingAttributes.DB_CONNECTION_STRING" emitStableDatabaseSemconv() ? null : "hsqldb:mem:"
"${maybeStable(DbIncubatingAttributes.DB_STATEMENT)}" "SELECT id AS request$requestId, name, price, weight FROM products"
"${maybeStable(DbIncubatingAttributes.DB_OPERATION)}" "SELECT"
"${maybeStable(DbIncubatingAttributes.DB_SQL_TABLE)}" "products"
}
}
}
}
}
cleanup:
pool.shutdownNow()
}
}