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 @@ -167,7 +167,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 @@ -182,6 +184,47 @@ 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}, 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

system env and properties are merged into application configuration, read once from configuration: https://github.com/jooby-project/jooby/blob/main/jooby/src/main/java/io/jooby/Environment.java#L292

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());
}

/**
* 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,54 @@ 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 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();
Expand Down
Loading