Fix mapping of consumed types to propagators

Fixes gh-37161
pull/37462/head
Scott Frederick 1 year ago
parent 3ab1dfb9dc
commit 1e85bf7ca8

@ -40,6 +40,7 @@ import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.
* configure different formats for injecting and for extracting. * configure different formats for injecting and for extracting.
* *
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Scott Frederick
*/ */
class CompositeTextMapPropagator implements TextMapPropagator { class CompositeTextMapPropagator implements TextMapPropagator {
@ -81,6 +82,10 @@ class CompositeTextMapPropagator implements TextMapPropagator {
return this.injectors; return this.injectors;
} }
Collection<TextMapPropagator> getExtractors() {
return this.extractors;
}
@Override @Override
public Collection<String> fields() { public Collection<String> fields() {
return this.fields; return this.fields;
@ -113,8 +118,7 @@ class CompositeTextMapPropagator implements TextMapPropagator {
} }
/** /**
* Creates a new {@link CompositeTextMapPropagator}, which uses the given * Creates a new {@link CompositeTextMapPropagator}.
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @param properties the tracing properties * @param properties the tracing properties
* @param baggagePropagator the baggage propagator to use, or {@code null} * @param baggagePropagator the baggage propagator to use, or {@code null}
* @return the {@link CompositeTextMapPropagator} * @return the {@link CompositeTextMapPropagator}
@ -128,7 +132,7 @@ class CompositeTextMapPropagator implements TextMapPropagator {
if (baggagePropagator != null) { if (baggagePropagator != null) {
injectors.add(baggagePropagator); injectors.add(baggagePropagator);
} }
List<TextMapPropagator> extractors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList(); List<TextMapPropagator> extractors = properties.getEffectiveConsumedTypes().stream().map(mapper::map).toList();
return new CompositeTextMapPropagator(injectors, extractors, baggagePropagator); return new CompositeTextMapPropagator(injectors, extractors, baggagePropagator);
} }

@ -22,22 +22,28 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.Context; import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey; import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.propagation.TextMapGetter; import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter; import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mockito; 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; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link CompositeTextMapPropagator}. * Tests for {@link CompositeTextMapPropagator}.
* *
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Scott Frederick
*/ */
class CompositeTextMapPropagatorTests { class CompositeTextMapPropagatorTests {
@ -91,6 +97,17 @@ class CompositeTextMapPropagatorTests {
assertThat(c).isEqualTo("c-value"); 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) { private DummyTextMapPropagator field(String field) {
return new DummyTextMapPropagator(field, this.contextKeyRegistry); return new DummyTextMapPropagator(field, this.contextKeyRegistry);
} }

@ -24,7 +24,6 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import io.micrometer.tracing.SpanCustomizer; import io.micrometer.tracing.SpanCustomizer;
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext; import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext;
@ -209,8 +208,8 @@ class OpenTelemetryAutoConfigurationTests {
void shouldSupplyB3PropagationIfPropagationPropertySet() { void shouldSupplyB3PropagationIfPropagationPropertySet() {
this.contextRunner.withPropertyValues("management.tracing.propagation.type=B3").run((context) -> { this.contextRunner.withPropertyValues("management.tracing.propagation.type=B3").run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class); TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass); List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).containsExactly(B3Propagator.class, BaggageTextMapPropagator.class); assertThat(injectors).hasExactlyElementsOfTypes(B3Propagator.class, BaggageTextMapPropagator.class);
}); });
} }
@ -220,8 +219,8 @@ class OpenTelemetryAutoConfigurationTests {
.withPropertyValues("management.tracing.propagation.type=B3", "management.tracing.baggage.enabled=false") .withPropertyValues("management.tracing.propagation.type=B3", "management.tracing.baggage.enabled=false")
.run((context) -> { .run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class); TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass); List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).containsExactly(B3Propagator.class); assertThat(injectors).hasExactlyElementsOfTypes(B3Propagator.class);
}); });
} }
@ -242,8 +241,8 @@ class OpenTelemetryAutoConfigurationTests {
void shouldSupplyW3CPropagationWithoutBaggageWhenDisabled() { void shouldSupplyW3CPropagationWithoutBaggageWhenDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> { this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class); TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass); List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).containsExactly(W3CTraceContextPropagator.class); assertThat(injectors).hasExactlyElementsOfTypes(W3CTraceContextPropagator.class);
}); });
} }

Loading…
Cancel
Save