From a83621ebb26f3f6423ad9d490e82083881821da1 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 23 Jun 2020 12:38:01 +0200 Subject: [PATCH] Adds support for the new properties in baggage propagation --- .../autoconfig/SleuthBaggageProperties.java | 116 ++++++++ .../autoconfig/TraceAutoConfiguration.java | 2 +- .../autoconfig/TraceBaggageConfiguration.java | 262 +++++++++++++++--- ...raceAutoConfigurationCustomizersTests.java | 105 ++++++- 4 files changed, 426 insertions(+), 59 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthBaggageProperties.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthBaggageProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthBaggageProperties.java new file mode 100644 index 000000000..81a44c4da --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthBaggageProperties.java @@ -0,0 +1,116 @@ +/* + * Copyright 2013-2019 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.cloud.sleuth.autoconfig; + +import java.util.ArrayList; +import java.util.List; + +import brave.Tags; +import brave.baggage.BaggageField; +import brave.baggage.BaggagePropagationConfig; +import brave.baggage.CorrelationScopeConfig; +import brave.baggage.CorrelationScopeDecorator; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Sleuth Baggage settings. + * + * @author Adrian Cole + * @since 2.2.4 + */ +@ConfigurationProperties("spring.sleuth.baggage") +class SleuthBaggageProperties { + + /** + * Adds a {@link CorrelationScopeDecorator} to put baggage values into the correlation + * context. + */ + private boolean correlationEnabled = true; + + /** + * A list of {@link BaggageField#name() fields} to add to correlation (MDC) context. + * + * @see CorrelationScopeConfig.SingleCorrelationField#create(BaggageField) + */ + private List correlationFields = new ArrayList<>(); + + /** + * Same as {@link #remoteFields} except that this field is not propagated to remote + * services. + * + * @see BaggagePropagationConfig.SingleBaggageField#local(BaggageField) + */ + private List localFields = new ArrayList<>(); + + /** + * List of fields that are referenced the same in-process as it is on the wire. For + * example, the field "x-vcap-request-id" would be set as-is including the prefix. + * + * @see BaggagePropagationConfig.SingleBaggageField#remote(BaggageField) + * @see BaggagePropagationConfig.SingleBaggageField.Builder#addKeyName(String) + */ + private List remoteFields = new ArrayList<>(); + + /** + * A list of {@link BaggageField#name() fields} to tag into the span. + * + * @see Tags#BAGGAGE_FIELD + */ + private List tagFields = new ArrayList<>(); + + public boolean isCorrelationEnabled() { + return correlationEnabled; + } + + public void setCorrelationEnabled(boolean correlationEnabled) { + this.correlationEnabled = correlationEnabled; + } + + public List getCorrelationFields() { + return correlationFields; + } + + public void setCorrelationFields(List correlationFields) { + this.correlationFields = correlationFields; + } + + public List getLocalFields() { + return localFields; + } + + public void setLocalFields(List localFields) { + this.localFields = localFields; + } + + public List getRemoteFields() { + return remoteFields; + } + + public void setRemoteFields(List remoteFields) { + this.remoteFields = remoteFields; + } + + public List getTagFields() { + return this.tagFields; + } + + public void setTagFields(List tagFields) { + this.tagFields = tagFields; + } + +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java index 14fe86070..035535946 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java @@ -71,7 +71,7 @@ import org.springframework.util.StringUtils; */ @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true) -@EnableConfigurationProperties(SleuthProperties.class) +@EnableConfigurationProperties({ SleuthProperties.class, SleuthBaggageProperties.class }) @Import({ SleuthLogAutoConfiguration.class, TraceBaggageConfiguration.class, SamplerAutoConfiguration.class }) // public allows @AutoConfigureAfter(TraceAutoConfiguration) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.java index 6b58ac6cf..5755391bc 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceBaggageConfiguration.java @@ -19,24 +19,40 @@ package org.springframework.cloud.sleuth.autoconfig; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import brave.Tags; import brave.baggage.BaggageField; import brave.baggage.BaggagePropagation; import brave.baggage.BaggagePropagationConfig.SingleBaggageField; import brave.baggage.BaggagePropagationCustomizer; +import brave.baggage.CorrelationScopeConfig.SingleCorrelationField; +import brave.baggage.CorrelationScopeCustomizer; +import brave.baggage.CorrelationScopeDecorator; +import brave.context.slf4j.MDCScopeDecorator; +import brave.handler.MutableSpan; +import brave.handler.SpanHandler; import brave.propagation.B3Propagation; -import brave.propagation.B3Propagation.Format; +import brave.propagation.CurrentTraceContext.ScopeDecorator; import brave.propagation.ExtraFieldCustomizer; import brave.propagation.ExtraFieldPropagation; import brave.propagation.Propagation; +import brave.propagation.TraceContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.lang.Nullable; @@ -49,7 +65,7 @@ import org.springframework.lang.Nullable; * @since 2.0.0 */ @Configuration(proxyBeanMethods = false) -@EnableConfigurationProperties(SleuthProperties.class) +@EnableConfigurationProperties({ SleuthProperties.class, SleuthBaggageProperties.class }) class TraceBaggageConfiguration { static final Log logger = LogFactory.getLog(TraceBaggageConfiguration.class); @@ -57,11 +73,13 @@ class TraceBaggageConfiguration { static final String LOCAL_KEYS = "spring.sleuth.local-keys"; static final String BAGGAGE_KEYS = "spring.sleuth.baggage-keys"; static final String PROPAGATION_KEYS = "spring.sleuth.propagation-keys"; + static final String WHITELISTED_KEYS = "spring.sleuth.propagation.tag.whitelisted-keys"; + static final String WHITELISTED_MDC_KEYS = "spring.sleuth.log.slf4j.whitelisted-mdc-keys"; // Note: Versions <2.2.3 use injectFormat(MULTI) for non-remote (ex spring-messaging) // See #1643 static final Propagation.Factory B3_FACTORY = B3Propagation.newFactoryBuilder() - .injectFormat(Format.SINGLE_NO_PARENT).build(); + .injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build(); // These List beans allow us to get deprecated property values, regardless of // if they were comma or yaml encoded. This keeps them out of SleuthBaggageProperties @@ -84,14 +102,20 @@ class TraceBaggageConfiguration { return new ArrayList<>(); } + @Bean(WHITELISTED_MDC_KEYS) + @ConfigurationProperties(WHITELISTED_MDC_KEYS) + List whiteListedMDCKeys() { + return new ArrayList<>(); + } + /** * To override the underlying context format, override this bean and set the delegate * to what you need. {@link BaggagePropagation.FactoryBuilder} will unwrap itself if * no fields are configured. * *

