support otel.instrumentation.common.default-enabled in spring starter (#11746)

This commit is contained in:
Gregor Zeitlinger 2024-07-08 21:03:43 +02:00 committed by GitHub
parent bfeca08188
commit 727a7e3548
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 1 deletions

View File

@ -22,7 +22,16 @@ public class InstrumentationPropertyEnabled implements Condition {
metadata.getAnnotationAttributes(ConditionalOnEnabledInstrumentation.class.getName());
String name = String.format("otel.instrumentation.%s.enabled", attributes.get("module"));
Boolean explicit = context.getEnvironment().getProperty(name, Boolean.class);
if (explicit != null) {
return explicit;
}
boolean defaultValue = (boolean) attributes.get("enabledByDefault");
return context.getEnvironment().getProperty(name, Boolean.class, defaultValue);
if (!defaultValue) {
return false;
}
return context
.getEnvironment()
.getProperty("otel.instrumentation.common.default-enabled", Boolean.class, true);
}
}

View File

@ -41,6 +41,7 @@ class SpringWebInstrumentationAutoConfigurationTest {
void instrumentationEnabled() {
contextRunner
.withPropertyValues("otel.instrumentation.spring-web.enabled=true")
.withPropertyValues("otel.instrumentation.common.default-enabled=false")
.run(
context -> {
assertThat(
@ -69,6 +70,25 @@ class SpringWebInstrumentationAutoConfigurationTest {
assertThat(context.containsBean("otelRestTemplateBeanPostProcessor")).isFalse());
}
@Test
void instrumentationDisabledButAllEnabled() {
contextRunner
.withPropertyValues("otel.instrumentation.spring-web.enabled=false")
.withPropertyValues("otel.instrumentation.common.default-enabled=true")
.run(
context ->
assertThat(context.containsBean("otelRestTemplateBeanPostProcessor")).isFalse());
}
@Test
void allInstrumentationDisabled() {
contextRunner
.withPropertyValues("otel.instrumentation.common.default-enabled=false")
.run(
context ->
assertThat(context.containsBean("otelRestTemplateBeanPostProcessor")).isFalse());
}
@Test
void defaultConfiguration() {
contextRunner.run(