Skip to content

Commit

Permalink
Rework @AutoConfigureObservability and tracing auto-configurations
Browse files Browse the repository at this point in the history
@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
  • Loading branch information
mhalbritter committed Jun 16, 2023
1 parent fb4b26a commit 27add2b
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
@AutoConfiguration(before = MicrometerTracingAutoConfiguration.class)
@ConditionalOnClass({ Tracer.class, BraveTracer.class })
@EnableConfigurationProperties(TracingProperties.class)
@ConditionalOnEnabledTracing
public class BraveAutoConfiguration {

private static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
*/
@AutoConfiguration
@ConditionalOnClass(Tracer.class)
@ConditionalOnEnabledTracing
public class MicrometerTracingAutoConfiguration {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
* @since 3.0.0
*/
@AutoConfiguration(before = MicrometerTracingAutoConfiguration.class)
@ConditionalOnEnabledTracing
@ConditionalOnClass({ OtelTracer.class, SdkTracerProvider.class, OpenTelemetry.class })
@EnableConfigurationProperties(TracingProperties.class)
public class OpenTelemetryAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
* @since 3.1.0
*/
@AutoConfiguration
@ConditionalOnEnabledTracing
@ConditionalOnClass({ OtelTracer.class, SdkTracerProvider.class, OpenTelemetry.class, OtlpHttpSpanExporter.class })
@EnableConfigurationProperties(OtlpProperties.class)
public class OtlpAutoConfiguration {
Expand All @@ -59,6 +58,7 @@ public class OtlpAutoConfiguration {
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
@ConditionalOnProperty(prefix = "management.otlp.tracing", name = "endpoint")
@ConditionalOnEnabledTracing
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
.setEndpoint(properties.getEndpoint())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -43,7 +42,6 @@
after = MicrometerTracingAutoConfiguration.class)
@ConditionalOnBean(Tracer.class)
@ConditionalOnClass({ Tracer.class, SpanContextSupplier.class })
@ConditionalOnEnabledTracing
public class PrometheusExemplarsAutoConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
WavefrontAutoConfiguration.class })
@ConditionalOnClass({ WavefrontSender.class, WavefrontSpanHandler.class })
@ConditionalOnEnabledTracing
@EnableConfigurationProperties(WavefrontProperties.class)
@Import(WavefrontSenderConfiguration.class)
public class WavefrontTracingAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WavefrontSender.class)
@ConditionalOnEnabledTracing
WavefrontSpanHandler wavefrontSpanHandler(WavefrontProperties properties, WavefrontSender wavefrontSender,
SpanMetrics spanMetrics, ApplicationTags applicationTags) {
return new WavefrontSpanHandler(properties.getSender().getMaxQueueSize(), wavefrontSender, spanMetrics,
Expand Down Expand Up @@ -96,6 +96,7 @@ static class WavefrontBrave {

@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledTracing
WavefrontBraveSpanHandler wavefrontBraveSpanHandler(WavefrontSpanHandler wavefrontSpanHandler) {
return new WavefrontBraveSpanHandler(wavefrontSpanHandler);
}
Expand All @@ -108,6 +109,7 @@ static class WavefrontOpenTelemetry {

@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledTracing
WavefrontOtelSpanExporter wavefrontOtelSpanExporter(WavefrontSpanHandler wavefrontSpanHandler) {
return new WavefrontOtelSpanExporter(wavefrontSpanHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import zipkin2.codec.SpanBytesEncoder;
import zipkin2.reporter.Sender;

import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.BraveConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.OpenTelemetryConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.ReporterConfiguration;
Expand All @@ -48,7 +47,6 @@
@ConditionalOnClass(Sender.class)
@Import({ SenderConfiguration.class, ReporterConfiguration.class, BraveConfiguration.class,
OpenTelemetryConfiguration.class })
@ConditionalOnEnabledTracing
@EnableConfigurationProperties(ZipkinProperties.class)
public class ZipkinAutoConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import zipkin2.reporter.urlconnection.URLConnectionSender;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -142,6 +143,7 @@ static class BraveConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(Reporter.class)
@ConditionalOnEnabledTracing
ZipkinSpanHandler zipkinSpanHandler(Reporter<Span> spanReporter) {
return (ZipkinSpanHandler) ZipkinSpanHandler.newBuilder(spanReporter).build();
}
Expand All @@ -155,6 +157,7 @@ static class OpenTelemetryConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(Sender.class)
@ConditionalOnEnabledTracing
ZipkinSpanExporter zipkinSpanExporter(BytesEncoder<Span> encoder, Sender sender) {
return ZipkinSpanExporter.builder().setEncoder(encoder).setSender(sender).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import com.wavefront.sdk.common.WavefrontSender;
import com.wavefront.sdk.common.clients.WavefrontClient.Builder;

import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport;
import org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront.WavefrontMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.actuate.autoconfigure.tracing.wavefront.WavefrontTracingAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

Expand All @@ -46,6 +50,7 @@ public class WavefrontSenderConfiguration {

@Bean
@ConditionalOnMissingBean
@Conditional(WavefrontTracingOrMetricsCondition.class)
public WavefrontSender wavefrontSender(WavefrontProperties properties) {
Builder builder = new Builder(properties.getEffectiveUri().toString(), properties.getApiTokenOrThrow());
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
Expand All @@ -57,4 +62,22 @@ public WavefrontSender wavefrontSender(WavefrontProperties properties) {
return builder.build();
}

static final class WavefrontTracingOrMetricsCondition extends AnyNestedCondition {

WavefrontTracingOrMetricsCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}

@ConditionalOnEnabledTracing
static class TracingCondition {

}

@ConditionalOnEnabledMetricsExport("wavefront")
static class MetricsCondition {

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.BraveAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryAutoConfiguration;
import org.springframework.boot.actuate.health.HealthEndpointWebExtension;
import org.springframework.boot.actuate.health.ReactiveHealthEndpointWebExtension;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand Down Expand Up @@ -80,7 +82,8 @@ private ReactiveWebApplicationContextRunner reactiveWebRunner() {
MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class, HazelcastAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class, RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class })
RedisRepositoriesAutoConfiguration.class, BraveAutoConfiguration.class,
OpenTelemetryAutoConfiguration.class })
@SpringBootConfiguration
static class WebEndpointTestApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ void shouldSupplyB3PropagationFactoryViaProperty() {
});
}

@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
}

@Test
void shouldNotSupplyCorrelationScopeDecoratorIfBaggageDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,6 @@ void shouldNotSupplyBeansIfPropagatorIsMissing() {
});
}

@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withUserConfiguration(TracerConfiguration.class, PropagatorConfiguration.class)
.withPropertyValues("management.tracing.enabled=false")
.run((context) -> {
assertThat(context).doesNotHaveBean(DefaultTracingObservationHandler.class);
assertThat(context).doesNotHaveBean(PropagatingReceiverTracingObservationHandler.class);
assertThat(context).doesNotHaveBean(PropagatingSenderTracingObservationHandler.class);
});
}

@Configuration(proxyBeanMethods = false)
private static class TracerConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class OtlpAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OtlpAutoConfiguration.class));

