Add properties to enable/disable tracing per exporter

There are now three new properties, which control the trace exporting on
a more fine-grained level:

- management.otlp.tracing.export.enabled
- management.wavefront.tracing.export.enabled
- management.zipkin.tracing.export.enabled

They default to null, and if set, take precedence over the global
management.metrics.enabled property.

Closes gh-34620
This commit is contained in:
Moritz Halbritter 2024-06-18 10:37:48 +02:00
parent 6c0c9b9a80
commit f900d0ba4e
13 changed files with 306 additions and 47 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,13 +22,15 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
/**
* {@link Conditional @Conditional} that checks whether tracing is enabled. It matches if
* the value of the {@code management.tracing.enabled} property is {@code true} or if it
* is not configured.
* is not configured. If the {@link #value() tracing exporter name} is set, the
* {@code management.<name>.tracing.export.enabled} property can be used to control the
* behavior for the specific tracing exporter. In that case, the exporter specific
* property takes precedence over the global property.
*
* @author Moritz Halbritter
* @since 3.0.0
@ -36,7 +38,14 @@ import org.springframework.context.annotation.Conditional;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnProperty(prefix = "management.tracing", name = "enabled", matchIfMissing = true)
@Conditional(OnEnabledTracingCondition.class)
public @interface ConditionalOnEnabledTracing {
/**
* Name of the tracing exporter.
* @return the name of the tracing exporter
* @since 3.4.0
*/
String value() default "";
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
/**
* {@link SpringBootCondition} to check whether tracing is enabled.
*
* @author Moritz Halbritter
* @see ConditionalOnEnabledTracing
*/
class OnEnabledTracingCondition extends SpringBootCondition {
private static final String GLOBAL_PROPERTY = "management.tracing.enabled";
private static final String EXPORTER_PROPERTY = "management.%s.tracing.export.enabled";
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String tracingExporter = getExporterName(metadata);
if (StringUtils.hasLength(tracingExporter)) {
Boolean exporterTracingEnabled = context.getEnvironment()
.getProperty(EXPORTER_PROPERTY.formatted(tracingExporter), Boolean.class);
if (exporterTracingEnabled != null) {
return new ConditionOutcome(exporterTracingEnabled,
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
.because(EXPORTER_PROPERTY.formatted(tracingExporter) + " is " + exporterTracingEnabled));
}
}
Boolean globalTracingEnabled = context.getEnvironment().getProperty(GLOBAL_PROPERTY, Boolean.class);
if (globalTracingEnabled != null) {
return new ConditionOutcome(globalTracingEnabled,
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
.because(GLOBAL_PROPERTY + " is " + globalTracingEnabled));
}
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
.because("tracing is enabled by default"));
}
private static String getExporterName(AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName());
if (attributes == null) {
return null;
}
return (String) attributes.get("value");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -72,7 +72,7 @@ class OtlpTracingConfigurations {
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("otlp")
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -59,7 +59,7 @@ public class WavefrontTracingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WavefrontSender.class)
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("wavefront")
WavefrontSpanHandler wavefrontSpanHandler(WavefrontProperties properties, WavefrontSender wavefrontSender,
SpanMetrics spanMetrics, ApplicationTags applicationTags) {
return new WavefrontSpanHandler(properties.getSender().getMaxQueueSize(), wavefrontSender, spanMetrics,
@ -96,7 +96,7 @@ public class WavefrontTracingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("wavefront")
WavefrontBraveSpanHandler wavefrontBraveSpanHandler(WavefrontSpanHandler wavefrontSpanHandler) {
return new WavefrontBraveSpanHandler(wavefrontSpanHandler);
}
@ -109,7 +109,7 @@ public class WavefrontTracingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("wavefront")
WavefrontOtelSpanExporter wavefrontOtelSpanExporter(WavefrontSpanHandler wavefrontSpanHandler) {
return new WavefrontOtelSpanExporter(wavefrontSpanHandler);
}

View File

@ -187,7 +187,7 @@ class ZipkinConfigurations {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(BytesMessageSender.class)
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("zipkin")
AsyncZipkinSpanHandler asyncZipkinSpanHandler(BytesMessageSender sender,
BytesEncoder<MutableSpan> mutableSpanBytesEncoder) {
return AsyncZipkinSpanHandler.newBuilder(sender).build(mutableSpanBytesEncoder);
@ -208,7 +208,7 @@ class ZipkinConfigurations {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(BytesMessageSender.class)
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("zipkin")
ZipkinSpanExporter zipkinSpanExporter(BytesMessageSender sender, BytesEncoder<Span> spanBytesEncoder) {
return ZipkinSpanExporter.builder().setSender(sender).setEncoder(spanBytesEncoder).build();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -69,7 +69,7 @@ public class WavefrontSenderConfiguration {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnEnabledTracing
@ConditionalOnEnabledTracing("wavefront")
static class TracingCondition {
}

View File

@ -2093,6 +2093,11 @@
"name": "management.otlp.tracing.compression",
"defaultValue": "none"
},
{
"name": "management.otlp.tracing.export.enabled",
"type": "java.lang.Boolean",
"description": "Whether auto-configuration of tracing is enabled to export OTLP traces."
},
{
"name": "management.prometheus.metrics.export.histogram-flavor",
"defaultValue": "prometheus"
@ -2258,12 +2263,22 @@
"W3C"
]
},
{
"name": "management.wavefront.tracing.export.enabled",
"type": "java.lang.Boolean",
"description": "Whether auto-configuration of tracing is enabled to export Wavefront traces."
},
{
"name": "management.zipkin.tracing.encoding",
"defaultValue": [
"JSON"
]
},
{
"name": "management.zipkin.tracing.export.enabled",
"type": "java.lang.Boolean",
"description": "Whether auto-configuration of tracing is enabled to export Zipkin traces."
},
{
"name": "micrometer.observations.annotations.enabled",
"type": "java.lang.Boolean",

View File

@ -0,0 +1,129 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link OnEnabledTracingCondition}.
*
* @author Moritz Halbritter
*/
class OnEnabledTracingConditionTests {
@Test
void shouldMatchIfNoPropertyIsSet() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(), mockMetaData(""));
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing tracing is enabled by default");
}
@Test
void shouldNotMatchIfGlobalPropertyIsFalse() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "false")), mockMetaData(""));
assertThat(outcome.isMatch()).isFalse();
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is false");
}
@Test
void shouldMatchIfGlobalPropertyIsTrue() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "true")), mockMetaData(""));
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is true");
}
@Test
void shouldNotMatchIfExporterPropertyIsFalse() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition.getMatchOutcome(
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "false")),
mockMetaData("zipkin"));
assertThat(outcome.isMatch()).isFalse();
assertThat(outcome.getMessage())
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false");
}
@Test
void shouldMatchIfExporterPropertyIsTrue() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition.getMatchOutcome(
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "true")),
mockMetaData("zipkin"));
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage())
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true");
}
@Test
void exporterPropertyShouldOverrideGlobalPropertyIfTrue() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
Map.of("management.tracing.enabled", "false", "management.zipkin.tracing.export.enabled", "true")),
mockMetaData("zipkin"));
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage())
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true");
}
@Test
void exporterPropertyShouldOverrideGlobalPropertyIfFalse() {
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
Map.of("management.tracing.enabled", "true", "management.zipkin.tracing.export.enabled", "false")),
mockMetaData("zipkin"));
assertThat(outcome.isMatch()).isFalse();
assertThat(outcome.getMessage())
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false");
}
private ConditionContext mockConditionContext() {
return mockConditionContext(Collections.emptyMap());
}
private ConditionContext mockConditionContext(Map<String, String> properties) {
ConditionContext context = mock(ConditionContext.class);
MockEnvironment environment = new MockEnvironment();
properties.forEach(environment::setProperty);
given(context.getEnvironment()).willReturn(environment);
return context;
}
private AnnotatedTypeMetadata mockMetaData(String exporter) {
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
given(metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName()))
.willReturn(Map.of("value", exporter));
return metadata;
}
}

