Skip to content

Commit b22d3ca

Browse files
nosansnicoll
authored andcommitted
Consistent condition for ObservedAspectConfiguration
This commit adds the same conditions that control some observation aspects to control the creation of an `ObservedAspect` bean. It now is guarded by `management.observations.annotations.enabled` and `micrometer.observations.annotations.enabled`. See gh-45601 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 00ceb71 commit b22d3ca

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfiguration.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,12 +39,15 @@
3939
import org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration;
4040
import org.springframework.boot.autoconfigure.AutoConfiguration;
4141
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
42+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
4243
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
4344
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4445
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4546
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
47+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4648
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4749
import org.springframework.context.annotation.Bean;
50+
import org.springframework.context.annotation.Conditional;
4851
import org.springframework.context.annotation.Configuration;
4952
import org.springframework.core.annotation.Order;
5053

@@ -159,6 +162,7 @@ TracingAwareMeterObservationHandler<Observation.Context> tracingAwareMeterObserv
159162

160163
@Configuration(proxyBeanMethods = false)
161164
@ConditionalOnClass(Advice.class)
165+
@Conditional(ObservationAnnotationsEnabledCondition.class)
162166
static class ObservedAspectConfiguration {
163167

164168
@Bean
@@ -169,4 +173,22 @@ ObservedAspect observedAspect(ObservationRegistry observationRegistry) {
169173

170174
}
171175

176+
static final class ObservationAnnotationsEnabledCondition extends AnyNestedCondition {
177+
178+
ObservationAnnotationsEnabledCondition() {
179+
super(ConfigurationPhase.PARSE_CONFIGURATION);
180+
}
181+
182+
@ConditionalOnProperty(prefix = "micrometer.observations.annotations", name = "enabled", havingValue = "true")
183+
static class MicrometerObservationsEnabledCondition {
184+
185+
}
186+
187+
@ConditionalOnProperty(prefix = "management.observations.annotations", name = "enabled", havingValue = "true")
188+
static class ManagementObservationsEnabledCondition {
189+
190+
}
191+
192+
}
193+
172194
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/ObservationAutoConfigurationTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,11 +66,13 @@
6666
class ObservationAutoConfigurationTests {
6767

6868
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().with(MetricsRun.simple())
69+
.withPropertyValues("management.observations.annotations.enabled=true")
6970
.withClassLoader(new FilteredClassLoader("io.micrometer.tracing"))
7071
.withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class));
7172

7273
private final ApplicationContextRunner tracingContextRunner = new ApplicationContextRunner()
7374
.with(MetricsRun.simple())
75+
.withPropertyValues("management.observations.annotations.enabled=true")
7476
.withUserConfiguration(TracerConfiguration.class)
7577
.withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class));
7678

@@ -141,6 +143,7 @@ void supplyMeterHandlerAndGroupingWhenMicrometerCoreAndTracingAreOnClassPath() {
141143
@Test
142144
void supplyMeterHandlerAndGroupingWhenMicrometerCoreAndTracingAreOnClassPathButThereIsNoTracer() {
143145
new ApplicationContextRunner().with(MetricsRun.simple())
146+
.withPropertyValues("management.observations.annotations.enabled=true")
144147
.withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class))
145148
.run((context) -> {
146149
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
@@ -181,6 +184,20 @@ void allowsObservedAspectToBeDisabled() {
181184
.run((context) -> assertThat(context).doesNotHaveBean(ObservedAspect.class));
182185
}
183186

187+
@Test
188+
void allowsObservedAspectToBeEnabledWithDeprecatedProperty() {
189+
this.contextRunner
190+
.withPropertyValues("management.observations.annotations.enabled=false",
191+
"micrometer.observations.annotations.enabled=true")
192+
.run((context) -> assertThat(context).hasSingleBean(ObservedAspect.class));
193+
}
194+
195+
@Test
196+
void allowsObservedAspectToBeDisabledWithProperty() {
197+
this.contextRunner.withPropertyValues("management.observations.annotations.enabled=false")
198+
.run((context) -> assertThat(context).doesNotHaveBean(ObservedAspect.class));
199+
}
200+
184201
@Test
185202
void allowsObservedAspectToBeCustomized() {
186203
this.contextRunner.withUserConfiguration(CustomObservedAspectConfiguration.class)

0 commit comments

Comments
 (0)