Skip to content

Commit f900d0b

Browse files
committed
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
1 parent 6c0c9b9 commit f900d0b

File tree

13 files changed

+306
-47
lines changed

13 files changed

+306
-47
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/ConditionalOnEnabledTracing.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,21 +22,30 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2625
import org.springframework.context.annotation.Conditional;
2726

2827
/**
2928
* {@link Conditional @Conditional} that checks whether tracing is enabled. It matches if
3029
* the value of the {@code management.tracing.enabled} property is {@code true} or if it
31-
* is not configured.
30+
* is not configured. If the {@link #value() tracing exporter name} is set, the
31+
* {@code management.<name>.tracing.export.enabled} property can be used to control the
32+
* behavior for the specific tracing exporter. In that case, the exporter specific
33+
* property takes precedence over the global property.
3234
*
3335
* @author Moritz Halbritter
3436
* @since 3.0.0
3537
*/
3638
@Retention(RetentionPolicy.RUNTIME)
3739
@Target({ ElementType.TYPE, ElementType.METHOD })
3840
@Documented
39-
@ConditionalOnProperty(prefix = "management.tracing", name = "enabled", matchIfMissing = true)
41+
@Conditional(OnEnabledTracingCondition.class)
4042
public @interface ConditionalOnEnabledTracing {
4143

44+
/**
45+
* Name of the tracing exporter.
46+
* @return the name of the tracing exporter
47+
* @since 3.4.0
48+
*/
49+
String value() default "";
50+
4251
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2024 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.actuate.autoconfigure.tracing;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
22+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
23+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
24+
import org.springframework.context.annotation.ConditionContext;
25+
import org.springframework.core.type.AnnotatedTypeMetadata;
26+
import org.springframework.util.StringUtils;
27+
28+
/**
29+
* {@link SpringBootCondition} to check whether tracing is enabled.
30+
*
31+
* @author Moritz Halbritter
32+
* @see ConditionalOnEnabledTracing
33+
*/
34+
class OnEnabledTracingCondition extends SpringBootCondition {
35+
36+
private static final String GLOBAL_PROPERTY = "management.tracing.enabled";
37+
38+
private static final String EXPORTER_PROPERTY = "management.%s.tracing.export.enabled";
39+
40+
@Override
41+
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
42+
String tracingExporter = getExporterName(metadata);
43+
if (StringUtils.hasLength(tracingExporter)) {
44+
Boolean exporterTracingEnabled = context.getEnvironment()
45+
.getProperty(EXPORTER_PROPERTY.formatted(tracingExporter), Boolean.class);
46+
if (exporterTracingEnabled != null) {
47+
return new ConditionOutcome(exporterTracingEnabled,
48+
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
49+
.because(EXPORTER_PROPERTY.formatted(tracingExporter) + " is " + exporterTracingEnabled));
50+
}
51+
}
52+
Boolean globalTracingEnabled = context.getEnvironment().getProperty(GLOBAL_PROPERTY, Boolean.class);
53+
if (globalTracingEnabled != null) {
54+
return new ConditionOutcome(globalTracingEnabled,
55+
ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
56+
.because(GLOBAL_PROPERTY + " is " + globalTracingEnabled));
57+
}
58+
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledTracing.class)
59+
.because("tracing is enabled by default"));
60+
}
61+
62+
private static String getExporterName(AnnotatedTypeMetadata metadata) {
63+
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName());
64+
if (attributes == null) {
65+
return null;
66+
}
67+
return (String) attributes.get("value");
68+
}
69+
70+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpTracingConfigurations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ static class Exporters {
7272
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
7373
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
7474
@ConditionalOnBean(OtlpTracingConnectionDetails.class)
75-
@ConditionalOnEnabledTracing
75+
@ConditionalOnEnabledTracing("otlp")
7676
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
7777
OtlpTracingConnectionDetails connectionDetails) {
7878
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/wavefront/WavefrontTracingAutoConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ public class WavefrontTracingAutoConfiguration {
5959
@Bean
6060
@ConditionalOnMissingBean
6161
@ConditionalOnBean(WavefrontSender.class)
62-
@ConditionalOnEnabledTracing
62+
@ConditionalOnEnabledTracing("wavefront")
6363
WavefrontSpanHandler wavefrontSpanHandler(WavefrontProperties properties, WavefrontSender wavefrontSender,
6464
SpanMetrics spanMetrics, ApplicationTags applicationTags) {
6565
return new WavefrontSpanHandler(properties.getSender().getMaxQueueSize(), wavefrontSender, spanMetrics,
@@ -96,7 +96,7 @@ static class WavefrontBrave {
9696

9797
@Bean
9898
@ConditionalOnMissingBean
99-
@ConditionalOnEnabledTracing
99+
@ConditionalOnEnabledTracing("wavefront")
100100
WavefrontBraveSpanHandler wavefrontBraveSpanHandler(WavefrontSpanHandler wavefrontSpanHandler) {
101101
return new WavefrontBraveSpanHandler(wavefrontSpanHandler);
102102
}
@@ -109,7 +109,7 @@ static class WavefrontOpenTelemetry {
109109

110110
@Bean
111111
@ConditionalOnMissingBean
112-
@ConditionalOnEnabledTracing
112+
@ConditionalOnEnabledTracing("wavefront")
113113
WavefrontOtelSpanExporter wavefrontOtelSpanExporter(WavefrontSpanHandler wavefrontSpanHandler) {
114114
return new WavefrontOtelSpanExporter(wavefrontSpanHandler);
115115
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ BytesEncoder<MutableSpan> mutableSpanBytesEncoder(Encoding encoding,
187187
@Bean
188188
@ConditionalOnMissingBean
189189
@ConditionalOnBean(BytesMessageSender.class)
190-
@ConditionalOnEnabledTracing
190+
@ConditionalOnEnabledTracing("zipkin")
191191
AsyncZipkinSpanHandler asyncZipkinSpanHandler(BytesMessageSender sender,
192192
BytesEncoder<MutableSpan> mutableSpanBytesEncoder) {
193193
return AsyncZipkinSpanHandler.newBuilder(sender).build(mutableSpanBytesEncoder);
@@ -208,7 +208,7 @@ BytesEncoder<Span> spanBytesEncoder(Encoding encoding) {
208208
@Bean
209209
@ConditionalOnMissingBean
210210
@ConditionalOnBean(BytesMessageSender.class)
211-
@ConditionalOnEnabledTracing
211+
@ConditionalOnEnabledTracing("zipkin")
212212
ZipkinSpanExporter zipkinSpanExporter(BytesMessageSender sender, BytesEncoder<Span> spanBytesEncoder) {
213213
return ZipkinSpanExporter.builder().setSender(sender).setEncoder(spanBytesEncoder).build();
214214
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontSenderConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ static final class WavefrontTracingOrMetricsCondition extends AnyNestedCondition
6969
super(ConfigurationPhase.REGISTER_BEAN);
7070
}
7171

72-
@ConditionalOnEnabledTracing
72+
@ConditionalOnEnabledTracing("wavefront")
7373
static class TracingCondition {
7474

7575
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,11 @@
20932093
"name": "management.otlp.tracing.compression",
20942094
"defaultValue": "none"
20952095
},
2096+
{
2097+
"name": "management.otlp.tracing.export.enabled",
2098+
"type": "java.lang.Boolean",
2099+
"description": "Whether auto-configuration of tracing is enabled to export OTLP traces."
2100+
},
20962101
{
20972102
"name": "management.prometheus.metrics.export.histogram-flavor",
20982103
"defaultValue": "prometheus"
@@ -2258,12 +2263,22 @@
22582263
"W3C"
22592264
]
22602265
},
2266+
{
2267+
"name": "management.wavefront.tracing.export.enabled",
2268+
"type": "java.lang.Boolean",
2269+
"description": "Whether auto-configuration of tracing is enabled to export Wavefront traces."
2270+
},
22612271
{
22622272
"name": "management.zipkin.tracing.encoding",
22632273
"defaultValue": [
22642274
"JSON"
22652275
]
22662276
},
2277+
{
2278+
"name": "management.zipkin.tracing.export.enabled",
2279+
"type": "java.lang.Boolean",
2280+
"description": "Whether auto-configuration of tracing is enabled to export Zipkin traces."
2281+
},
22672282
{
22682283
"name": "micrometer.observations.annotations.enabled",
22692284
"type": "java.lang.Boolean",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2012-2024 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.actuate.autoconfigure.tracing;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
25+
import org.springframework.context.annotation.ConditionContext;
26+
import org.springframework.core.type.AnnotatedTypeMetadata;
27+
import org.springframework.mock.env.MockEnvironment;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.BDDMockito.given;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Tests for {@link OnEnabledTracingCondition}.
35+
*
36+
* @author Moritz Halbritter
37+
*/
38+
class OnEnabledTracingConditionTests {
39+
40+
@Test
41+
void shouldMatchIfNoPropertyIsSet() {
42+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
43+
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(), mockMetaData(""));
44+
assertThat(outcome.isMatch()).isTrue();
45+
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing tracing is enabled by default");
46+
}
47+
48+
@Test
49+
void shouldNotMatchIfGlobalPropertyIsFalse() {
50+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
51+
ConditionOutcome outcome = condition
52+
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "false")), mockMetaData(""));
53+
assertThat(outcome.isMatch()).isFalse();
54+
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is false");
55+
}
56+
57+
@Test
58+
void shouldMatchIfGlobalPropertyIsTrue() {
59+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
60+
ConditionOutcome outcome = condition
61+
.getMatchOutcome(mockConditionContext(Map.of("management.tracing.enabled", "true")), mockMetaData(""));
62+
assertThat(outcome.isMatch()).isTrue();
63+
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledTracing management.tracing.enabled is true");
64+
}
65+
66+
@Test
67+
void shouldNotMatchIfExporterPropertyIsFalse() {
68+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
69+
ConditionOutcome outcome = condition.getMatchOutcome(
70+
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "false")),
71+
mockMetaData("zipkin"));
72+
assertThat(outcome.isMatch()).isFalse();
73+
assertThat(outcome.getMessage())
74+
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false");
75+
}
76+
77+
@Test
78+
void shouldMatchIfExporterPropertyIsTrue() {
79+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
80+
ConditionOutcome outcome = condition.getMatchOutcome(
81+
mockConditionContext(Map.of("management.zipkin.tracing.export.enabled", "true")),
82+
mockMetaData("zipkin"));
83+
assertThat(outcome.isMatch()).isTrue();
84+
assertThat(outcome.getMessage())
85+
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true");
86+
}
87+
88+
@Test
89+
void exporterPropertyShouldOverrideGlobalPropertyIfTrue() {
90+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
91+
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
92+
Map.of("management.tracing.enabled", "false", "management.zipkin.tracing.export.enabled", "true")),
93+
mockMetaData("zipkin"));
94+
assertThat(outcome.isMatch()).isTrue();
95+
assertThat(outcome.getMessage())
96+
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is true");
97+
}
98+
99+
@Test
100+
void exporterPropertyShouldOverrideGlobalPropertyIfFalse() {
101+
OnEnabledTracingCondition condition = new OnEnabledTracingCondition();
102+
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
103+
Map.of("management.tracing.enabled", "true", "management.zipkin.tracing.export.enabled", "false")),
104+
mockMetaData("zipkin"));
105+
assertThat(outcome.isMatch()).isFalse();
106+
assertThat(outcome.getMessage())
107+
.isEqualTo("@ConditionalOnEnabledTracing management.zipkin.tracing.export.enabled is false");
108+
}
109+
110+
private ConditionContext mockConditionContext() {
111+
return mockConditionContext(Collections.emptyMap());
112+
}
113+
114+
private ConditionContext mockConditionContext(Map<String, String> properties) {
115+
ConditionContext context = mock(ConditionContext.class);
116+
MockEnvironment environment = new MockEnvironment();
117+
properties.forEach(environment::setProperty);
118+
given(context.getEnvironment()).willReturn(environment);
119+
return context;
120+
}
121+
122+
private AnnotatedTypeMetadata mockMetaData(String exporter) {
123+
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
124+
given(metadata.getAnnotationAttributes(ConditionalOnEnabledTracing.class.getName()))
125+
.willReturn(Map.of("value", exporter));
126+
return metadata;
127+
}
128+
129+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/otlp/OtlpAutoConfigurationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ void shouldSupplyBeans() {
5959
}
6060

6161
@Test
62-
void shouldNotSupplyBeansIfTracingIsDisabled() {
62+
void shouldNotSupplyBeansIfGlobalTracingIsDisabled() {
6363
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
6464
.run((context) -> assertThat(context).doesNotHaveBean(SpanExporter.class));
6565
}
6666

67+
@Test
68+
void shouldNotSupplyBeansIfOtlpTracingIsDisabled() {
69+
this.contextRunner.withPropertyValues("management.otlp.tracing.export.enabled=false")
70+
.run((context) -> assertThat(context).doesNotHaveBean(SpanExporter.class));
71+
}
72+
6773
@Test
6874
void shouldNotSupplyBeansIfTracingBridgeIsMissing() {
6975
this.contextRunner.withClassLoader(new FilteredClassLoader("io.micrometer.tracing"))

0 commit comments

Comments
 (0)