Skip to content

Commit 9ee91c4

Browse files
committed
OtelModule: cover Java agent class detection in tests
1 parent 56276d1 commit 9ee91c4

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,13 @@ private boolean isRuntimeTelemetryEnabled(Jooby application) {
200200
* duplicate every JVM metric.
201201
*/
202202
boolean isJavaAgentPresent() {
203+
// Agent classes live in the bootstrap class loader, visible from any class loader.
204+
return isClassPresent("io.opentelemetry.javaagent.bootstrap.AgentInitializer");
205+
}
206+
207+
boolean isClassPresent(String className) {
203208
try {
204-
// Agent classes live in the bootstrap class loader, visible from any class loader.
205-
Class.forName(
206-
"io.opentelemetry.javaagent.bootstrap.AgentInitializer",
207-
false,
208-
getClass().getClassLoader());
209+
Class.forName(className, false, getClass().getClassLoader());
209210
return true;
210211
} catch (ClassNotFoundException | LinkageError e) {
211212
return false;

‎modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.jupiter.api.Assertions.assertFalse;
99
import static org.junit.jupiter.api.Assertions.assertNotNull;
1010
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
1112
import static org.mockito.ArgumentMatchers.any;
1213
import static org.mockito.ArgumentMatchers.anyString;
1314
import static org.mockito.ArgumentMatchers.eq;
@@ -139,6 +140,13 @@ void shouldDetectMissingJavaAgent() {
139140
assertFalse(new OtelModule().isJavaAgentPresent());
140141
}
141142

143+
@Test
144+
void shouldDetectPresentClass() {
145+
OtelModule module = new OtelModule();
146+
assertTrue(module.isClassPresent(OtelModule.class.getName()));
147+
assertFalse(module.isClassPresent("io.jooby.opentelemetry.DoesNotExist"));
148+
}
149+
142150
@Test
143151
void shouldEvaluateIsRunningInJoobyRunLocally() {
144152
OtelModule module = new OtelModule();

0 commit comments

Comments
 (0)