From 727a7e35489b04eea5b9fbc4f01c5ef96862d4e3 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Mon, 8 Jul 2024 21:03:43 +0200 Subject: [PATCH] support otel.instrumentation.common.default-enabled in spring starter (#11746) --- .../InstrumentationPropertyEnabled.java | 11 +++++++++- ...bInstrumentationAutoConfigurationTest.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/InstrumentationPropertyEnabled.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/InstrumentationPropertyEnabled.java index 419c74976108..87687f668b6c 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/InstrumentationPropertyEnabled.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/InstrumentationPropertyEnabled.java @@ -22,7 +22,16 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) 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); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfigurationTest.java index 5dd0b973a345..9091047c8432 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/SpringWebInstrumentationAutoConfigurationTest.java @@ -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 @@ void instrumentationDisabled() { 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(