Skip to content

Commit b107cd6

Browse files
jknackclaude
andauthored
OtelModule: honor otel.instrumentation.runtime-telemetry.enabled (#4050)
* OtelModule: honor otel.instrumentation.runtime-telemetry.enabled RuntimeTelemetry was always created, so JVM metrics could not be muted and duplicated those of the OpenTelemetry Java agent. Skip it when the standard property is false (system property, env var or otel config). Fixes #4047 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> * 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> * OtelModule: read runtime-telemetry toggle from application config only * OtelModule: cover Java agent class detection in tests --------- Signed-off-by: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2 parents 880aeac + 9ee91c4 commit b107cd6

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

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

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

164-
application.onStop(RuntimeTelemetry.create(otel));
164+
if (isRuntimeTelemetryEnabled(application) && !isJavaAgentPresent()) {
165+
application.onStop(RuntimeTelemetry.create(otel));
166+
}
165167
var services = application.getServices();
166168
services.put(OpenTelemetry.class, otel);
167169
services.put(Tracer.class, tracer);
@@ -176,6 +178,41 @@ private static Provider<Trace> trace(Tracer tracer) {
176178
return () -> new Trace(tracer);
177179
}
178180

181+
private static final String RUNTIME_TELEMETRY_ENABLED =
182+
"otel.instrumentation.runtime-telemetry.enabled";
183+
184+
/**
185+
* JVM runtime metrics are enabled by default. They can be disabled with the standard
186+
* OpenTelemetry property {@code otel.instrumentation.runtime-telemetry.enabled=false}. System
187+
* properties and environment variables are already merged into the application configuration, so
188+
* it is read from there only.
189+
*/
190+
private boolean isRuntimeTelemetryEnabled(Jooby application) {
191+
var appConfig = application.getConfig();
192+
return appConfig == null
193+
|| !appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED)
194+
|| appConfig.getBoolean(RUNTIME_TELEMETRY_ENABLED);
195+
}
196+
197+
/**
198+
* The OpenTelemetry Java agent already registers JVM runtime metrics (unless disabled with {@code
199+
* otel.instrumentation.runtime-telemetry.enabled=false}), so registering them again would
200+
* duplicate every JVM metric.
201+
*/
202+
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) {
208+
try {
209+
Class.forName(className, false, getClass().getClassLoader());
210+
return true;
211+
} catch (ClassNotFoundException | LinkageError e) {
212+
return false;
213+
}
214+
}
215+
179216
boolean isRunningInJoobyRun() {
180217
return getClass()
181218
.getClassLoader()

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

Lines changed: 64 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;
@@ -83,6 +84,69 @@ void shouldRegisterProvidedOpenTelemetryInstance() {
8384
assertNotNull(providerCaptor.getValue().get());
8485
}
8586

87+
@Test
88+
void shouldRegisterRuntimeTelemetryByDefault() {
89+
OtelModule module = new OtelModule(OpenTelemetry.noop());
90+
91+
module.install(application);
92+
93+
verify(application).onStop(any(AutoCloseable.class));
94+
}
95+
96+
@Test
97+
void shouldSkipRuntimeTelemetryWhenDisabledByConfig() {
98+
when(application.getConfig()).thenReturn(rootConfig);
99+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
100+
when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false);
101+
102+
new OtelModule(OpenTelemetry.noop()).install(application);
103+
104+
verify(application, never()).onStop(any(AutoCloseable.class));
105+
}
106+
107+
@Test
108+
void shouldRegisterRuntimeTelemetryWhenEnabledByConfig() {
109+
when(application.getConfig()).thenReturn(rootConfig);
110+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
111+
when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
112+
113+
new OtelModule(OpenTelemetry.noop()).install(application);
114+
115+
verify(application).onStop(any(AutoCloseable.class));
116+
}
117+
118+
@Test
119+
void shouldRegisterRuntimeTelemetryWhenConfigHasNoSetting() {
120+
when(application.getConfig()).thenReturn(rootConfig);
121+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false);
122+
123+
new OtelModule(OpenTelemetry.noop()).install(application);
124+
125+
verify(application).onStop(any(AutoCloseable.class));
126+
}
127+
128+
@Test
129+
void shouldSkipRuntimeTelemetryWhenJavaAgentIsPresent() {
130+
OtelModule module = spy(new OtelModule(OpenTelemetry.noop()));
131+
doReturn(true).when(module).isJavaAgentPresent();
132+
133+
module.install(application);
134+
135+
verify(application, never()).onStop(any(AutoCloseable.class));
136+
}
137+
138+
@Test
139+
void shouldDetectMissingJavaAgent() {
140+
assertFalse(new OtelModule().isJavaAgentPresent());
141+
}
142+
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+
86150
@Test
87151
void shouldEvaluateIsRunningInJoobyRunLocally() {
88152
OtelModule module = new OtelModule();

0 commit comments

Comments
 (0)