-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathAgentTestRunnerTest.groovy
More file actions
187 lines (164 loc) · 6.35 KB
/
AgentTestRunnerTest.groovy
File metadata and controls
187 lines (164 loc) · 6.35 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
import com.google.common.reflect.ClassPath
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.config.inversion.ConfigHelper
import datadog.trace.agent.test.BootstrapClasspathSetupListener
import datadog.trace.api.GlobalTracer
import datadog.trace.api.Platform
import datadog.trace.bootstrap.Constants
import datadog.trace.bootstrap.instrumentation.api.AgentScope
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import spock.lang.Shared
import java.util.concurrent.TimeoutException
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
import static datadog.trace.api.config.TraceInstrumentationConfig.TRACE_CLASSES_EXCLUDE
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan
class AgentTestRunnerTest extends InstrumentationSpecification {
private static final ClassLoader BOOTSTRAP_CLASSLOADER = null
private static final BigDecimal JAVA_VERSION = new BigDecimal(System.getProperty("java.specification.version"))
private static final boolean IS_AT_LEAST_JAVA_17 = JAVA_VERSION.isAtLeast(17.0)
@Shared
private Class sharedSpanClass
@Override
void configurePreAgent() {
super.configurePreAgent()
// Opt out of strict config validation - test module loads test instrumentations with fake names
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST)
injectSysConfig(TRACE_CLASSES_EXCLUDE, "config.exclude.packagename.*, config.exclude.SomeClass,config.exclude.SomeClass\$NestedClass")
}
def setupSpec() {
sharedSpanClass = AgentSpan
}
def "spock runner bootstrap prefixes correct for test setup"() {
expect:
BootstrapClasspathSetupListener.BOOTSTRAP_PACKAGE_PREFIXES_COPY == Constants.BOOTSTRAP_PACKAGE_PREFIXES
}
def "classpath setup"() {
setup:
boolean jfrSupported = isJFRSupported()
final List<String> bootstrapClassesIncorrectlyLoaded = []
for (ClassPath.ClassInfo info : BootstrapClasspathSetupListener.TEST_CLASSPATH.getAllClasses()) {
for (int i = 0; i < Constants.BOOTSTRAP_PACKAGE_PREFIXES.length; ++i) {
if (info.getName().startsWith(Constants.BOOTSTRAP_PACKAGE_PREFIXES[i])) {
if (!jfrSupported && info.name.startsWith("datadog.trace.bootstrap.instrumentation.jfr.")) {
continue // skip exception-profiling classes - they won't load if JFR is not available
}
try {
Class<?> bootstrapClass = Class.forName(info.getName())
if (bootstrapClass.getClassLoader() != BOOTSTRAP_CLASSLOADER) {
bootstrapClassesIncorrectlyLoaded.add(bootstrapClass)
}
break
} catch (UnsupportedClassVersionError e) {
// A dirty hack to allow passing this test on Java 7
if (info.getName().startsWith("datadog.trace.api.sampling.")) {
// The rate limiting sampler support is consciously compiled to Java 8 bytecode
// The sampler will not be used unless JFR is available -> running on Java 8+
// Simply ignore the error as the class will not be even attempted to get loaded on Java 7
break
}
if (info.getName().startsWith("datadog.trace.util.stacktrace.")) {
//It is known that support for Java 7 is going to be discontinued
//so we have decided to implement everything related to IAST in java8
break
}
// rethrow the exception otherwise
throw e
} catch (IllegalAccessError e) {
// A dirty hack to allow passing this test on Java 17
if (IS_AT_LEAST_JAVA_17 && info.getName() == "datadog.trace.bootstrap.instrumentation.rmi.ContextDispatcher") {
// The ContextDispatcher class implements a public interface sun.rmi.server.Dispatcher which
// is in a module that is not open under Java 17+
break
}
// rethrow the exception otherwise
throw e
} catch (NoClassDefFoundError e) {
// A dirty hack to allow passing this test on Java 7
if (info.getName() == "sun.misc.SharedSecrets") {
//datadog.trace.util.stacktrace.HotSpotStackWalker uses sun.misc.SharedSecrets to improve performance in jdk8 with hotspot
break
}
// rethrow the exception otherwise
}
}
}
}
expect:
sharedSpanClass.getClassLoader() == BOOTSTRAP_CLASSLOADER
AgentTracer.getClassLoader() == BOOTSTRAP_CLASSLOADER
TEST_TRACER == AgentTracer.get()
AgentTracer.get() == GlobalTracer.get()
bootstrapClassesIncorrectlyLoaded == []
}
def "waiting for child spans times out"() {
when:
runUnderTrace("parent") {
blockUntilChildSpansFinished(1)
}
then:
thrown(TimeoutException)
}
def "waiting for noop span returns immediately"() {
when:
AgentScope scope
runUnderTrace("parent") {
scope = TEST_TRACER.activateManualSpan(noopSpan())
blockUntilChildSpansFinished(1)
}
then:
noExceptionThrown()
cleanup:
scope?.close()
}
def "logging works"() {
when:
org.slf4j.LoggerFactory.getLogger(AgentTestRunnerTest).debug("hello")
then:
noExceptionThrown()
}
def "excluded classes are not instrumented #iterationIndex"() {
when:
runUnderTrace("parent") {
subject.run()
}
then:
!TRANSFORMED_CLASSES_NAMES.contains(subject.class.name)
assertTraces(1) {
trace(1) {
span {
operationName "parent"
}
}
}
where:
subject | _
new config.exclude.SomeClass() | _
new config.exclude.SomeClass.NestedClass() | _
new config.exclude.packagename.SomeClass() | _
new config.exclude.packagename.SomeClass.NestedClass() | _
}
def "test unblocked by completed span"() {
setup:
runUnderTrace("parent") {
runUnderTrace("child") {}
blockUntilChildSpansFinished(1)
}
expect:
assertTraces(1) {
trace(2) {
span {
operationName "parent"
parent()
}
span {
operationName "child"
childOf(span(0))
}
}
}
}
boolean isJFRSupported() {
return Platform.hasJfr()
}
}