-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathAkkaHttpServerInstrumentationTest.groovy
More file actions
343 lines (282 loc) · 7.98 KB
/
AkkaHttpServerInstrumentationTest.groovy
File metadata and controls
343 lines (282 loc) · 7.98 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import datadog.trace.agent.test.base.HttpServer
import datadog.trace.agent.test.base.HttpServerTest
import datadog.trace.agent.test.naming.TestingGenericHttpNamingConventions
import datadog.trace.test.util.ThreadUtils
import datadog.trace.instrumentation.akkahttp.AkkaHttpServerDecorator
import okhttp3.HttpUrl
import okhttp3.MultipartBody
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import spock.lang.IgnoreIf
import spock.lang.Shared
import java.util.concurrent.atomic.AtomicInteger
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_JSON
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_MULTIPART
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS
abstract class AkkaHttpServerInstrumentationTest extends HttpServerTest<AkkaHttpTestWebServer> {
@Override
String component() {
return AkkaHttpServerDecorator.DECORATE.component()
}
@Override
String expectedOperationName() {
return "akka-http.request"
}
@Override
boolean testExceptionBody() {
false
}
@Override
boolean hasExtraErrorInformation() {
return true
}
@Override
protected boolean enabledFinishTimingChecks() {
true
}
@Override
boolean changesAll404s() {
true
}
@Override
boolean testBlocking() {
true
}
@Override
boolean testBlockingOnResponse() {
true
}
@Override
boolean testRequestBody() {
true
}
@Override
boolean testBodyUrlencoded() {
true
}
@Override
boolean testBodyMultipart() {
true
}
@Override
boolean testBodyFilenames() {
true
}
@Override
boolean testBodyJson() {
true
}
@Override
boolean isRequestBodyNoStreaming() {
true
}
//@Ignore("https://github.com/DataDog/dd-trace-java/pull/5213")
@Override
boolean testBadUrl() {
false
}
@Shared
def totalInvocations = 200
@Shared
AtomicInteger counter = new AtomicInteger(0)
void doAndValidateRequest(int id) {
def type = id & 1 ? "p" : "f"
String url = address.resolve("/injected-id/${type}ing/$id")
def traceId = totalInvocations + id
def request = new Request.Builder().url(url).get().header("x-datadog-trace-id", traceId.toString()).build()
def response = client.newCall(request).execute()
def responseBodyStr = response.body().string()
assert responseBodyStr == "${type}ong $id -> $traceId"
assert response.code() == 200
}
def "propagate trace id when we ping akka-http concurrently"() {
expect:
ThreadUtils.runConcurrently(10, totalInvocations, {
def id = counter.incrementAndGet()
doAndValidateRequest(id)
})
and:
TEST_WRITER.waitForTraces(totalInvocations)
}
@IgnoreIf({ !instance.testBodyMultipart() })
def 'test instrumentation gateway multipart request body — strict variant'() {
setup:
def body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart('a', 'x')
.build()
def url = HttpUrl.get(BODY_MULTIPART.resolve(address)).newBuilder()
.encodedQuery('variant=strictUnmarshaller')
.build()
def request = new Request.Builder()
.url(url)
.method('POST', body)
.build()
def response = client.newCall(request).execute()
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
}
expect:
response.body().charStream().text == '[a:[x]]'
when:
TEST_WRITER.waitForTraces(1)
then:
TEST_WRITER.get(0).any {
it.getTag('request.body.converted') == '[a:[x]]'
}
}
@IgnoreIf({ !instance.testBodyJson()})
def 'test instrumentation gateway json request body — spray variant'() {
setup:
def url = HttpUrl.get(BODY_JSON.resolve(address)).newBuilder()
.encodedQuery('variant=spray')
.build()
def request = new Request.Builder()
.url(url)
.method('POST', RequestBody.create(okhttp3.MediaType.get('application/json'), '{"a":"x"}\n'))
.build()
def response = client.newCall(request).execute()
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
}
expect:
response.body().charStream().text == '{"a":"x"}'
when:
TEST_WRITER.waitForTraces(1)
then:
TEST_WRITER.get(0).any {
it.getTag('request.body.converted') == '[a:[x]]'
}
}
void 'content length and type are provided to IG on strict responses'() {
setup:
Request request = request(SUCCESS, 'GET', null)
.header(IG_EXTRA_SPAN_NAME_HEADER, 'ig-span')
.header(IG_ASK_FOR_RESPONSE_HEADER_TAGS_HEADER, 'true')
.build()
Response response = client.newCall(request).execute()
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
}
expect:
response.body().charStream().text == SUCCESS.body
when:
TEST_WRITER.waitForTraces(1)
def tags = TEST_WRITER.get(0).find { it.spanName == 'ig-span' }.tags
then:
tags['response.header.content-type'] != null
tags['response.header.content-length'] == SUCCESS.body.length() as String
}
}
abstract class AkkaHttpServerInstrumentationSyncTest extends AkkaHttpServerInstrumentationTest {
@Override
HttpServer server() {
return new AkkaHttpTestWebServer(AkkaHttpTestWebServer.BindAndHandleSync())
}
@Override
String expectedOperationName() {
return operation()
}
// we test body endpoints only on the async tests
@Override
boolean testRequestBody() {
false
}
@Override
boolean testBodyMultipart() {
false
}
@Override
boolean testBodyFilenames() {
false
}
@Override
boolean testBodyJson() {
false
}
@Override
boolean testBodyUrlencoded() {
false
}
}
class AkkaHttpServerInstrumentationSyncV0ForkedTest extends AkkaHttpServerInstrumentationSyncTest {
@Override
int version() {
return 0
}
@Override
String service() {
return null
}
@Override
String operation() {
return "akka-http.request"
}
}
class AkkaHttpServerInstrumentationSyncV1ForkedTest extends AkkaHttpServerInstrumentationSyncTest implements TestingGenericHttpNamingConventions.ServerV1 {
}
class AkkaHttpServerInstrumentationAsyncTest extends AkkaHttpServerInstrumentationTest {
@Override
HttpServer server() {
return new AkkaHttpTestWebServer(AkkaHttpTestWebServer.BindAndHandleAsync())
}
}
class AkkaHttpServerInstrumentationBindAndHandleTest extends AkkaHttpServerInstrumentationTest {
String akkaHttpVersion
@Override
HttpServer server() {
AkkaHttpTestWebServer server = new AkkaHttpTestWebServer(AkkaHttpTestWebServer.BindAndHandle())
akkaHttpVersion = server.system().settings().config().getString('akka.http.version')
server
}
@Override
boolean redirectHasBody() {
return true
}
// StrictForm marshaller rejects with providing a Rejection
// We can't detect the BlockingException as a consequence
@Override
boolean testBodyMultipart() {
akkaHttpVersion != '10.0.10'
}
@Override
boolean testBodyFilenames() {
akkaHttpVersion != '10.0.10'
}
@Override
boolean testBodyUrlencoded() {
akkaHttpVersion != '10.0.10'
}
}
class AkkaHttpServerInstrumentationBindAndHandleAsyncWithRouteAsyncHandlerTest extends AkkaHttpServerInstrumentationTest {
String akkaHttpVersion
@Override
HttpServer server() {
AkkaHttpTestWebServer server = new AkkaHttpTestWebServer(AkkaHttpTestWebServer.BindAndHandleAsyncWithRouteAsyncHandler())
akkaHttpVersion = server.system().settings().config().getString('akka.http.version')
server
}
@Override
boolean redirectHasBody() {
return true
}
@Override
boolean testBodyMultipart() {
akkaHttpVersion != '10.0.10'
}
@Override
boolean testBodyFilenames() {
akkaHttpVersion != '10.0.10'
}
@Override
boolean testBodyUrlencoded() {
akkaHttpVersion != '10.0.10'
}
}
class AkkaHttpServerInstrumentationAsyncHttp2Test extends AkkaHttpServerInstrumentationTest {
@Override
HttpServer server() {
return new AkkaHttpTestWebServer(AkkaHttpTestWebServer.BindAndHandleAsyncHttp2())
}
}