@@ -77,12 +77,12 @@ only depend on the OpenTelemetry API, `instrumentation-common`, and the instrume
77
77
[ instrumentation-library.gradle] ( ../../gradle/instrumentation-library.gradle ) needs to be applied to
78
78
configure build tooling for the library.
79
79
80
- ## Writing unit tests
80
+ ## Writing instrumentation tests
81
81
82
- Once the instrumentation is completed, we add unit tests to the ` testing ` module. Tests will
82
+ Once the instrumentation is completed, we add tests to the ` testing ` module. Tests will
83
83
generally apply to both library and agent instrumentation, with the only difference being how a client
84
84
or server is initialized. In a library test, there will be code calling into the instrumentation API,
85
- while in an agent test, it will generally just use the underlying library's API as is. Create unit tests in an
85
+ while in an agent test, it will generally just use the underlying library's API as is. Create tests in an
86
86
abstract class with an abstract method that returns an instrumented object like a client. The class
87
87
should itself extend from ` InstrumentationSpecification ` to be recognized by Spock and include helper
88
88
methods for assertions.
@@ -100,11 +100,11 @@ Now that we have working instrumentation, we can implement agent instrumentation
100
100
do not have to modify their apps to use it. Make sure the ` javaagent ` submodule has a dependency on the
101
101
` library ` submodule and a test dependency on the ` testing ` submodule. Agent instrumentation defines
102
102
classes to match against to generate bytecode for. You will often match against the class you used
103
- in the unit test for library instrumentation, for example the builder of a client. And then you could
103
+ in the test for library instrumentation, for example the builder of a client. And then you could
104
104
match against the method that creates the builder, for example its constructor. Agent instrumentation
105
105
can inject byte code to be run after the constructor returns, which would invoke e.g.,
106
106
` registerInterceptor ` and initialize the instrumentation. Often, the code inside the byte code
107
- decorator will be identical to the one in the unit test you wrote above - the agent does the work for
107
+ decorator will be identical to the one in the test you wrote above - the agent does the work for
108
108
initializing the instrumentation library, so a user doesn't have to.
109
109
110
110
With that written, let's add tests for the agent instrumentation. We basically want to ensure that
@@ -113,6 +113,34 @@ the base class you wrote earlier, but in this, create a client using none of the
113
113
only the ones offered by the library. Implement the ` AgentTestRunner ` trait for common setup logic,
114
114
and try running. All the tests should pass for agent instrumentation too.
115
115
116
+ Note that all the tests inside the ` javaagent ` module will be run using the shaded ` -javaagent `
117
+ in order to perform the same bytecode instrumentation as when the agent is run against a normal app.
118
+ This means that the javaagent instrumentation will be inside the javaagent (inside of the
119
+ ` AgentClassLoader ` ) and will not be directly accessible to your test code. See the next section in
120
+ case you need to write unit tests that directly access the javaagent instrumentation.
121
+
122
+ ## Writing Java agent unit tests
123
+
124
+ As mentioned above, tests in the ` javaagent ` module cannot access the javaagent instrumentation
125
+ classes directly.
126
+
127
+ Ideally javaagent instrumentation is just a thin wrapper over library instrumentation, and so there
128
+ is no need to write unit tests that directly access the javaagent instrumentation classes.
129
+
130
+ If you still want to write a unit test against javaagent instrumentation, add another module
131
+ named ` javaagent-unittests ` . Continuing with the example above:
132
+
133
+ ```
134
+ instrumentation ->
135
+ ...
136
+ yarpc-1.0 ->
137
+ javaagent
138
+ yarpc-1.0-javaagent.gradle
139
+ javaagent-unittest
140
+ yarpc-1.0-javaagent-unittest.gradle
141
+ ...
142
+ ```
143
+
116
144
### Java agent instrumentation gotchas
117
145
118
146
#### Calling Java 8 default methods from advice
0 commit comments