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..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 @@ -161,7 +161,9 @@ public void install(Jooby application) { } var tracer = otel.getTracer("io.jooby.opentelemetry"); - application.onStop(RuntimeTelemetry.create(otel)); + if (isRuntimeTelemetryEnabled(application) && !isJavaAgentPresent()) { + application.onStop(RuntimeTelemetry.create(otel)); + } var services = application.getServices(); services.put(OpenTelemetry.class, otel); services.put(Tracer.class, tracer); @@ -176,6 +178,41 @@ 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}. 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 appConfig = application.getConfig(); + return appConfig == null + || !appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED) + || appConfig.getBoolean(RUNTIME_TELEMETRY_ENABLED); + } + + /** + * 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() { + // 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 { + Class.forName(className, 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 51751542f7..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; @@ -83,6 +84,69 @@ 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 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 shouldRegisterRuntimeTelemetryWhenEnabledByConfig() { + when(application.getConfig()).thenReturn(rootConfig); + when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); + when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true); + + new OtelModule(OpenTelemetry.noop()).install(application); + + 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 + 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 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();