Rework @AutoConfigureObservability and tracing auto-configurations

@ConditionalOnEnabledTracing is now applied to the minimal amount of
beans. The beans which are annotated with it are beans that will lead
to span sending to backends.

This leaves the majority of the Micrometer Tracing, Brave and
OpenTelemetry infrastructure untouched in tests.

Closes gh-35354
This commit is contained in:
Moritz Halbritter
2023-06-16 14:09:00 +02:00
parent fb4b26a842
commit 27add2bbe3
21 changed files with 100 additions and 208 deletions

View File

@@ -18,22 +18,15 @@ package org.springframework.boot.test.autoconfigure.actuate.observability;
import java.util.Collections;
import io.micrometer.tracing.Tracer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.ContextCustomizer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link AutoConfigureObservability} and
@@ -82,59 +75,6 @@ class ObservabilityContextCustomizerFactoryTests {
assertThatTracingIsEnabled(context);
}
@Test
void shouldRegisterNoopTracerIfTracingIsDisabled() {
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
ConfigurableApplicationContext context = new GenericApplicationContext();
applyCustomizerToContext(customizer, context);
context.refresh();
Tracer tracer = context.getBean(Tracer.class);
assertThat(tracer).isNotNull();
assertThat(tracer.nextSpan().isNoop()).isTrue();
}
@Test
void shouldNotRegisterNoopTracerIfTracingIsEnabled() {
ContextCustomizer customizer = createContextCustomizer(WithAnnotation.class);
ConfigurableApplicationContext context = new GenericApplicationContext();
applyCustomizerToContext(customizer, context);
context.refresh();
assertThat(context.getBeanProvider(Tracer.class).getIfAvailable()).as("Tracer bean").isNull();
}
@Test
void shouldNotRegisterNoopTracerIfMicrometerTracingIsNotPresent() throws Exception {
try (FilteredClassLoader filteredClassLoader = new FilteredClassLoader("io.micrometer.tracing")) {
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
new ApplicationContextRunner().withClassLoader(filteredClassLoader)
.withInitializer(applyCustomizer(customizer))
.run((context) -> {
assertThat(context).doesNotHaveBean(Tracer.class);
assertThatMetricsAreDisabled(context);
assertThatTracingIsDisabled(context);
});
}
}
@Test
void shouldBackOffOnCustomTracer() {
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
new ApplicationContextRunner().withConfiguration(UserConfigurations.of(CustomTracer.class))
.withInitializer(applyCustomizer(customizer))
.run((context) -> {
assertThat(context).hasSingleBean(Tracer.class);
assertThat(context).hasBean("customTracer");
});
}
@Test
void shouldNotRunIfAotIsEnabled() {
ContextCustomizer customizer = createContextCustomizer(NoAnnotation.class);
new ApplicationContextRunner().withSystemProperties("spring.aot.enabled:true")
.withInitializer(applyCustomizer(customizer))
.run((context) -> assertThat(context).doesNotHaveBean(Tracer.class));
}
@Test
void notEquals() {
ContextCustomizer customizer1 = createContextCustomizer(OnlyMetrics.class);
@@ -256,14 +196,4 @@ class ObservabilityContextCustomizerFactoryTests {
}
@Configuration(proxyBeanMethods = false)
static class CustomTracer {
@Bean
Tracer customTracer() {
return mock(Tracer.class);
}
}
}