From 2074c9e8be512369d8cd9dddd934cee54c171b2f Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:17:29 +0530 Subject: [PATCH 1/4] 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 Signed-off-by: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> --- .../io/jooby/opentelemetry/OtelModule.java | 27 ++++++++++++++- .../jooby/opentelemetry/OtelModuleTest.java | 33 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java index ab253373f2..7651b1780f 100644 --- a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java +++ b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java @@ -161,7 +161,9 @@ public void install(Jooby application) { } var tracer = otel.getTracer("io.jooby.opentelemetry"); - application.onStop(RuntimeTelemetry.create(otel)); + if (isRuntimeTelemetryEnabled(application)) { + application.onStop(RuntimeTelemetry.create(otel)); + } var services = application.getServices(); services.put(OpenTelemetry.class, otel); services.put(Tracer.class, tracer); @@ -176,6 +178,29 @@ private static Provider trace(Tracer tracer) { return () -> new Trace(tracer); } + private static final String RUNTIME_TELEMETRY_ENABLED = + "otel.instrumentation.runtime-telemetry.enabled"; + + /** + * JVM runtime metrics are enabled by default. They can be disabled with the standard + * OpenTelemetry property {@code otel.instrumentation.runtime-telemetry.enabled=false}, either as + * a system property, as the {@code OTEL_INSTRUMENTATION_RUNTIME_TELEMETRY_ENABLED} environment + * variable or in the {@code otel} block of the application configuration. + */ + private boolean isRuntimeTelemetryEnabled(Jooby application) { + var value = System.getProperty(RUNTIME_TELEMETRY_ENABLED); + if (value == null) { + value = System.getenv(RUNTIME_TELEMETRY_ENABLED.toUpperCase().replaceAll("[.-]", "_")); + } + if (value == null) { + var appConfig = application.getConfig(); + if (appConfig != null && appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED)) { + value = appConfig.getValue(RUNTIME_TELEMETRY_ENABLED).unwrapped().toString(); + } + } + return value == null || Boolean.parseBoolean(value.trim()); + } + boolean isRunningInJoobyRun() { return getClass() .getClassLoader() diff --git a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java index 51751542f7..20ca5df395 100644 --- a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java +++ b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java @@ -83,6 +83,39 @@ void shouldRegisterProvidedOpenTelemetryInstance() { assertNotNull(providerCaptor.getValue().get()); } + @Test + void shouldRegisterRuntimeTelemetryByDefault() { + OtelModule module = new OtelModule(OpenTelemetry.noop()); + + module.install(application); + + verify(application).onStop(any(AutoCloseable.class)); + } + + @Test + void shouldSkipRuntimeTelemetryWhenDisabledBySystemProperty() { + System.setProperty("otel.instrumentation.runtime-telemetry.enabled", "false"); + try { + new OtelModule(OpenTelemetry.noop()).install(application); + } finally { + System.clearProperty("otel.instrumentation.runtime-telemetry.enabled"); + } + + verify(application, never()).onStop(any(AutoCloseable.class)); + } + + @Test + void shouldSkipRuntimeTelemetryWhenDisabledByConfig() { + when(application.getConfig()).thenReturn(rootConfig); + when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); + when(rootConfig.getValue("otel.instrumentation.runtime-telemetry.enabled")) + .thenReturn(ConfigValueFactory.fromAnyRef(false)); + + new OtelModule(OpenTelemetry.noop()).install(application); + + verify(application, never()).onStop(any(AutoCloseable.class)); + } + @Test void shouldEvaluateIsRunningInJoobyRunLocally() { OtelModule module = new OtelModule(); From caab64dd4a202757b456aa3fc3fea8ff35a41caf Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:31:00 +0000 Subject: [PATCH 2/4] 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 --- .../io/jooby/opentelemetry/OtelModule.java | 20 ++++++++++++++++++- .../jooby/opentelemetry/OtelModuleTest.java | 15 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java index 7651b1780f..969b4a82b6 100644 --- a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java +++ b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java @@ -161,7 +161,7 @@ public void install(Jooby application) { } var tracer = otel.getTracer("io.jooby.opentelemetry"); - if (isRuntimeTelemetryEnabled(application)) { + if (isRuntimeTelemetryEnabled(application) && !isJavaAgentPresent()) { application.onStop(RuntimeTelemetry.create(otel)); } var services = application.getServices(); @@ -201,6 +201,24 @@ private boolean isRuntimeTelemetryEnabled(Jooby application) { return value == null || Boolean.parseBoolean(value.trim()); } + /** + * The OpenTelemetry Java agent already registers JVM runtime metrics (unless disabled with {@code + * otel.instrumentation.runtime-telemetry.enabled=false}), so registering them again would + * duplicate every JVM metric. + */ + boolean isJavaAgentPresent() { + try { + // Agent classes live in the bootstrap class loader, visible from any class loader. + Class.forName( + "io.opentelemetry.javaagent.bootstrap.AgentInitializer", + false, + getClass().getClassLoader()); + return true; + } catch (ClassNotFoundException | LinkageError e) { + return false; + } + } + boolean isRunningInJoobyRun() { return getClass() .getClassLoader() diff --git a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java index 20ca5df395..70e15d529e 100644 --- a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java +++ b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java @@ -116,6 +116,21 @@ void shouldSkipRuntimeTelemetryWhenDisabledByConfig() { verify(application, never()).onStop(any(AutoCloseable.class)); } + @Test + void shouldSkipRuntimeTelemetryWhenJavaAgentIsPresent() { + OtelModule module = spy(new OtelModule(OpenTelemetry.noop())); + doReturn(true).when(module).isJavaAgentPresent(); + + module.install(application); + + verify(application, never()).onStop(any(AutoCloseable.class)); + } + + @Test + void shouldDetectMissingJavaAgent() { + assertFalse(new OtelModule().isJavaAgentPresent()); + } + @Test void shouldEvaluateIsRunningInJoobyRunLocally() { OtelModule module = new OtelModule(); From 56276d17e2075b4de889ef8a9166c7eadbc9f3ad Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:49:54 +0530 Subject: [PATCH 3/4] OtelModule: read runtime-telemetry toggle from application config only --- .../io/jooby/opentelemetry/OtelModule.java | 21 +++++-------- .../jooby/opentelemetry/OtelModuleTest.java | 30 ++++++++++++------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java index 969b4a82b6..83c7ea0ad6 100644 --- a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java +++ b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java @@ -183,22 +183,15 @@ private static Provider trace(Tracer tracer) { /** * JVM runtime metrics are enabled by default. They can be disabled with the standard - * OpenTelemetry property {@code otel.instrumentation.runtime-telemetry.enabled=false}, either as - * a system property, as the {@code OTEL_INSTRUMENTATION_RUNTIME_TELEMETRY_ENABLED} environment - * variable or in the {@code otel} block of the application configuration. + * OpenTelemetry property {@code otel.instrumentation.runtime-telemetry.enabled=false}. System + * properties and environment variables are already merged into the application configuration, so + * it is read from there only. */ private boolean isRuntimeTelemetryEnabled(Jooby application) { - var value = System.getProperty(RUNTIME_TELEMETRY_ENABLED); - if (value == null) { - value = System.getenv(RUNTIME_TELEMETRY_ENABLED.toUpperCase().replaceAll("[.-]", "_")); - } - if (value == null) { - var appConfig = application.getConfig(); - if (appConfig != null && appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED)) { - value = appConfig.getValue(RUNTIME_TELEMETRY_ENABLED).unwrapped().toString(); - } - } - return value == null || Boolean.parseBoolean(value.trim()); + var appConfig = application.getConfig(); + return appConfig == null + || !appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED) + || appConfig.getBoolean(RUNTIME_TELEMETRY_ENABLED); } /** diff --git a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java index 70e15d529e..4400b96dda 100644 --- a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java +++ b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java @@ -93,27 +93,35 @@ void shouldRegisterRuntimeTelemetryByDefault() { } @Test - void shouldSkipRuntimeTelemetryWhenDisabledBySystemProperty() { - System.setProperty("otel.instrumentation.runtime-telemetry.enabled", "false"); - try { - new OtelModule(OpenTelemetry.noop()).install(application); - } finally { - System.clearProperty("otel.instrumentation.runtime-telemetry.enabled"); - } + void shouldSkipRuntimeTelemetryWhenDisabledByConfig() { + when(application.getConfig()).thenReturn(rootConfig); + when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); + when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false); + + new OtelModule(OpenTelemetry.noop()).install(application); verify(application, never()).onStop(any(AutoCloseable.class)); } @Test - void shouldSkipRuntimeTelemetryWhenDisabledByConfig() { + void shouldRegisterRuntimeTelemetryWhenEnabledByConfig() { when(application.getConfig()).thenReturn(rootConfig); when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); - when(rootConfig.getValue("otel.instrumentation.runtime-telemetry.enabled")) - .thenReturn(ConfigValueFactory.fromAnyRef(false)); + when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); new OtelModule(OpenTelemetry.noop()).install(application); - verify(application, never()).onStop(any(AutoCloseable.class)); + verify(application).onStop(any(AutoCloseable.class)); + } + + @Test + void shouldRegisterRuntimeTelemetryWhenConfigHasNoSetting() { + when(application.getConfig()).thenReturn(rootConfig); + when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false); + + new OtelModule(OpenTelemetry.noop()).install(application); + + verify(application).onStop(any(AutoCloseable.class)); } @Test From 9ee91c4ae288d7567e1c8f2a2a77156a625599ad Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:10:47 +0530 Subject: [PATCH 4/4] OtelModule: cover Java agent class detection in tests --- .../main/java/io/jooby/opentelemetry/OtelModule.java | 11 ++++++----- .../java/io/jooby/opentelemetry/OtelModuleTest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java index 83c7ea0ad6..35cf0938a5 100644 --- a/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java +++ b/modules/jooby-opentelemetry/src/main/java/io/jooby/opentelemetry/OtelModule.java @@ -200,12 +200,13 @@ private boolean isRuntimeTelemetryEnabled(Jooby application) { * duplicate every JVM metric. */ boolean isJavaAgentPresent() { + // Agent classes live in the bootstrap class loader, visible from any class loader. + return isClassPresent("io.opentelemetry.javaagent.bootstrap.AgentInitializer"); + } + + boolean isClassPresent(String className) { try { - // Agent classes live in the bootstrap class loader, visible from any class loader. - Class.forName( - "io.opentelemetry.javaagent.bootstrap.AgentInitializer", - false, - getClass().getClassLoader()); + Class.forName(className, false, getClass().getClassLoader()); return true; } catch (ClassNotFoundException | LinkageError e) { return false; diff --git a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java index 4400b96dda..c4a2b40795 100644 --- a/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java +++ b/modules/jooby-opentelemetry/src/test/java/io/jooby/opentelemetry/OtelModuleTest.java @@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -139,6 +140,13 @@ void shouldDetectMissingJavaAgent() { assertFalse(new OtelModule().isJavaAgentPresent()); } + @Test + void shouldDetectPresentClass() { + OtelModule module = new OtelModule(); + assertTrue(module.isClassPresent(OtelModule.class.getName())); + assertFalse(module.isClassPresent("io.jooby.opentelemetry.DoesNotExist")); + } + @Test void shouldEvaluateIsRunningInJoobyRunLocally() { OtelModule module = new OtelModule();