Skip to content

Commit c00e6eb

Browse files
committed
Merge branch '3.0.x'
Closes gh-35360
2 parents a4092fb + 1c87fcb commit c00e6eb

File tree

3 files changed

+98
-18
lines changed

3 files changed

+98
-18
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/actuate/observability/ObservabilityContextCustomizerFactory.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.context.ConfigurableApplicationContext;
3838
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
3939
import org.springframework.core.Ordered;
40+
import org.springframework.core.env.Environment;
4041
import org.springframework.test.context.ContextConfigurationAttributes;
4142
import org.springframework.test.context.ContextCustomizer;
4243
import org.springframework.test.context.ContextCustomizerFactory;
@@ -45,8 +46,9 @@
4546
import org.springframework.util.ClassUtils;
4647

4748
/**
48-
* {@link ContextCustomizerFactory} that globally disables metrics export and tracing
49-
* unless {@link AutoConfigureObservability} is set on the test class.
49+
* {@link ContextCustomizerFactory} that globally disables metrics export and tracing in
50+
* tests. The behaviour can be controlled with {@link AutoConfigureObservability} on the
51+
* test class or via the {@value #AUTO_CONFIGURE_PROPERTY} property.
5052
* <p>
5153
* Registers {@link Tracer#NOOP} if tracing is disabled, micrometer-tracing is on the
5254
* classpath, and the user hasn't supplied their own {@link Tracer}.
@@ -56,43 +58,53 @@
5658
*/
5759
class ObservabilityContextCustomizerFactory implements ContextCustomizerFactory {
5860

61+
static final String AUTO_CONFIGURE_PROPERTY = "spring.test.observability.auto-configure";
62+
5963
@Override
6064
public ContextCustomizer createContextCustomizer(Class<?> testClass,
6165
List<ContextConfigurationAttributes> configAttributes) {
6266
AutoConfigureObservability annotation = TestContextAnnotationUtils.findMergedAnnotation(testClass,
6367
AutoConfigureObservability.class);
64-
if (annotation == null) {
65-
return new DisableObservabilityContextCustomizer(true, true);
66-
}
67-
return new DisableObservabilityContextCustomizer(!annotation.metrics(), !annotation.tracing());
68+
return new DisableObservabilityContextCustomizer(annotation);
6869
}
6970

7071
private static class DisableObservabilityContextCustomizer implements ContextCustomizer {
7172

72-
private final boolean disableMetrics;
73-
74-
private final boolean disableTracing;
73+
private final AutoConfigureObservability annotation;
7574

76-
DisableObservabilityContextCustomizer(boolean disableMetrics, boolean disableTracing) {
77-
this.disableMetrics = disableMetrics;
78-
this.disableTracing = disableTracing;
75+
DisableObservabilityContextCustomizer(AutoConfigureObservability annotation) {
76+
this.annotation = annotation;
7977
}
8078

8179
@Override
8280
public void customizeContext(ConfigurableApplicationContext context,
8381
MergedContextConfiguration mergedContextConfiguration) {
84-
if (this.disableMetrics) {
82+
if (areMetricsDisabled(context.getEnvironment())) {
8583
TestPropertyValues
8684
.of("management.defaults.metrics.export.enabled=false",
8785
"management.simple.metrics.export.enabled=true")
8886
.applyTo(context);
8987
}
90-
if (this.disableTracing) {
88+
if (isTracingDisabled(context.getEnvironment())) {
9189
TestPropertyValues.of("management.tracing.enabled=false").applyTo(context);
9290
registerNoopTracer(context);
9391
}
9492
}
9593

94+
private boolean areMetricsDisabled(Environment environment) {
95+
if (this.annotation != null) {
96+
return !this.annotation.metrics();
97+
}
98+
return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false);
99+
}
100+
101+
private boolean isTracingDisabled(Environment environment) {
102+
if (this.annotation != null) {
103+
return !this.annotation.tracing();
104+
}
105+
return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false);
106+
}
107+
96108
private void registerNoopTracer(ConfigurableApplicationContext context) {
97109
if (AotDetector.useGeneratedArtifacts()) {
98110
return;
@@ -121,12 +133,12 @@ public boolean equals(Object o) {
121133
return false;
122134
}
123135
DisableObservabilityContextCustomizer that = (DisableObservabilityContextCustomizer) o;
124-
return this.disableMetrics == that.disableMetrics && this.disableTracing == that.disableTracing;
136+
return Objects.equals(this.annotation, that.annotation);
125137
}
126138

127139
@Override
128140
public int hashCode() {
129-
return Objects.hash(this.disableMetrics, this.disableTracing);
141+
return Objects.hash(this.annotation);
130142
}
131143

132144
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"type": "org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint",
1212
"description": "MVC Print option.",
1313
"defaultValue": "default"
14+
},
15+
{
16+
"name": "spring.test.observability.auto-configure",
17+
"type": "java.lang.Boolean",
18+
"description": "Whether observability should be auto-configured in tests.",
19+
"defaultValue": "false"
1420
}
1521
]
16-
}
22+
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/actuate/observability/ObservabilityContextCustomizerFactoryTests.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.context.support.GenericApplicationContext;
32+
import org.springframework.mock.env.MockEnvironment;
3233
import org.springframework.test.context.ContextCustomizer;
3334

3435
import static org.assertj.core.api.Assertions.assertThat;
@@ -135,12 +136,68 @@ void shouldNotRunIfAotIsEnabled() {
135136
}
136137

137138
@Test
138-
void hashCodeAndEquals() {
139+
void notEquals() {
139140
ContextCustomizer customizer1 = createContextCustomizer(OnlyMetrics.class);
140141
ContextCustomizer customizer2 = createContextCustomizer(OnlyTracing.class);
141142
assertThat(customizer1).isNotEqualTo(customizer2);
142143
}
143144

145+
@Test
146+
void equals() {
147+
ContextCustomizer customizer1 = createContextCustomizer(OnlyMetrics.class);
148+
ContextCustomizer customizer2 = createContextCustomizer(OnlyMetrics.class);
149+
assertThat(customizer1).isEqualTo(customizer2);
150+
assertThat(customizer1).hasSameHashCodeAs(customizer2);
151+
}
152+
153+
@Test
154+
void metricsAndTracingCanBeEnabledViaProperty() {
155+
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
156+
ConfigurableApplicationContext context = new GenericApplicationContext();
157+
MockEnvironment environment = new MockEnvironment();
158+
environment.setProperty("spring.test.observability.auto-configure", "true");
159+
context.setEnvironment(environment);
160+
applyCustomizerToContext(customizer, context);
161+
assertThatMetricsAreEnabled(context);
162+
assertThatTracingIsEnabled(context);
163+
}
164+
165+
@Test
166+
void metricsAndTracingCanBeDisabledViaProperty() {
167+
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
168+
ConfigurableApplicationContext context = new GenericApplicationContext();
169+
MockEnvironment environment = new MockEnvironment();
170+
environment.setProperty("spring.test.observability.auto-configure", "false");
171+
context.setEnvironment(environment);
172+
applyCustomizerToContext(customizer, context);
173+
assertThatMetricsAreDisabled(context);
174+
assertThatTracingIsDisabled(context);
175+
}
176+
177+
@Test
178+
void annotationTakesPrecedenceOverDisabledProperty() {
179+
ContextCustomizer customizer = createContextCustomizer(WithAnnotation.class);
180+
ConfigurableApplicationContext context = new GenericApplicationContext();
181+
MockEnvironment environment = new MockEnvironment();
182+
environment.setProperty("spring.test.observability.auto-configure", "false");
183+
context.setEnvironment(environment);
184+
applyCustomizerToContext(customizer, context);
185+
assertThatMetricsAreEnabled(context);
186+
assertThatTracingIsEnabled(context);
187+
}
188+
189+
@Test
190+
void annotationTakesPrecedenceOverEnabledProperty() {
191+
ContextCustomizer customizer = createContextCustomizer(WithDisabledAnnotation.class);
192+
ConfigurableApplicationContext context = new GenericApplicationContext();
193+
MockEnvironment environment = new MockEnvironment();
194+
environment.setProperty("spring.test.observability.auto-configure", "true");
195+
context.setEnvironment(environment);
196+
applyCustomizerToContext(customizer, context);
197+
assertThatMetricsAreDisabled(context);
198+
assertThatTracingIsDisabled(context);
199+
}
200+
144201
private void applyCustomizerToContext(ContextCustomizer customizer, ConfigurableApplicationContext context) {
145202
customizer.customizeContext(context, null);
146203
}
@@ -194,6 +251,11 @@ static class WithAnnotation {
194251

195252
}
196253

254+
@AutoConfigureObservability(metrics = false, tracing = false)
255+
static class WithDisabledAnnotation {
256+
257+
}
258+
197259
@Configuration(proxyBeanMethods = false)
198260
static class CustomTracer {
199261

0 commit comments

Comments
 (0)