-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathBootstrapInitializationTelemetryTest.groovy
More file actions
124 lines (97 loc) · 3.7 KB
/
BootstrapInitializationTelemetryTest.groovy
File metadata and controls
124 lines (97 loc) · 3.7 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
package datadog.trace.bootstrap
import spock.lang.Specification
import static java.nio.charset.StandardCharsets.UTF_8
class BootstrapInitializationTelemetryTest extends Specification {
def initTelemetry, capture
def setup() {
def capture = new Capture()
def initTelemetry = new BootstrapInitializationTelemetry.JsonBased(capture)
// There's an annoying interaction between our bootstrap injection
// and the GroovyClassLoader class resolution. Groovy resolves the import
// against the application ClassLoader, but when a method invocation
// happens it resolves the invocation against the bootstrap classloader.
// To side step this problem, put a Groovy Proxy around the object under test
// codeNarc was incorrectly flagging "import groovy.util.Proxy" as unnecessary,
// since groovy.util is imported implicitly. However, java.util is also
// implicitly imported and also contains a Proxy class, so need to use the
// full name inline to disambiguate and pass codeNarc.
def initTelemetryProxy = new groovy.util.Proxy()
initTelemetryProxy.setAdaptee(initTelemetry)
this.initTelemetry = initTelemetryProxy
this.initTelemetry.initMetaInfo("runtime_name", "java")
this.initTelemetry.initMetaInfo("runtime_version", "1.8.0_382")
this.capture = capture
}
def "test success"() {
when:
initTelemetry.finish()
then:
capture.json() == json("success", "success", "Successfully configured ddtrace package",
[[name: "library_entrypoint.complete"]])
}
def "real example"() {
when:
initTelemetry.onError(new Exception("foo"))
initTelemetry.finish()
then:
capture.json() == json("error", "internal_error", "foo", [
[name: "library_entrypoint.error", tags: ["error_type:java.lang.Exception"]],
[name: "library_entrypoint.complete"]
])
}
def "test abort"() {
when:
initTelemetry.onAbort(reasonCode)
initTelemetry.finish()
then:
capture.json() == json("abort", resultClass, reasonCode,
[[name: "library_entrypoint.abort", tags: ["reason:${reasonCode}"]]])
where:
reasonCode | resultClass
"jdk_tool" | "unsupported_binary"
"already_initialized" | "already_instrumented"
"other-java-agents" | "incompatible_library"
"foo" | "unknown"
}
def "trivial completion check"() {
when:
initTelemetry.finish()
then:
capture.json().contains("library_entrypoint.complete")
}
def "trivial incomplete check"() {
when:
initTelemetry.markIncomplete()
initTelemetry.finish()
then:
!capture.json().contains("library_entrypoint.complete")
}
def "incomplete on fatal error"() {
when:
initTelemetry.onFatalError(new Exception("foo"))
initTelemetry.finish()
then:
!capture.json().contains("library_entrypoint.complete")
capture.json() == json("error", "internal_error", "foo",
[[name: "library_entrypoint.error", tags: ["error_type:java.lang.Exception"]]])
}
def "incomplete on abort"() {
when:
initTelemetry.onAbort("reason")
initTelemetry.finish()
then:
!capture.json().contains("library_entrypoint.complete")
}
private String json(String result, String resultClass, String resultReason, List points) {
return """{"metadata":{"runtime_name":"java","runtime_version":"1.8.0_382","result":"${result}","result_class":"${resultClass}","result_reason":"${resultReason}"},"points":${new groovy.json.JsonBuilder(points)}}"""
}
static class Capture implements BootstrapInitializationTelemetry.JsonSender {
String json
void send(byte[] payload) {
this.json = new String(payload, UTF_8)
}
String json() {
return this.json
}
}
}