Skip to content

Commit 198dbb4

Browse files
committed
Auto-configure observatibility beans in sliced tests
If @AutoConfigureObservability is applied to a sliced test, it auto-configures: - An in-memory MeterRegistry - A no-op Tracer - An ObservationRegistry Closes gh-38568
1 parent ff82b8d commit 198dbb4

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,21 @@ If such test needs access to an `MBeanServer`, consider marking it dirty as well
255255
include::code:MyJmxTests[]
256256

257257

258+
[[features.testing.spring-boot-applications.observations]]
259+
==== Using Observations
260+
If you annotate <<features#features.testing.spring-boot-applications.autoconfigured-tests, a sliced test>> with `@AutoConfigureObservability`, it auto-configures an `ObservationRegistry`.
261+
262+
258263

259264
[[features.testing.spring-boot-applications.metrics]]
260265
==== Using Metrics
261266
Regardless of your classpath, meter registries, except the in-memory backed, are not auto-configured when using `@SpringBootTest`.
262267

263268
If you need to export metrics to a different backend as part of an integration test, annotate it with `@AutoConfigureObservability`.
264269

270+
If you annotate <<features#features.testing.spring-boot-applications.autoconfigured-tests, a sliced test>> with `@AutoConfigureObservability`, it auto-configures an in-memory `MeterRegistry`.
271+
Data exporting in sliced tests is not supported with the `@AutoConfigureObservability` annotation.
272+
265273

266274

267275
[[features.testing.spring-boot-applications.tracing]]
@@ -272,6 +280,9 @@ If you need those components as part of an integration test, annotate the test w
272280

273281
If you have created your own reporting components (e.g. a custom `SpanExporter` or `SpanHandler`) and you don't want them to be active in tests, you can use the `@ConditionalOnEnabledTracing` annotation to disable them.
274282

283+
If you annotate <<features#features.testing.spring-boot-applications.autoconfigured-tests, a sliced test>> with `@AutoConfigureObservability`, it auto-configures a no-op `Tracer`.
284+
Data exporting in sliced tests is not supported with the `@AutoConfigureObservability` annotation.
285+
275286

276287

277288
[[features.testing.spring-boot-applications.mocking-beans]]

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
2628
/**
2729
* Annotation that can be applied to a test class to enable auto-configuration for
2830
* observability.
31+
* <p>
32+
* If this annotation is applied to a sliced test, an in-memory {@code MeterRegistry}, a
33+
* no-op {@code Tracer} and an {@code ObservationRegistry} is added to the application
34+
* context.
2935
*
3036
* @author Moritz Halbritter
3137
* @since 3.0.0
@@ -34,17 +40,18 @@
3440
@Retention(RetentionPolicy.RUNTIME)
3541
@Documented
3642
@Inherited
43+
@ImportAutoConfiguration
3744
public @interface AutoConfigureObservability {
3845

3946
/**
40-
* Whether metrics should be enabled in the test.
41-
* @return whether metrics should be enabled in the test
47+
* Whether metrics should be reported to external systems in the test.
48+
* @return whether metrics should be reported to external systems in the test
4249
*/
4350
boolean metrics() default true;
4451

4552
/**
46-
* Whether tracing should be enabled in the test.
47-
* @return whether tracing should be enabled in the test
53+
* Whether traces should be reported to external systems in the test.
54+
* @return whether traces should be reported to external systems in the test
4855
*/
4956
boolean tracing() default true;
5057

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AutoConfigureObservability auto-configuration imports
2+
3+
# Observation
4+
org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration
5+
6+
# Metrics
7+
org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration
8+
org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration
9+
org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration
10+
11+
# Tracing
12+
org.springframework.boot.actuate.autoconfigure.tracing.NoopTracerAutoConfiguration
13+
org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.actuate.observability;
18+
19+
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
21+
import io.micrometer.observation.ObservationRegistry;
22+
import io.micrometer.tracing.Tracer;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
27+
import org.springframework.context.ApplicationContext;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link AutoConfigureObservability} when used on a sliced test.
33+
*
34+
* @author Moritz Halbritter
35+
*/
36+
@WebMvcTest
37+
@AutoConfigureObservability
38+
class AutoConfigureObservabilitySlicedIntegrationTests {
39+
40+
@Autowired
41+
private ApplicationContext context;
42+
43+
@Test
44+
void shouldHaveTracer() {
45+
assertThat(this.context.getBean(Tracer.class)).isEqualTo(Tracer.NOOP);
46+
}
47+
48+
@Test
49+
void shouldHaveMeterRegistry() {
50+
assertThat(this.context.getBean(MeterRegistry.class)).isInstanceOf(SimpleMeterRegistry.class);
51+
}
52+
53+
@Test
54+
void shouldHaveObservationRegistry() {
55+
assertThat(this.context.getBean(ObservationRegistry.class)).isNotNull();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)