-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathOpenTelemetryTest.groovy
More file actions
359 lines (308 loc) · 9.69 KB
/
OpenTelemetryTest.groovy
File metadata and controls
359 lines (308 loc) · 9.69 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
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.interceptor.MutableSpan
import datadog.trace.core.propagation.PropagationTags
import static datadog.trace.api.TracePropagationStyle.NONE
import static datadog.trace.api.sampling.PrioritySampling.*
import static datadog.trace.api.sampling.SamplingMechanism.*
import datadog.trace.context.TraceScope
import datadog.trace.core.propagation.ExtractedContext
import io.grpc.Context
import io.opentelemetry.OpenTelemetry
import io.opentelemetry.context.Scope
import io.opentelemetry.context.propagation.HttpTextFormat
import io.opentelemetry.trace.Span
import io.opentelemetry.trace.Status
import io.opentelemetry.trace.TracingContextUtils
import spock.lang.Subject
class OpenTelemetryTest extends InstrumentationSpecification {
@Subject
def tracer = OpenTelemetry.tracerProvider.get("test-inst")
def httpPropagator = OpenTelemetry.getPropagators().httpTextFormat
@Override
void configurePreAgent() {
super.configurePreAgent()
injectSysConfig("dd.integration.opentelemetry-beta.enabled", "true")
}
def "test span tags"() {
setup:
def builder = tracer.spanBuilder("some name")
if (tagBuilder) {
builder.setAttribute(DDTags.RESOURCE_NAME, "some resource")
.setAttribute("string", "a")
.setAttribute("number", 1)
.setAttribute("boolean", true)
}
def result = builder.startSpan()
if (tagSpan) {
result.setAttribute(DDTags.RESOURCE_NAME, "other resource")
result.setAttribute("string", "b")
result.setAttribute("number", 2)
result.setAttribute("boolean", false)
}
expect:
result instanceof MutableSpan
(result as MutableSpan).localRootSpan == result.delegate
tracer.currentSpan == null
when:
result.end()
then:
assertTraces(1) {
trace(1) {
span {
parent()
operationName "test-inst"
if (tagSpan) {
resourceName "other resource"
} else if (tagBuilder) {
resourceName "some resource"
} else {
resourceName "some name"
}
errored false
tags {
if (tagSpan) {
"string" "b"
"number" 2
"boolean" false
} else if (tagBuilder) {
"string" "a"
"number" 1
"boolean" true
}
defaultTags()
}
assert span.context().integrationName == "otel"
}
}
}
where:
tagBuilder | tagSpan
true | false
true | true
false | false
false | true
}
def "test span exception"() {
setup:
def builder = tracer.spanBuilder("some name")
def result = builder.startSpan()
result.setStatus(Status.UNKNOWN)
result.setAttribute(DDTags.ERROR_MSG, (String) exception.message)
result.setAttribute(DDTags.ERROR_TYPE, (String) exception.class.name)
final StringWriter errorString = new StringWriter()
exception.printStackTrace(new PrintWriter(errorString))
result.setAttribute(DDTags.ERROR_STACK, errorString.toString())
expect:
result instanceof MutableSpan
(result as MutableSpan).localRootSpan == result.delegate
(result as MutableSpan).isError() == (exception != null)
tracer.currentSpan == null
when:
result.end()
then:
assertTraces(1) {
trace(1) {
span {
parent()
operationName "test-inst"
resourceName "some name"
errored true
tags {
errorTags(exception.class)
defaultTags()
}
}
}
}
where:
exception = new Exception()
}
def "test span links"() {
setup:
def builder = tracer.spanBuilder("some name")
if (parentId) {
def ctx = new ExtractedContext(DDTraceId.ONE, parentId, SAMPLER_DROP, null, PropagationTags.factory().empty(), NONE)
builder.setParent(tracer.converter.toSpanContext(ctx))
}
if (linkId) {
def ctx = new ExtractedContext(DDTraceId.ONE, linkId, SAMPLER_DROP, null, PropagationTags.factory().empty(), NONE)
builder.addLink(tracer.converter.toSpanContext(ctx))
}
def result = builder.startSpan()
expect:
result instanceof MutableSpan
(result as MutableSpan).localRootSpan == result.delegate
tracer.currentSpan == null
when:
result.end()
then:
assertTraces(1) {
trace(1) {
span {
if (expectedId) {
traceDDId(DDTraceId.ONE)
parentSpanId(expectedId)
} else {
parent()
}
operationName "test-inst"
resourceName "some name"
errored false
tags {
defaultTags(expectedId != null)
}
}
}
}
where:
parentId | linkId | expectedId
null | null | null
2 | null | 2
null | 3 | 3
2 | 3 | 2
}
def "test scope"() {
setup:
def span = tracer.spanBuilder("some name").startSpan()
def scope = tracer.withSpan(span)
expect:
scope instanceof TraceScope
tracer.currentSpan.delegate == scope.delegate.span()
when:
scope.close()
then:
tracer.currentSpan == null
cleanup:
span.end()
}
def "test closing scope when not on top"() {
when:
Span firstSpan = tracer.spanBuilder("someOperation").startSpan()
Scope firstScope = tracer.withSpan(firstSpan)
Span secondSpan = tracer.spanBuilder("someOperation").startSpan()
Scope secondScope = tracer.withSpan(secondSpan)
firstSpan.end()
firstScope.close()
then:
tracer.currentSpan.delegate == secondScope.delegate.span()
_ * TEST_PROFILING_CONTEXT_INTEGRATION._
0 * _
when:
secondSpan.end()
secondScope.close()
then:
tracer.currentSpan == null
_ * TEST_PROFILING_CONTEXT_INTEGRATION._
0 * _
}
def "test continuation"() {
setup:
def span = tracer.spanBuilder("some name").startSpan()
TraceScope scope = tracer.withSpan(span)
expect:
tracer.currentSpan.delegate == span.delegate
when:
def continuation = scope.capture()
then:
continuation instanceof TraceScope.Continuation
when:
scope.close()
then:
tracer.currentSpan == null
when:
scope = continuation.activate()
then:
tracer.currentSpan.delegate == span.delegate
cleanup:
scope.close()
span.end()
}
def "test inject extract"() {
setup:
def span = tracer.spanBuilder("some name").startSpan()
def context = TracingContextUtils.withSpan(span, Context.current())
def textMap = [:]
when:
span.delegate.samplingPriority = contextPriority
httpPropagator.inject(context, textMap, new TextMapSetter())
then:
def traceId = span.delegate.traceId as DDTraceId
def spanId = span.delegate.spanId
def expectedTraceparent = "00-${traceId.toHexStringPadded(32)}" +
"-${DDSpanId.toHexStringPadded(spanId)}" +
"-" + (propagatedPriority > 0 ? "01" : "00")
def expectedTracestate = "dd=s:${propagatedPriority};p:${DDSpanId.toHexStringPadded(spanId)}"
def datadogTags = []
if (propagatedMechanism != UNKNOWN) {
datadogTags << "_dd.p.dm=-" + propagatedMechanism
expectedTracestate += ";t.dm:-" + propagatedMechanism
}
if (traceId.toHighOrderLong() != 0) {
expectedTracestate += ";t.tid:${traceId.toHexStringPadded(32).substring(0, 16)}"
}
if (contextPriority == UNSET) {
expectedTracestate += ";t.ksr:1"
}
if (traceId.toHighOrderLong() != 0) {
datadogTags << "_dd.p.tid=" + traceId.toHexStringPadded(32).substring(0, 16)
}
if (contextPriority == UNSET) {
datadogTags << "_dd.p.ksr=1"
}
def expectedTextMap = [
"x-datadog-trace-id" : "$traceId",
"x-datadog-parent-id" : "$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 extractedContext = httpPropagator.extract(context, textMap, new TextMapGetter())
def extract = TracingContextUtils.getSpanWithoutDefault(extractedContext)
then:
extract.context.traceId == span.context.traceId
extract.context.spanId == span.context.spanId
extract.context.delegate.samplingPriority == propagatedPriority
cleanup:
span.end()
where:
contextPriority | propagatedPriority | propagatedMechanism
SAMPLER_DROP | SAMPLER_DROP | UNKNOWN
SAMPLER_KEEP | SAMPLER_KEEP | UNKNOWN
UNSET | SAMPLER_KEEP | AGENT_RATE
USER_KEEP | USER_KEEP | UNKNOWN
USER_DROP | USER_DROP | UNKNOWN
}
def "tolerate null span activation"() {
when:
try {
tracer.withSpan(null)?.close()
} catch (Exception ignored) {}
// make sure scope stack has been left in a valid state
def testSpan = tracer.spanBuilder("some name").startSpan()
def testScope = tracer.withSpan(testSpan)
testSpan.end()
testScope.close()
then:
tracer.currentSpan == null
}
static class TextMapGetter implements HttpTextFormat.Getter<Map<String, String>> {
@Override
String get(Map<String, String> carrier, String key) {
return carrier.get(key)
}
}
static class TextMapSetter implements HttpTextFormat.Setter<Map<String, String>> {
@Override
void set(Map<String, String> carrier, String key, String value) {
carrier.put(key, value)
}
}
}