From 1e85bf7ca8d0716b88428d32c27086dfe5796072 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 1 Sep 2023 14:40:01 -0500 Subject: [PATCH] Fix mapping of consumed types to propagators Fixes gh-37161 --- .../tracing/CompositeTextMapPropagator.java | 10 +++++++--- .../CompositeTextMapPropagatorTests.java | 17 +++++++++++++++++ .../OpenTelemetryAutoConfigurationTests.java | 13 ++++++------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagator.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagator.java index 5db4f629a0ea..aef0a11f01a3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagator.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagator.java @@ -40,6 +40,7 @@ * configure different formats for injecting and for extracting. * * @author Moritz Halbritter + * @author Scott Frederick */ class CompositeTextMapPropagator implements TextMapPropagator { @@ -81,6 +82,10 @@ Collection getInjectors() { return this.injectors; } + Collection getExtractors() { + return this.extractors; + } + @Override public Collection fields() { return this.fields; @@ -113,8 +118,7 @@ public Context extract(Context context, C carrier, TextMapGetter getter) } /** - * Creates a new {@link CompositeTextMapPropagator}, which uses the given - * {@code injectionTypes} for injection and {@code extractionTypes} for extraction. + * Creates a new {@link CompositeTextMapPropagator}. * @param properties the tracing properties * @param baggagePropagator the baggage propagator to use, or {@code null} * @return the {@link CompositeTextMapPropagator} @@ -128,7 +132,7 @@ static TextMapPropagator create(TracingProperties.Propagation properties, TextMa if (baggagePropagator != null) { injectors.add(baggagePropagator); } - List extractors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList(); + List extractors = properties.getEffectiveConsumedTypes().stream().map(mapper::map).toList(); return new CompositeTextMapPropagator(injectors, extractors, baggagePropagator); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagatorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagatorTests.java index dc5ccbc6c975..64268433c0af 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagatorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagatorTests.java @@ -22,22 +22,28 @@ import java.util.List; import java.util.Map; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextKey; import io.opentelemetry.context.propagation.TextMapGetter; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.context.propagation.TextMapSetter; +import io.opentelemetry.extension.trace.propagation.B3Propagator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InOrder; import org.mockito.Mockito; +import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation; +import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation.PropagationType; + import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link CompositeTextMapPropagator}. * * @author Moritz Halbritter + * @author Scott Frederick */ class CompositeTextMapPropagatorTests { @@ -91,6 +97,17 @@ void extractWithBaggagePropagator() { assertThat(c).isEqualTo("c-value"); } + @Test + void createMapsInjectorsAndExtractors() { + Propagation properties = new Propagation(); + properties.setProduce(List.of(PropagationType.W3C)); + properties.setConsume(List.of(PropagationType.B3)); + CompositeTextMapPropagator propagator = (CompositeTextMapPropagator) CompositeTextMapPropagator + .create(properties, null); + assertThat(propagator.getInjectors()).hasExactlyElementsOfTypes(W3CTraceContextPropagator.class); + assertThat(propagator.getExtractors()).hasExactlyElementsOfTypes(B3Propagator.class); + } + private DummyTextMapPropagator field(String field) { return new DummyTextMapPropagator(field, this.contextKeyRegistry); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfigurationTests.java index 8f611a2c9cb7..be5c2fcda8a4 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfigurationTests.java @@ -24,7 +24,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.stream.Stream; import io.micrometer.tracing.SpanCustomizer; import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext; @@ -209,8 +208,8 @@ void shouldNotSupplySlf4JBaggageEventListenerWhenBaggageDisabled() { void shouldSupplyB3PropagationIfPropagationPropertySet() { this.contextRunner.withPropertyValues("management.tracing.propagation.type=B3").run((context) -> { TextMapPropagator propagator = context.getBean(TextMapPropagator.class); - Stream> injectors = getInjectors(propagator).stream().map(Object::getClass); - assertThat(injectors).containsExactly(B3Propagator.class, BaggageTextMapPropagator.class); + List injectors = getInjectors(propagator); + assertThat(injectors).hasExactlyElementsOfTypes(B3Propagator.class, BaggageTextMapPropagator.class); }); } @@ -220,8 +219,8 @@ void shouldSupplyB3PropagationIfPropagationPropertySetAndBaggageDisabled() { .withPropertyValues("management.tracing.propagation.type=B3", "management.tracing.baggage.enabled=false") .run((context) -> { TextMapPropagator propagator = context.getBean(TextMapPropagator.class); - Stream> injectors = getInjectors(propagator).stream().map(Object::getClass); - assertThat(injectors).containsExactly(B3Propagator.class); + List injectors = getInjectors(propagator); + assertThat(injectors).hasExactlyElementsOfTypes(B3Propagator.class); }); } @@ -242,8 +241,8 @@ void shouldSupplyW3CPropagationWithBaggageByDefault() { void shouldSupplyW3CPropagationWithoutBaggageWhenDisabled() { this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> { TextMapPropagator propagator = context.getBean(TextMapPropagator.class); - Stream> injectors = getInjectors(propagator).stream().map(Object::getClass); - assertThat(injectors).containsExactly(W3CTraceContextPropagator.class); + List injectors = getInjectors(propagator); + assertThat(injectors).hasExactlyElementsOfTypes(W3CTraceContextPropagator.class); }); }