View File

@ -59,11 +59,17 @@ class OtlpAutoConfigurationTests {
}
@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
void shouldNotSupplyBeansIfGlobalTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(SpanExporter.class));
}
@Test
void shouldNotSupplyBeansIfOtlpTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.otlp.tracing.export.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(SpanExporter.class));
}
@Test
void shouldNotSupplyBeansIfTracingBridgeIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader("io.micrometer.tracing"))

View File

@ -47,9 +47,6 @@ 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) -> {
@ -85,12 +82,25 @@ class WavefrontTracingAutoConfigurationTests {
}
@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.tracingDisabledContextRunner.withUserConfiguration(WavefrontSenderConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(WavefrontSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontBraveSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontOtelSpanExporter.class);
});
void shouldNotSupplyBeansIfGlobalTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.withUserConfiguration(WavefrontSenderConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(WavefrontSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontBraveSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontOtelSpanExporter.class);
});
}
@Test
void shouldNotSupplyBeansIfWavefrontTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.wavefront.tracing.export.enabled=false")
.withUserConfiguration(WavefrontSenderConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(WavefrontSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontBraveSpanHandler.class);
assertThat(context).doesNotHaveBean(WavefrontOtelSpanExporter.class);
});
}
@Test

View File

@ -48,9 +48,6 @@ class ZipkinConfigurationsBraveConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DefaultEncodingConfiguration.class, BraveConfiguration.class));
private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");
@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(SenderConfiguration.class)
@ -93,8 +90,16 @@ class ZipkinConfigurationsBraveConfigurationTests {
}
@Test
void shouldNotSupplyAsyncZipkinSpanHandlerIfTracingIsDisabled() {
this.tracingDisabledContextRunner.withUserConfiguration(SenderConfiguration.class)
void shouldNotSupplyAsyncZipkinSpanHandlerIfGlobalTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.withUserConfiguration(SenderConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(AsyncZipkinSpanHandler.class));
}
@Test
void shouldNotSupplyAsyncZipkinSpanHandlerIfZipkinTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.zipkin.tracing.export.enabled=false")
.withUserConfiguration(SenderConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(AsyncZipkinSpanHandler.class));
}

