Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -176,6 +178,41 @@ private static Provider<Trace> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading