Skip to content

Commit 2074c9e

Browse files
ARAYN7011claude
andcommitted
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>
1 parent 9c1d0ee commit 2074c9e

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

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

Lines changed: 26 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)) {
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,29 @@ 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}, either as
187+
* a system property, as the {@code OTEL_INSTRUMENTATION_RUNTIME_TELEMETRY_ENABLED} environment
188+
* variable or in the {@code otel} block of the application configuration.
189+
*/
190+
private boolean isRuntimeTelemetryEnabled(Jooby application) {
191+
var value = System.getProperty(RUNTIME_TELEMETRY_ENABLED);
192+
if (value == null) {
193+
value = System.getenv(RUNTIME_TELEMETRY_ENABLED.toUpperCase().replaceAll("[.-]", "_"));
194+
}
195+
if (value == null) {
196+
var appConfig = application.getConfig();
197+
if (appConfig != null && appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED)) {
198+
value = appConfig.getValue(RUNTIME_TELEMETRY_ENABLED).unwrapped().toString();
199+
}
200+
}
201+
return value == null || Boolean.parseBoolean(value.trim());
202+
}
203+
179204
boolean isRunningInJoobyRun() {
180205
return getClass()
181206
.getClassLoader()

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ void shouldRegisterProvidedOpenTelemetryInstance() {
8383
assertNotNull(providerCaptor.getValue().get());
8484
}
8585

86+
@Test
87+
void shouldRegisterRuntimeTelemetryByDefault() {
88+
OtelModule module = new OtelModule(OpenTelemetry.noop());
89+
90+
module.install(application);
91+
92+
verify(application).onStop(any(AutoCloseable.class));
93+
}
94+
95+
@Test
96+
void shouldSkipRuntimeTelemetryWhenDisabledBySystemProperty() {
97+
System.setProperty("otel.instrumentation.runtime-telemetry.enabled", "false");
98+
try {
99+
new OtelModule(OpenTelemetry.noop()).install(application);
100+
} finally {
101+
System.clearProperty("otel.instrumentation.runtime-telemetry.enabled");
102+
}
103+
104+
verify(application, never()).onStop(any(AutoCloseable.class));
105+
}
106+
107+
@Test
108+
void shouldSkipRuntimeTelemetryWhenDisabledByConfig() {
109+
when(application.getConfig()).thenReturn(rootConfig);
110+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
111+
when(rootConfig.getValue("otel.instrumentation.runtime-telemetry.enabled"))
112+
.thenReturn(ConfigValueFactory.fromAnyRef(false));
113+
114+
new OtelModule(OpenTelemetry.noop()).install(application);
115+
116+
verify(application, never()).onStop(any(AutoCloseable.class));
117+
}
118+
86119
@Test
87120
void shouldEvaluateIsRunningInJoobyRunLocally() {
88121
OtelModule module = new OtelModule();

0 commit comments

Comments
 (0)