Skip to content

Commit caab64d

Browse files
ARAYN7011claude
andcommitted
OtelModule: skip RuntimeTelemetry when the OpenTelemetry Java agent is present
The agent already registers JVM runtime metrics, so installing the module duplicated every jvm.* metric (refs #4047). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 2074c9e commit caab64d

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void install(Jooby application) {
161161
}
162162
var tracer = otel.getTracer("io.jooby.opentelemetry");
163163

164-
if (isRuntimeTelemetryEnabled(application)) {
164+
if (isRuntimeTelemetryEnabled(application) && !isJavaAgentPresent()) {
165165
application.onStop(RuntimeTelemetry.create(otel));
166166
}
167167
var services = application.getServices();
@@ -201,6 +201,24 @@ private boolean isRuntimeTelemetryEnabled(Jooby application) {
201201
return value == null || Boolean.parseBoolean(value.trim());
202202
}
203203

204+
/**
205+
* The OpenTelemetry Java agent already registers JVM runtime metrics (unless disabled with {@code
206+
* otel.instrumentation.runtime-telemetry.enabled=false}), so registering them again would
207+
* duplicate every JVM metric.
208+
*/
209+
boolean isJavaAgentPresent() {
210+
try {
211+
// Agent classes live in the bootstrap class loader, visible from any class loader.
212+
Class.forName(
213+
"io.opentelemetry.javaagent.bootstrap.AgentInitializer",
214+
false,
215+
getClass().getClassLoader());
216+
return true;
217+
} catch (ClassNotFoundException | LinkageError e) {
218+
return false;
219+
}
220+
}
221+
204222
boolean isRunningInJoobyRun() {
205223
return getClass()
206224
.getClassLoader()

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ void shouldSkipRuntimeTelemetryWhenDisabledByConfig() {
116116
verify(application, never()).onStop(any(AutoCloseable.class));
117117
}
118118

119+
@Test
120+
void shouldSkipRuntimeTelemetryWhenJavaAgentIsPresent() {
121+
OtelModule module = spy(new OtelModule(OpenTelemetry.noop()));
122+
doReturn(true).when(module).isJavaAgentPresent();
123+
124+
module.install(application);
125+
126+
verify(application, never()).onStop(any(AutoCloseable.class));
127+
}
128+
129+
@Test
130+
void shouldDetectMissingJavaAgent() {
131+
assertFalse(new OtelModule().isJavaAgentPresent());
132+
}
133+
119134
@Test
120135
void shouldEvaluateIsRunningInJoobyRunLocally() {
121136
OtelModule module = new OtelModule();

0 commit comments

Comments
 (0)