diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java deleted file mode 100644 index 4a551215f62..00000000000 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BaggageTagSpanHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2012-2023 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.Collection; - -import brave.Tags; -import brave.baggage.BaggageField; -import brave.handler.MutableSpan; -import brave.handler.SpanHandler; -import brave.propagation.TraceContext; - -class BaggageTagSpanHandler extends SpanHandler { - - private final Collection fieldsToTag; - - BaggageTagSpanHandler(Collection fieldsToTag) { - this.fieldsToTag = fieldsToTag; - } - - @Override - public boolean end(TraceContext context, MutableSpan span, Cause cause) { - for (BaggageField field : this.fieldsToTag) { - Tags.BAGGAGE_FIELD.tag(field, context, span); - } - return true; - } - -} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java index b1fbb798c6d..eaa754c5e1e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java @@ -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. @@ -70,13 +70,17 @@ import org.springframework.core.env.Environment; BravePropagationConfigurations.NoPropagation.class }) public class BraveAutoConfiguration { - static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager(); - /** * Default value for application name if {@code spring.application.name} is not set. */ private static final String DEFAULT_APPLICATION_NAME = "application"; + private final TracingProperties tracingProperties; + + BraveAutoConfiguration(TracingProperties tracingProperties) { + this.tracingProperties = tracingProperties; + } + @Bean @ConditionalOnMissingBean @Order(Ordered.HIGHEST_PRECEDENCE) @@ -88,22 +92,22 @@ public class BraveAutoConfiguration { @Bean @ConditionalOnMissingBean - public Tracing braveTracing(Environment environment, TracingProperties properties, List spanHandlers, + Tracing braveTracing(Environment environment, List spanHandlers, List tracingCustomizers, CurrentTraceContext currentTraceContext, Factory propagationFactory, Sampler sampler) { - if (properties.getBrave().isSpanJoiningSupported()) { - if (properties.getPropagation().getType() != null - && properties.getPropagation().getType().contains(PropagationType.W3C)) { + if (this.tracingProperties.getBrave().isSpanJoiningSupported()) { + if (this.tracingProperties.getPropagation().getType() != null + && this.tracingProperties.getPropagation().getType().contains(PropagationType.W3C)) { throw new IncompatibleConfigurationException("management.tracing.propagation.type", "management.tracing.brave.span-joining-supported"); } - if (properties.getPropagation().getType() == null - && properties.getPropagation().getProduce().contains(PropagationType.W3C)) { + if (this.tracingProperties.getPropagation().getType() == null + && this.tracingProperties.getPropagation().getProduce().contains(PropagationType.W3C)) { throw new IncompatibleConfigurationException("management.tracing.propagation.produce", "management.tracing.brave.span-joining-supported"); } - if (properties.getPropagation().getType() == null - && properties.getPropagation().getConsume().contains(PropagationType.W3C)) { + if (this.tracingProperties.getPropagation().getType() == null + && this.tracingProperties.getPropagation().getConsume().contains(PropagationType.W3C)) { throw new IncompatibleConfigurationException("management.tracing.propagation.consume", "management.tracing.brave.span-joining-supported"); } @@ -112,7 +116,7 @@ public class BraveAutoConfiguration { Builder builder = Tracing.newBuilder() .currentTraceContext(currentTraceContext) .traceId128Bit(true) - .supportsJoin(properties.getBrave().isSpanJoiningSupported()) + .supportsJoin(this.tracingProperties.getBrave().isSpanJoiningSupported()) .propagationFactory(propagationFactory) .sampler(sampler) .localServiceName(applicationName); @@ -125,13 +129,13 @@ public class BraveAutoConfiguration { @Bean @ConditionalOnMissingBean - public brave.Tracer braveTracer(Tracing tracing) { + brave.Tracer braveTracer(Tracing tracing) { return tracing.tracer(); } @Bean @ConditionalOnMissingBean - public CurrentTraceContext braveCurrentTraceContext(List scopeDecorators, + CurrentTraceContext braveCurrentTraceContext(List scopeDecorators, List currentTraceContextCustomizers) { ThreadLocalCurrentTraceContext.Builder builder = ThreadLocalCurrentTraceContext.newBuilder(); scopeDecorators.forEach(builder::addScopeDecorator); @@ -143,14 +147,15 @@ public class BraveAutoConfiguration { @Bean @ConditionalOnMissingBean - public Sampler braveSampler(TracingProperties properties) { - return Sampler.create(properties.getSampling().getProbability()); + Sampler braveSampler() { + return Sampler.create(this.tracingProperties.getSampling().getProbability()); } @Bean @ConditionalOnMissingBean(io.micrometer.tracing.Tracer.class) BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext) { - return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), BRAVE_BAGGAGE_MANAGER); + return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), + new BraveBaggageManager(this.tracingProperties.getBaggage().getTagFields())); } @Bean diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BravePropagationConfigurations.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BravePropagationConfigurations.java index 203f8fb15d7..31de139c7df 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BravePropagationConfigurations.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BravePropagationConfigurations.java @@ -27,11 +27,11 @@ import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.baggage.CorrelationScopeCustomizer; import brave.baggage.CorrelationScopeDecorator; import brave.context.slf4j.MDCScopeDecorator; -import brave.handler.SpanHandler; import brave.propagation.CurrentTraceContext.ScopeDecorator; import brave.propagation.Propagation; import brave.propagation.Propagation.Factory; import brave.propagation.Propagation.KeyFactory; +import io.micrometer.tracing.brave.bridge.BraveBaggageManager; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Baggage.Correlation; @@ -41,7 +41,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; -import org.springframework.util.CollectionUtils; /** * Brave propagation configurations. They are imported by {@link BraveAutoConfiguration}. @@ -92,7 +91,8 @@ class BravePropagationConfigurations { baggagePropagationCustomizers.orderedStream() .forEach((customizer) -> customizer.customize(throwAwayBuilder)); CompositePropagationFactory propagationFactory = CompositePropagationFactory.create( - this.tracingProperties.getPropagation(), BraveAutoConfiguration.BRAVE_BAGGAGE_MANAGER, + this.tracingProperties.getPropagation(), + new BraveBaggageManager(this.tracingProperties.getBaggage().getTagFields()), LocalBaggageFields.extractFrom(throwAwayBuilder)); FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(propagationFactory); throwAwayBuilder.configs().forEach(builder::add); @@ -112,20 +112,12 @@ class BravePropagationConfigurations { } @Bean - @Order(0) BaggagePropagationCustomizer remoteFieldsBaggagePropagationCustomizer() { return (builder) -> { List remoteFields = this.tracingProperties.getBaggage().getRemoteFields(); for (String fieldName : remoteFields) { builder.add(BaggagePropagationConfig.SingleBaggageField.remote(BaggageField.create(fieldName))); } - }; - } - - @Bean - @Order(1) - BaggagePropagationCustomizer localFieldsBaggagePropagationCustomizer() { - return (builder) -> { List localFields = this.tracingProperties.getBaggage().getLocalFields(); for (String localFieldName : localFields) { builder.add(BaggagePropagationConfig.SingleBaggageField.local(BaggageField.create(localFieldName))); @@ -133,16 +125,6 @@ class BravePropagationConfigurations { }; } - @Bean - @Order(2) - SpanHandler baggageTagSpanHandler() { - List tagFields = this.tracingProperties.getBaggage().getTagFields(); - if (CollectionUtils.isEmpty(tagFields)) { - return SpanHandler.NOOP; - } - return new BaggageTagSpanHandler(tagFields.stream().map(BaggageField::create).toList()); - } - @Bean @ConditionalOnMissingBean @ConditionalOnEnabledTracing diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java index b6b5d356aad..f09abe23163 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java @@ -26,7 +26,6 @@ import brave.Span; import brave.SpanCustomizer; import brave.Tracer; import brave.Tracing; -import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; import brave.handler.SpanHandler; @@ -345,22 +344,6 @@ class BraveAutoConfigurationTests { }); } - @Test - void shouldCreateTagHandler() { - this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=country-code,bp") - .run((context) -> assertThat(context.getBean(BaggageTagSpanHandler.class)).extracting("fieldsToTag") - .asInstanceOf(InstanceOfAssertFactories.list(BaggageField.class)) - .extracting(BaggageField::name) - .containsExactlyInAnyOrder("country-code", "bp")); - } - - @Test - void noopOnNoTagFields() { - this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=") - .run((context) -> assertThat(context.getBean("baggageTagSpanHandler", SpanHandler.class)) - .isSameAs(SpanHandler.NOOP)); - } - @Test void shouldDisablePropagationIfTracingIsDisabled() { this.contextRunner.withPropertyValues("management.tracing.enabled=false").run((context) -> { @@ -371,6 +354,16 @@ class BraveAutoConfigurationTests { }); } + @Test + void shouldConfigureTaggedFields() { + this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=t1").run((context) -> { + BraveTracer braveTracer = context.getBean(BraveTracer.class); + assertThat(braveTracer).extracting("braveBaggageManager.tagFields") + .asInstanceOf(InstanceOfAssertFactories.list(String.class)) + .containsExactly("t1"); + }); + } + private void injectToMap(Map map, String key, String value) { map.put(key, value); }