-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathOpenTracing32Test.groovy
More file actions
415 lines (345 loc) · 12.4 KB
/
OpenTracing32Test.groovy
File metadata and controls
415 lines (345 loc) · 12.4 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTags
import datadog.trace.api.DDTraceId
import datadog.trace.api.internal.util.LongStringUtils
import datadog.trace.api.interceptor.MutableSpan
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities
import datadog.trace.context.TraceScope
import datadog.trace.core.DDSpan
import datadog.trace.core.propagation.ExtractedContext
import datadog.trace.core.propagation.PropagationTags
import datadog.trace.instrumentation.opentracing.DefaultLogHandler
import datadog.trace.instrumentation.opentracing32.OTTracer
import datadog.trace.instrumentation.opentracing32.TypeConverter
import io.opentracing.References
import io.opentracing.Scope
import io.opentracing.Span
import io.opentracing.log.Fields
import io.opentracing.noop.NoopSpan
import io.opentracing.propagation.Format
import io.opentracing.propagation.TextMap
import io.opentracing.util.GlobalTracer
import spock.lang.Shared
import spock.lang.Subject
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
import static datadog.trace.api.TracePropagationStyle.NONE
import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_DROP
import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP
import static datadog.trace.api.sampling.PrioritySampling.UNSET
import static datadog.trace.api.sampling.PrioritySampling.USER_DROP
import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP
import static datadog.trace.api.sampling.SamplingMechanism.AGENT_RATE
import static datadog.trace.api.sampling.SamplingMechanism.DEFAULT
import static datadog.trace.api.sampling.SamplingMechanism.MANUAL
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation
class OpenTracing32Test extends InstrumentationSpecification {
@Subject
def tracer = GlobalTracer.get()
@Shared
TypeConverter typeConverter = new TypeConverter(new DefaultLogHandler())
def "test #method"() {
setup:
def builder = tracer.buildSpan("some name")
if (tagBuilder) {
builder.withTag(DDTags.RESOURCE_NAME, "some resource")
.withTag("string", "a")
.withTag("number", 1)
.withTag("boolean", true)
}
if (addReference) {
def ctx = new ExtractedContext(DDTraceId.ONE, 2, SAMPLER_DROP, null, PropagationTags.factory().empty(), NONE)
builder.addReference(addReference, tracer.tracer.converter.toSpanContext(ctx))
}
def result = builder.start()
if (tagSpan) {
result.setTag(DDTags.RESOURCE_NAME, "other resource")
.setTag("string", "b")
.setTag("number", 2)
.setTag("boolean", false)
}
if (exception) {
result.log([(Fields.ERROR_OBJECT): exception])
}
expect:
result instanceof MutableSpan
(result as MutableSpan).localRootSpan.delegate == result.delegate
(result as MutableSpan).isError() == (exception != null)
tracer.activeSpan() == null
result.context().baggageItems().isEmpty()
when:
result.setBaggageItem("test", "baggage")
then:
result.getBaggageItem("test") == "baggage"
result.context().baggageItems() == ["test": "baggage"].entrySet()
when:
result.finish()
then:
assertTraces(1) {
trace(1) {
span {
if ([References.CHILD_OF, References.FOLLOWS_FROM].contains(addReference)) {
parentSpanId(2)
} else {
parent()
}
operationName "some name"
if (tagSpan) {
resourceName "other resource"
} else if (tagBuilder) {
resourceName "some resource"
} else {
resourceName "some name"
}
errored exception != null
tags {
if (tagSpan) {
"string" "b"
"number" 2
"boolean" false
} else if (tagBuilder) {
"string" "a"
"number" 1
"boolean" true
}
if (exception) {
errorTags(exception.class)
}
defaultTags(addReference != null)
}
assert span.context().integrationName == "opentracing"
}
}
}
where:
method | addReference | tagBuilder | tagSpan | exception
"start" | null | true | false | null
"startManual" | References.CHILD_OF | true | true | new Exception()
"startManual" | "bogus" | false | false | new Exception()
"start" | References.FOLLOWS_FROM | false | true | null
}
def "test ignoreParent"() {
setup:
def otherSpan = runUnderTrace("parent") {
tracer.buildSpan("other").ignoreActiveSpan().start()
}
expect:
otherSpan.operationName == "other"
(otherSpan.delegate as DDSpan).parentId == DDSpanId.ZERO
}
def "test startActive"() {
setup:
def scope = tracer.buildSpan("some name").startActive(finishSpan)
expect:
scope instanceof TraceScope
tracer.activeSpan().delegate == scope.span().delegate
when:
scope.close()
then:
(scope.span().delegate as DDSpan).isFinished() == finishSpan
cleanup:
if (finishSpan) {
TEST_WRITER.waitForTraces(1)
}
where:
finishSpan << [true, false]
}
def "test scopemanager"() {
setup:
AgentTracer.TracerAPI internalTracer = tracer.tracer.tracer
def span = tracer.buildSpan("some name").start()
def scope = tracer.scopeManager().activate(span, finishSpan)
internalTracer.setAsyncPropagationEnabled(false)
expect:
span instanceof MutableSpan
scope instanceof TraceScope
!internalTracer.isAsyncPropagationEnabled()
(scope as TraceScope).capture() == noopContinuation()
(tracer.scopeManager().active().span().delegate == span.delegate)
when:
internalTracer.setAsyncPropagationEnabled(true)
def continuation = (scope as TraceScope).capture()
continuation.cancel()
then:
internalTracer.isAsyncPropagationEnabled()
continuation instanceof TraceScope.Continuation
when: "attempting to close the span this way doesn't work because we lost the 'finishSpan' reference"
tracer.scopeManager().active().close()
then:
!(span.delegate as DDSpan).isFinished()
when:
scope.close()
then:
(span.delegate as DDSpan).isFinished() == finishSpan
cleanup:
if (finishSpan) {
TEST_WRITER.waitForTraces(1)
}
where:
finishSpan | _
true | _
false | _
}
def "test scopemanager with non OTSpan"() {
setup:
def span = NoopSpan.INSTANCE
def scope = tracer.scopeManager().activate(span, true)
expect:
!(span instanceof MutableSpan)
scope instanceof TraceScope
and: "non OTSpans aren't supported and get converted to NoopAgentSpan"
tracer.scopeManager().active().span() != span
when:
scope.close()
scope.span().finish()
then:
assertTraces(0) {}
}
def "test continuation"() {
setup:
def span = tracer.buildSpan("some name").start()
TraceScope scope = tracer.scopeManager().activate(span, false)
expect:
tracer.activeSpan().delegate == span.delegate
when:
def continuation = scope.capture()
then:
continuation instanceof TraceScope.Continuation
when:
scope.close()
then:
tracer.activeSpan() == null
when:
scope = continuation.activate()
then:
tracer.activeSpan().delegate == span.delegate
cleanup:
scope.close()
}
def "closing scope when not on top"() {
when:
Span firstSpan = tracer.buildSpan("someOperation").start()
Scope firstScope = tracer.scopeManager().activate(firstSpan)
Span secondSpan = tracer.buildSpan("someOperation").start()
Scope secondScope = tracer.scopeManager().activate(secondSpan)
firstSpan.finish()
firstScope.close()
then:
tracer.scopeManager().active().span() == secondScope.span()
_ * TEST_PROFILING_CONTEXT_INTEGRATION._
0 * _
when:
secondSpan.finish()
secondScope.close()
then:
assert tracer.scopeManager().active() == null
}
def "test inject extract"() {
setup:
def context = tracer.buildSpan("some name").start().context()
def textMap = [:]
def adapter = new TextMapAdapter(textMap)
when:
context.delegate.setSamplingPriority(contextPriority, samplingMechanism)
tracer.inject(context, Format.Builtin.TEXT_MAP, adapter)
then:
def expectedTraceparent = "00-${context.delegate.traceId.toHexStringPadded(32)}" +
"-${DDSpanId.toHexStringPadded(context.delegate.spanId)}" +
"-" + (propagatedPriority > 0 ? "01" : "00")
def expectedTracestate = "dd=s:${propagatedPriority};p:${DDSpanId.toHexStringPadded(context.delegate.spanId)}"
def datadogTags = []
if (propagatedPriority > 0) {
def effectiveSamplingMechanism = contextPriority == UNSET ? AGENT_RATE : samplingMechanism
datadogTags << "_dd.p.dm=-" + effectiveSamplingMechanism
expectedTracestate+= ";t.dm:-" + effectiveSamplingMechanism
}
def traceId = context.delegate.traceId as DDTraceId
if (traceId.toHighOrderLong() != 0) {
expectedTracestate+= ";t.tid:${traceId.toHexStringPadded(32).substring(0, 16)}"
datadogTags << "_dd.p.tid=" + LongStringUtils.toHexStringPadded(traceId.toHighOrderLong(), 16)
}
if (contextPriority == UNSET) {
expectedTracestate+= ";t.ksr:1"
datadogTags << "_dd.p.ksr=1"
}
def expectedTextMap = [
"x-datadog-trace-id" : "$context.delegate.traceId",
"x-datadog-parent-id" : "$context.delegate.spanId",
"x-datadog-sampling-priority": propagatedPriority.toString(),
"traceparent" : expectedTraceparent,
"tracestate" : expectedTracestate
]
if (!datadogTags.empty) {
expectedTextMap.put("x-datadog-tags", datadogTags.join(','))
}
textMap == expectedTextMap
when:
def extract = tracer.extract(Format.Builtin.TEXT_MAP, adapter)
then:
extract.delegate.traceId == context.delegate.traceId
extract.delegate.spanId == context.delegate.spanId
extract.delegate.samplingPriority == propagatedPriority
where:
contextPriority | samplingMechanism | propagatedPriority
SAMPLER_DROP | DEFAULT | SAMPLER_DROP
SAMPLER_KEEP | DEFAULT | SAMPLER_KEEP
UNSET | DEFAULT | SAMPLER_KEEP
USER_KEEP | MANUAL | USER_KEEP
USER_DROP | MANUAL | USER_DROP
}
def "tolerate null span activation"() {
when:
try {
tracer.scopeManager().activate(null)?.close()
} catch (Exception ignored) {}
try {
tracer.activateSpan(null)?.close()
} catch (Exception ignored) {}
// make sure scope stack has been left in a valid state
Span testSpan = tracer.buildSpan("someOperation").start()
Scope testScope = tracer.scopeManager().activate(testSpan)
testSpan.finish()
testScope.close()
then:
assert tracer.scopeManager().active() == null
}
def "test resource name assignment through MutableSpan casting"() {
given:
OTTracer.OTSpanBuilder builder = tracer.buildSpan("parent") as OTTracer.OTSpanBuilder
builder.delegate.withResourceName("test-resource")
Span testSpan = builder.start()
Scope testScope = tracer.activateSpan(testSpan)
when:
Span active = GlobalTracer.get().activeSpan()
Span child = GlobalTracer.get().buildSpan("child").asChildOf(active).start()
Scope scope = GlobalTracer.get().activateSpan(child)
MutableSpan localRootSpan = ((MutableSpan) child).getLocalRootSpan()
localRootSpan.setResourceName("correct-resource")
then:
typeConverter.toAgentSpan(testSpan).getResourceName() == "correct-resource"
when:
typeConverter.toAgentSpan(testSpan).setResourceName("should-be-ignored", ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE)
then:
typeConverter.toAgentSpan(testSpan).getResourceName() == "correct-resource"
cleanup:
scope.close()
child.finish()
testScope.close()
testSpan.finish()
}
static class TextMapAdapter implements TextMap {
private final Map<String, String> map
TextMapAdapter(Map<String, String> map) {
this.map = map
}
@Override
Iterator<Map.Entry<String, String>> iterator() {
return map.entrySet().iterator()
}
@Override
void put(String key, String value) {
map.put(key, value)
}
}
}