View File

@ -42,9 +42,6 @@ class ZipkinConfigurationsOpenTelemetryConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DefaultEncodingConfiguration.class, OpenTelemetryConfiguration.class));
private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");
@Test
void shouldSupplyBeans() {
this.contextRunner.withUserConfiguration(SenderConfiguration.class, CustomEncoderConfiguration.class)
@ -92,8 +89,16 @@ class ZipkinConfigurationsOpenTelemetryConfigurationTests {
}
@Test
void shouldNotSupplyZipkinSpanExporterIfTracingIsDisabled() {
this.tracingDisabledContextRunner.withUserConfiguration(SenderConfiguration.class)
void shouldNotSupplyZipkinSpanExporterIfGlobalTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.withUserConfiguration(SenderConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanExporter.class));
}
@Test
void shouldNotSupplyZipkinSpanExporterIfZipkinTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.zipkin.tracing.export.enabled=false")
.withUserConfiguration(SenderConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(ZipkinSpanExporter.class));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,17 +45,9 @@ class WavefrontSenderConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(WavefrontSenderConfiguration.class));
private final ApplicationContextRunner tracingDisabledContextRunner = this.contextRunner
.withPropertyValues("management.tracing.enabled=false");
private final ApplicationContextRunner metricsDisabledContextRunner = this.contextRunner.withPropertyValues(
"management.defaults.metrics.export.enabled=false", "management.simple.metrics.export.enabled=true");
// Both metrics and tracing are disabled
private final ApplicationContextRunner observabilityDisabledContextRunner = this.contextRunner.withPropertyValues(
"management.tracing.enabled=false", "management.defaults.metrics.export.enabled=false",
"management.simple.metrics.export.enabled=true");
@Test
void shouldNotFailIfWavefrontIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader("com.wavefront"))
@ -98,14 +90,32 @@ class WavefrontSenderConfigurationTests {
}
@Test
void shouldNotSupplyWavefrontSenderIfObservabilityIsDisabled() {
this.observabilityDisabledContextRunner.withPropertyValues("management.wavefront.api-token=abcde")
void shouldNotSupplyWavefrontSenderIfMetricsAndGlobalTracingIsDisabled() {
this.metricsDisabledContextRunner
.withPropertyValues("management.tracing.enabled=false", "management.wavefront.api-token=abcde")
.run((context) -> assertThat(context).doesNotHaveBean(WavefrontSender.class));
}
@Test
void shouldSupplyWavefrontSenderIfOnlyTracingIsDisabled() {
this.tracingDisabledContextRunner.withPropertyValues("management.wavefront.api-token=abcde")
void shouldNotSupplyWavefrontSenderIfMetricsAndWavefrontTracingIsDisabled() {
this.metricsDisabledContextRunner
.withPropertyValues("management.wavefront.tracing.export.enabled=false",
"management.wavefront.api-token=abcde")
.run((context) -> assertThat(context).doesNotHaveBean(WavefrontSender.class));
}
@Test
void shouldSupplyWavefrontSenderIfOnlyGlobalTracingIsDisabled() {
this.contextRunner
.withPropertyValues("management.tracing.enabled=false", "management.wavefront.api-token=abcde")
.run((context) -> assertThat(context).hasSingleBean(WavefrontSender.class));
}
@Test
void shouldSupplyWavefrontSenderIfOnlyWavefrontTracingIsDisabled() {
this.contextRunner
.withPropertyValues("management.wavefront.tracing.export.enabled=false",
"management.wavefront.api-token=abcde")
.run((context) -> assertThat(context).hasSingleBean(WavefrontSender.class));
}