Skip to content

Commit 56276d1

Browse files
committed
OtelModule: read runtime-telemetry toggle from application config only
1 parent caab64d commit 56276d1

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,15 @@ private static Provider<Trace> trace(Tracer tracer) {
183183

184184
/**
185185
* 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.
186+
* OpenTelemetry property {@code otel.instrumentation.runtime-telemetry.enabled=false}. System
187+
* properties and environment variables are already merged into the application configuration, so
188+
* it is read from there only.
189189
*/
190190
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());
191+
var appConfig = application.getConfig();
192+
return appConfig == null
193+
|| !appConfig.hasPath(RUNTIME_TELEMETRY_ENABLED)
194+
|| appConfig.getBoolean(RUNTIME_TELEMETRY_ENABLED);
202195
}
203196

204197
/**

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,35 @@ void shouldRegisterRuntimeTelemetryByDefault() {
9393
}
9494

9595
@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-
}
96+
void shouldSkipRuntimeTelemetryWhenDisabledByConfig() {
97+
when(application.getConfig()).thenReturn(rootConfig);
98+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
99+
when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false);
100+
101+
new OtelModule(OpenTelemetry.noop()).install(application);
103102

104103
verify(application, never()).onStop(any(AutoCloseable.class));
105104
}
106105

107106
@Test
108-
void shouldSkipRuntimeTelemetryWhenDisabledByConfig() {
107+
void shouldRegisterRuntimeTelemetryWhenEnabledByConfig() {
109108
when(application.getConfig()).thenReturn(rootConfig);
110109
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
111-
when(rootConfig.getValue("otel.instrumentation.runtime-telemetry.enabled"))
112-
.thenReturn(ConfigValueFactory.fromAnyRef(false));
110+
when(rootConfig.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(true);
113111

114112
new OtelModule(OpenTelemetry.noop()).install(application);
115113

116-
verify(application, never()).onStop(any(AutoCloseable.class));
114+
verify(application).onStop(any(AutoCloseable.class));
115+
}
116+
117+
@Test
118+
void shouldRegisterRuntimeTelemetryWhenConfigHasNoSetting() {
119+
when(application.getConfig()).thenReturn(rootConfig);
120+
when(rootConfig.hasPath("otel.instrumentation.runtime-telemetry.enabled")).thenReturn(false);
121+
122+
new OtelModule(OpenTelemetry.noop()).install(application);
123+
124+
verify(application).onStop(any(AutoCloseable.class));
117125
}
118126

119127
@Test

0 commit comments

Comments
 (0)