- * This will use {@link Format#SINGLE_NO_PARENT} for non-remote spans, such as for - * messaging. Note: it will still parse incoming multi-header spans. + * This will use {@link B3Propagation.Format#SINGLE_NO_PARENT} for non-remote spans, + * such as for messaging. Note: it will still parse incoming multi-header spans. */ @Bean @ConditionalOnMissingBean @@ -99,6 +123,69 @@ class TraceBaggageConfiguration { return BaggagePropagation.newFactoryBuilder(B3_FACTORY); } + @Bean + @ConditionalOnMissingBean + Propagation.Factory sleuthPropagation( + @Nullable ExtraFieldPropagation.FactoryBuilder extraFieldPropagationFactoryBuilder, + @Nullable List extraFieldCustomizers, + BaggagePropagation.FactoryBuilder factoryBuilder, + @Qualifier(BAGGAGE_KEYS) List baggageKeys, + @Qualifier(LOCAL_KEYS) List localKeys, + @Qualifier(PROPAGATION_KEYS) List propagationKeys, + SleuthBaggageProperties sleuthBaggageProperties, + @Nullable List baggagePropagationCustomizers) { + + boolean useDeprecated = false; + if (extraFieldPropagationFactoryBuilder != null) { + logger.warn("ExtraFieldPropagation.FactoryBuilder is deprecated. " + + "Please switch to BaggagePropagation.FactoryBuilder"); + useDeprecated = true; + } + if (extraFieldCustomizers != null) { + logger.warn("ExtraFieldCustomizer is deprecated. " + + "Please switch to BaggagePropagationCustomizer"); + useDeprecated = true; + } + if (useDeprecated) { + return sleuthPropagation(extraFieldPropagationFactoryBuilder, localKeys, + propagationKeys, baggageKeys, extraFieldCustomizers); + } + + Set localFields = redirectOldPropertyToNew(LOCAL_KEYS, localKeys, + "spring.sleuth.baggage.local-fields", + sleuthBaggageProperties.getLocalFields()); + for (String fieldName : localFields) { + factoryBuilder.add(SingleBaggageField.local(BaggageField.create(fieldName))); + } + + Set remoteFields = redirectOldPropertyToNew(PROPAGATION_KEYS, + propagationKeys, "spring.sleuth.baggage.remote-fields", + sleuthBaggageProperties.getRemoteFields()); + for (String fieldName : remoteFields) { + factoryBuilder.add(SingleBaggageField.remote(BaggageField.create(fieldName))); + } + + if (!baggageKeys.isEmpty()) { + logger.warn("'" + BAGGAGE_KEYS + "' will be removed in a future release.\n" + + "To change header names define a @Bean of type " + + SingleBaggageField.class.getName()); + + for (String key : baggageKeys) { + factoryBuilder.add(SingleBaggageField.newBuilder(BaggageField.create(key)) + .addKeyName("baggage-" + key) // for HTTP + .addKeyName("baggage_" + key) // for messaging + .build()); + } + } + + if (baggagePropagationCustomizers != null) { + for (BaggagePropagationCustomizer customizer : baggagePropagationCustomizers) { + customizer.customize(factoryBuilder); + } + } + return factoryBuilder.build(); + } + Propagation.Factory sleuthPropagation( ExtraFieldPropagation.FactoryBuilder extraFieldPropagationFactoryBuilder, List baggageKeys, List localKeys, @@ -112,7 +199,8 @@ class TraceBaggageConfiguration { factoryBuilder = extraFieldPropagationFactoryBuilder; } else { - factoryBuilder = ExtraFieldPropagation.newFactoryBuilder(B3_FACTORY); + factoryBuilder = ExtraFieldPropagation + .newFactoryBuilder(B3Propagation.FACTORY); } if (!baggageKeys.isEmpty()) { factoryBuilder @@ -133,54 +221,140 @@ class TraceBaggageConfiguration { return factoryBuilder.build(); } + static Set redirectOldPropertyToNew(String oldProperty, List oldValue, + String newProperty, List newValue) { + Set result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + result.addAll(newValue); + if (!oldValue.isEmpty()) { + logger.warn("'" + oldProperty + "' has been renamed to '" + newProperty + + "' and will be removed in a future release."); + result.addAll(oldValue); // dedupes + } + return result; + } + @Bean @ConditionalOnMissingBean - Propagation.Factory sleuthPropagation( - @Nullable ExtraFieldPropagation.FactoryBuilder extraFieldPropagationFactoryBuilder, - @Nullable List extraFieldCustomizers, - BaggagePropagation.FactoryBuilder factoryBuilder, - @Qualifier(BAGGAGE_KEYS) List baggageKeys, - @Qualifier(LOCAL_KEYS) List localKeys, - @Qualifier(PROPAGATION_KEYS) List propagationKeys, - @Nullable List baggagePropagationCustomizers) { + @ConditionalOnClass(MDC.class) + CorrelationScopeDecorator.Builder correlationScopeDecoratorBuilder() { + return MDCScopeDecorator.newBuilder(); + } - boolean useDeprecated = false; - if (extraFieldPropagationFactoryBuilder != null) { - logger.warn("ExtraFieldPropagation.FactoryBuilder is deprecated. " - + "Please switch to BaggagePropagation.FactoryBuilder"); - useDeprecated = true; - } - if (extraFieldCustomizers != null) { - logger.warn("ExtraFieldCustomizer is deprecated. " - + "Please switch to BaggagePropagationCustomizer"); - useDeprecated = true; - } - if (useDeprecated) { - return sleuthPropagation(extraFieldPropagationFactoryBuilder, localKeys, - propagationKeys, baggageKeys, extraFieldCustomizers); - } + @Bean + @ConditionalOnMissingBean(CorrelationScopeDecorator.class) + @ConditionalOnBean(CorrelationScopeDecorator.Builder.class) + @ConditionalOnProperty(value = "spring.sleuth.baggage.correlation-enabled", + matchIfMissing = true) + ScopeDecorator correlationScopeDecorator( + @Qualifier(WHITELISTED_MDC_KEYS) List whiteListedMDCKeys, + SleuthBaggageProperties sleuthBaggageProperties, + @Nullable List correlationScopeCustomizers) { - for (String fieldName : localKeys) { - factoryBuilder.add(SingleBaggageField.local(BaggageField.create(fieldName))); - } + Set correlationFields = redirectOldPropertyToNew(WHITELISTED_MDC_KEYS, + whiteListedMDCKeys, "spring.sleuth.baggage.correlation-fields", + sleuthBaggageProperties.getCorrelationFields()); - for (String fieldName : propagationKeys) { - factoryBuilder.add(SingleBaggageField.remote(BaggageField.create(fieldName))); - } - - for (String key : baggageKeys) { - factoryBuilder.add(SingleBaggageField.newBuilder(BaggageField.create(key)) - .addKeyName("baggage-" + key) // for HTTP - .addKeyName("baggage_" + key) // for messaging + // Add fields from properties + CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder(); + for (String field : correlationFields) { + builder.add(SingleCorrelationField.newBuilder(BaggageField.create(field)) .build()); } - if (baggagePropagationCustomizers != null) { - for (BaggagePropagationCustomizer customizer : baggagePropagationCustomizers) { - customizer.customize(factoryBuilder); + // handle user overrides + if (correlationScopeCustomizers != null) { + for (CorrelationScopeCustomizer customizer : correlationScopeCustomizers) { + customizer.customize(builder); } } - return factoryBuilder.build(); + return builder.build(); + } + + /** + * This has to be conditional as it creates a bean of type {@link SpanHandler}. + * + *

+ * {@link SpanHandler} beans, even if {@link SpanHandler#NOOP}, can trigger + * {@code org.springframework.cloud.sleuth.sampler.SamplerCondition} + */ + @Configuration + @Conditional(BaggageTagSpanHandlerCondition.class) + @EnableConfigurationProperties(SleuthBaggageProperties.class) + static class BaggageTagSpanHandlerConfiguration { + + @Bean(WHITELISTED_KEYS) + @ConfigurationProperties(WHITELISTED_KEYS) + List whiteListedKeys() { + return new ArrayList<>(); + } + + @Bean + SpanHandler baggageTagSpanHandler( + @Qualifier(WHITELISTED_KEYS) List whiteListedKeys, + SleuthBaggageProperties sleuthBaggageProperties) { + + Set tagFields = redirectOldPropertyToNew(WHITELISTED_KEYS, + whiteListedKeys, "spring.sleuth.baggage.tag-fields", + sleuthBaggageProperties.getTagFields()); + + if (tagFields.isEmpty()) { + return SpanHandler.NOOP; // Brave ignores these + } + + return new BaggageTagSpanHandler(tagFields.stream().map(BaggageField::create) + .toArray(BaggageField[]::new)); + } + + } + + /** + * We need a special condition as it users could use either comma or yaml encoding, + * possibly with a deprecated prefix. + */ + static class BaggageTagSpanHandlerCondition extends AnyNestedCondition { + + BaggageTagSpanHandlerCondition() { + super(ConfigurationPhase.PARSE_CONFIGURATION); + } + + @ConditionalOnProperty("spring.sleuth.baggage.tag-fields") + static class TagFieldsProperty { + + } + + @ConditionalOnProperty("spring.sleuth.baggage.tag-fields[0]") + static class TagFieldsYamlListProperty { + + } + + @ConditionalOnProperty(WHITELISTED_KEYS) + static class WhitelistedKeysProperty { + + } + + @ConditionalOnProperty(WHITELISTED_KEYS + "[0]") + static class WhitelistedKeysYamlListProperty { + + } + + } + + static final class BaggageTagSpanHandler extends SpanHandler { + + final BaggageField[] fieldsToTag; + + BaggageTagSpanHandler(BaggageField[] fieldsToTag) { + this.fieldsToTag = fieldsToTag; + } + + @Override + public boolean end(TraceContext context, MutableSpan span, Cause cause) { + for (BaggageField field : fieldsToTag) { + Tags.BAGGAGE_FIELD.tag(field, context, span); + } + return true; + } + } } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfigurationCustomizersTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfigurationCustomizersTests.java index 8a0c2689a..252b23b5c 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfigurationCustomizersTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfigurationCustomizersTests.java @@ -17,6 +17,7 @@ package org.springframework.cloud.sleuth.autoconfig; import brave.TracingCustomizer; +import brave.baggage.BaggagePropagationCustomizer; import brave.http.HttpTracingCustomizer; import brave.messaging.MessagingTracingCustomizer; import brave.propagation.CurrentTraceContextCustomizer; @@ -24,15 +25,16 @@ import brave.propagation.ExtraFieldCustomizer; import brave.propagation.Propagation; import brave.rpc.RpcTracingCustomizer; import brave.sampler.Sampler; -import org.junit.Test; +import org.assertj.core.api.BDDAssertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.sleuth.instrument.messaging.TraceMessagingAutoConfiguration; import org.springframework.cloud.sleuth.instrument.rpc.TraceRpcAutoConfiguration; import org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration; -import org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.support.MessageHeaderAccessor; @@ -43,29 +45,82 @@ public class TraceAutoConfigurationCustomizersTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class, - TraceWebAutoConfiguration.class, TraceHttpAutoConfiguration.class, - TraceRpcAutoConfiguration.class, - FakeSpringMessagingAutoConfiguration.class, - TraceMessagingAutoConfiguration.class)) + TraceHttpAutoConfiguration.class, TraceRpcAutoConfiguration.class, + TraceMessagingAutoConfiguration.class, + FakeSpringMessagingAutoConfiguration.class)) .withUserConfiguration(Customizers.class); + @Test + public void should_apply_deprecated_customizers() { + this.contextRunner.withUserConfiguration(DeprecatedCustomizers.class) + .withPropertyValues("spring.sleuth.baggage-keys=my-baggage") + .run((context) -> { + Customizers bean = context.getBean(Customizers.class); + DeprecatedCustomizers deprecated = context + .getBean(DeprecatedCustomizers.class); + + shouldApplyOldCustomizations(bean, deprecated); + shouldNotOverrideTheDefaults(context); + }); + } + + @Test + public void should_apply_deprecated_customizers_with_new_values() { + this.contextRunner.withUserConfiguration(DeprecatedCustomizers.class) + .withPropertyValues("spring.sleuth.baggage.remote-fields=country-code") + .run((context) -> { + Customizers bean = context.getBean(Customizers.class); + DeprecatedCustomizers deprecated = context + .getBean(DeprecatedCustomizers.class); + + shouldApplyOldCustomizations(bean, deprecated); + shouldNotOverrideTheDefaults(context); + }); + } + @Test public void should_apply_customizers() { this.contextRunner.withPropertyValues("spring.sleuth.baggage-keys=my-baggage") .run((context) -> { Customizers bean = context.getBean(Customizers.class); + assertThatDeprecatedCustomizersAreNotDefined(context); - shouldApplyCustomizations(bean); + shouldApplyNewCustomizations(bean); + shouldNotOverrideTheDefaults(context); + }); + } + + private void assertThatDeprecatedCustomizersAreNotDefined( + AssertableApplicationContext context) { + try { + context.getBean(DeprecatedCustomizers.class); + BDDAssertions.fail("DeprecatedCustomizers bean should not be defined"); + } + catch (NoSuchBeanDefinitionException ex) { + + } + } + + @Test + public void should_apply_customizers_with_new_values() { + this.contextRunner + .withPropertyValues("spring.sleuth.baggage.remote-fields=country-code") + .run((context) -> { + Customizers bean = context.getBean(Customizers.class); + assertThatDeprecatedCustomizersAreNotDefined(context); + + shouldApplyNewCustomizations(bean); shouldNotOverrideTheDefaults(context); }); } @Test - public void should_apply_extra_field_customizer_when_no_extra_properties_are_defined() { + public void should_apply_baggage_customizer_when_no_baggage_properties_are_defined() { this.contextRunner.run((context) -> { Customizers bean = context.getBean(Customizers.class); + assertThatDeprecatedCustomizersAreNotDefined(context); - shouldApplyCustomizations(bean); + shouldApplyNewCustomizations(bean); }); } @@ -73,10 +128,20 @@ public class TraceAutoConfigurationCustomizersTests { then(context.getBean(Sampler.class)).isSameAs(Sampler.ALWAYS_SAMPLE); } - private void shouldApplyCustomizations(Customizers bean) { + private void shouldApplyOldCustomizations(Customizers bean, + DeprecatedCustomizers deprecated) { then(bean.tracingCustomizerApplied).isTrue(); then(bean.contextCustomizerApplied).isTrue(); - then(bean.extraFieldCustomizerApplied).isTrue(); + then(deprecated.extraFieldCustomizerApplied).isTrue(); + then(bean.baggagePropagationCustomizerApplied).isFalse(); + then(bean.httpCustomizerApplied).isTrue(); + then(bean.rpcCustomizerApplied).isTrue(); + } + + private void shouldApplyNewCustomizations(Customizers bean) { + then(bean.tracingCustomizerApplied).isTrue(); + then(bean.contextCustomizerApplied).isTrue(); + then(bean.baggagePropagationCustomizerApplied).isTrue(); then(bean.httpCustomizerApplied).isTrue(); then(bean.rpcCustomizerApplied).isTrue(); } @@ -92,6 +157,18 @@ public class TraceAutoConfigurationCustomizersTests { } + @Configuration + static class DeprecatedCustomizers { + + boolean extraFieldCustomizerApplied; + + @Bean + ExtraFieldCustomizer sleuthExtraFieldCustomizer() { + return builder -> extraFieldCustomizerApplied = true; + } + + } + @Configuration static class Customizers { @@ -99,7 +176,7 @@ public class TraceAutoConfigurationCustomizersTests { boolean contextCustomizerApplied; - boolean extraFieldCustomizerApplied; + boolean baggagePropagationCustomizerApplied; boolean httpCustomizerApplied; @@ -118,8 +195,8 @@ public class TraceAutoConfigurationCustomizersTests { } @Bean - ExtraFieldCustomizer sleuthExtraFieldCustomizer() { - return builder -> extraFieldCustomizerApplied = true; + BaggagePropagationCustomizer sleuthBaggagePropagationCustomizer() { + return builder -> baggagePropagationCustomizerApplied = true; } @Bean