private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");

@Test
void shouldNotSupplyBeansIfPropertyIsNotSet() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(OtlpHttpSpanExporter.class));
Expand Down Expand Up @@ -96,6 +99,13 @@ void shouldBackOffWhenCustomGrpcExporterIsDefined() {
.hasSingleBean(SpanExporter.class));
}

@Test
void shouldNotSupplyOtlpHttpSpanExporterIfTracingIsDisabled() {
this.tracingDisabledContextRunner
.withPropertyValues("management.otlp.tracing.endpoint=http://localhost:4318/v1/traces")
.run((context) -> assertThat(context).doesNotHaveBean(OtlpHttpSpanExporter.class));
}

@Configuration(proxyBeanMethods = false)
private static class CustomHttpExporterConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* Tests for {@link PrometheusExemplarsAutoConfiguration}.
*
* * @author Jonatan Ivanov
* @author Jonatan Ivanov
*/
class PrometheusExemplarsAutoConfigurationTests {

Expand All @@ -52,12 +52,6 @@ class PrometheusExemplarsAutoConfigurationTests {
AutoConfigurations.of(PrometheusExemplarsAutoConfiguration.class, ObservationAutoConfiguration.class,
BraveAutoConfiguration.class, MicrometerTracingAutoConfiguration.class));

@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(SpanContextSupplier.class));
}

@Test
void shouldNotSupplyBeansIfPrometheusSupportIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader("io.prometheus.client.exemplars"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class WavefrontTracingAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(WavefrontAutoConfiguration.class, WavefrontTracingAutoConfiguration.class));

private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");

@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class).run((context) -> {
Expand Down Expand Up @@ -83,14 +86,11 @@ void shouldNotSupplyBeansIfMicrometerReporterWavefrontIsMissing() {

@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.withUserConfiguration(WavefrontSenderConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(WavefrontSpanHandler.class);
assertThat(context).doesNotHaveBean(SpanMetrics.class);
assertThat(context).doesNotHaveBean(WavefrontBraveSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontOtelSpanExporter.class);
});
this.tracingDisabledContextRunner.withUserConfiguration(WavefrontSenderConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(WavefrontSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontBraveSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontOtelSpanExporter.class);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ void shouldBackOffOnCustomBeans() {
});
}

@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(BytesEncoder.class));
}

@Test
void definesPropertiesBasedConnectionDetailsByDefault() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(PropertiesZipkinConnectionDetails.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ZipkinConfigurationsBraveConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(BraveConfiguration.class));

private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");

@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(ReporterConfiguration.class)
Expand Down Expand Up @@ -79,6 +82,12 @@ void shouldSupplyZipkinSpanHandlerWithCustomSpanHandler() {
});
}

@Test
void shouldNotSupplyZipkinSpanHandlerIfTracingIsDisabled() {
this.tracingDisabledContextRunner.withUserConfiguration(ReporterConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanHandler.class));
}

@Configuration(proxyBeanMethods = false)
private static class ReporterConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ZipkinConfigurationsOpenTelemetryConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(BaseConfiguration.class, OpenTelemetryConfiguration.class));

private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");

@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(SenderConfiguration.class)
Expand Down Expand Up @@ -70,6 +73,12 @@ void shouldBackOffOnCustomBeans() {
});
}

@Test
void shouldNotSupplyZipkinSpanExporterIfTracingIsDisabled() {
this.tracingDisabledContextRunner.withUserConfiguration(SenderConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanExporter.class));
}

@Configuration(proxyBeanMethods = false)
private static class SenderConfiguration {

Expand Down
Loading

0 comments on commit 27add2b

Please sign in to comment.