Polish 'Add support for multiple tracing propagation formats'

See gh-35611
pull/37018/head
Phillip Webb 1 year ago
parent 691b549e36
commit 9b72cc41ab

@ -167,8 +167,7 @@ public class BraveAutoConfiguration {
@Bean
@ConditionalOnMissingBean
Factory propagationFactory(TracingProperties properties) {
return CompositePropagationFactory.create(properties.getPropagation().getEffectiveProducedTypes(),
properties.getPropagation().getEffectiveConsumedTypes());
return CompositePropagationFactory.create(properties.getPropagation(), null);
}
}
@ -187,10 +186,9 @@ public class BraveAutoConfiguration {
@ConditionalOnMissingBean
BaggagePropagation.FactoryBuilder propagationFactoryBuilder(
ObjectProvider<BaggagePropagationCustomizer> baggagePropagationCustomizers) {
Factory delegate = CompositePropagationFactory.create(BRAVE_BAGGAGE_MANAGER,
this.tracingProperties.getPropagation().getEffectiveProducedTypes(),
this.tracingProperties.getPropagation().getEffectiveConsumedTypes());
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(delegate);
CompositePropagationFactory factory = CompositePropagationFactory
.create(this.tracingProperties.getPropagation(), BRAVE_BAGGAGE_MANAGER);
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(factory);
baggagePropagationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder;
}

@ -19,183 +19,206 @@ package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
import brave.internal.propagation.StringPropagationAdapter;
import brave.propagation.B3Propagation;
import brave.propagation.Propagation;
import brave.propagation.Propagation.Factory;
import brave.propagation.TraceContext;
import brave.propagation.TraceContextOrSamplingFlags;
import graphql.com.google.common.collect.Streams;
import io.micrometer.tracing.BaggageManager;
import io.micrometer.tracing.brave.bridge.W3CPropagation;
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation.PropagationType;
/**
* {@link Factory} which supports multiple tracing formats. It is able to configure
* different formats for injecting and for extracting.
* {@link brave.propagation.Propagation.Factory Propagation factory} which supports
* multiple tracing formats. It is able to configure different formats for injecting and
* for extracting.
*
* @author Marcin Grzejszczak
* @author Moritz Halbritter
* @author Phillip Webb
*/
class CompositePropagationFactory extends Propagation.Factory implements Propagation<String> {
private final Collection<Propagation.Factory> injectorFactories;
private final Collection<Propagation.Factory> extractorFactories;
private final List<Propagation<String>> injectors;
class CompositePropagationFactory extends Propagation.Factory {
private final List<Propagation<String>> extractors;
private final PropagationFactories injectors;
private final boolean supportsJoin;
private final PropagationFactories extractors;
private final boolean requires128BitTraceId;
private final List<String> keys;
private final CompositePropagation propagation;
CompositePropagationFactory(Collection<Factory> injectorFactories, Collection<Factory> extractorFactories) {
this.injectorFactories = injectorFactories;
this.extractorFactories = extractorFactories;
this.injectors = this.injectorFactories.stream().map(Factory::get).toList();
this.extractors = this.extractorFactories.stream().map(Factory::get).toList();
this.supportsJoin = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream())
.allMatch(Factory::supportsJoin);
this.requires128BitTraceId = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream())
.anyMatch(Factory::requires128BitTraceId);
this.keys = Stream.concat(this.injectors.stream(), this.extractors.stream())
.flatMap((entry) -> entry.keys().stream())
.distinct()
.toList();
}
Collection<Factory> getInjectorFactories() {
return this.injectorFactories;
this.injectors = new PropagationFactories(injectorFactories);
this.extractors = new PropagationFactories(extractorFactories);
this.propagation = new CompositePropagation(this.injectors, this.extractors);
}
@Override
public List<String> keys() {
return this.keys;
Stream<Factory> getInjectors() {
return this.injectors.stream();
}
@Override
public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) {
return (traceContext, request) -> {
for (Propagation<String> injector : this.injectors) {
injector.injector(setter).inject(traceContext, request);
}
};
public boolean supportsJoin() {
return this.injectors.supportsJoin() && this.extractors.supportsJoin();
}
@Override
public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
return (request) -> {
for (Propagation<String> extractor : this.extractors) {
TraceContextOrSamplingFlags extract = extractor.extractor(getter).extract(request);
if (extract != TraceContextOrSamplingFlags.EMPTY) {
return extract;
}
}
return TraceContextOrSamplingFlags.EMPTY;
};
public boolean requires128BitTraceId() {
return this.injectors.requires128BitTraceId() || this.extractors.requires128BitTraceId();
}
@Override
@SuppressWarnings("deprecation")
public <K> Propagation<K> create(KeyFactory<K> keyFactory) {
return StringPropagationAdapter.create(this, keyFactory);
}
@Override
public boolean supportsJoin() {
return this.supportsJoin;
}
@Override
public boolean requires128BitTraceId() {
return this.requires128BitTraceId;
public <K> Propagation<K> create(Propagation.KeyFactory<K> keyFactory) {
return StringPropagationAdapter.create(this.propagation, keyFactory);
}
@Override
public TraceContext decorate(TraceContext context) {
for (Factory injectorFactory : this.injectorFactories) {
TraceContext decorated = injectorFactory.decorate(context);
if (decorated != context) {
return decorated;
}
}
for (Factory extractorFactory : this.extractorFactories) {
TraceContext decorated = extractorFactory.decorate(context);
if (decorated != context) {
return decorated;
}
}
return super.decorate(context);
return Streams.concat(this.injectors.stream(), this.extractors.stream())
.map((factory) -> factory.decorate(context))
.filter((decorated) -> decorated != context)
.findFirst()
.orElse(context);
}
/**
* Creates a new {@link CompositePropagationFactory}, which uses the given
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @param properties the propagation properties
* @param baggageManager the baggage manager to use, or {@code null}
* @param injectionTypes the propagation types for injection
* @param extractionTypes the propagation types for extraction
* @return the {@link CompositePropagationFactory}
*/
static CompositePropagationFactory create(BaggageManager baggageManager,
Collection<TracingProperties.Propagation.PropagationType> injectionTypes,
Collection<TracingProperties.Propagation.PropagationType> extractionTypes) {
List<Factory> injectors = injectionTypes.stream()
.map((injection) -> factoryForType(baggageManager, injection))
.toList();
List<Factory> extractors = extractionTypes.stream()
.map((extraction) -> factoryForType(baggageManager, extraction))
.toList();
static CompositePropagationFactory create(TracingProperties.Propagation properties, BaggageManager baggageManager) {
PropagationFactoryMapper mapper = new PropagationFactoryMapper(baggageManager);
List<Factory> injectors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList();
List<Factory> extractors = properties.getEffectiveConsumedTypes().stream().map(mapper::map).toList();
return new CompositePropagationFactory(injectors, extractors);
}
/**
* Creates a new {@link CompositePropagationFactory}, which uses the given
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @param injectionTypes the propagation types for injection
* @param extractionTypes the propagation types for extraction
* @return the {@link CompositePropagationFactory}
* Mapper used to create a {@link brave.propagation.Propagation.Factory Propagation
* factory} from a {@link PropagationType}.
*/
static CompositePropagationFactory create(Collection<TracingProperties.Propagation.PropagationType> injectionTypes,
Collection<TracingProperties.Propagation.PropagationType> extractionTypes) {
return create(null, injectionTypes, extractionTypes);
}
private static class PropagationFactoryMapper {
private static Factory factoryForType(BaggageManager baggageManager,
TracingProperties.Propagation.PropagationType type) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c(baggageManager);
};
}
private final BaggageManager baggageManager;
PropagationFactoryMapper(BaggageManager baggageManager) {
this.baggageManager = baggageManager;
}
Propagation.Factory map(PropagationType type) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c();
};
}
/**
* Creates a new B3 propagation factory using a single B3 header.
* @return the B3 propagation factory
*/
private Propagation.Factory b3Single() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
}
/**
* Creates a new B3 propagation factory using multiple B3 headers.
* @return the B3 propagation factory
*/
private Propagation.Factory b3Multi() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
}
/**
* Creates a new W3C propagation factory.
* @return the W3C propagation factory
*/
private Propagation.Factory w3c() {
return (this.baggageManager != null) ? new W3CPropagation(this.baggageManager, Collections.emptyList())
: new W3CPropagation();
}
/**
* Creates a new B3 propagation factory using a single B3 header.
* @return the B3 propagation factory
*/
private static Factory b3Single() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
}
/**
* Creates a new B3 propagation factory using multiple B3 headers.
* @return the B3 propagation factory
* A collection of propagation factories.
*/
private static Factory b3Multi() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
private static class PropagationFactories {
private final List<Propagation.Factory> factories;
PropagationFactories(Collection<Factory> factories) {
this.factories = List.copyOf(factories);
}
boolean requires128BitTraceId() {
return stream().anyMatch(Propagation.Factory::requires128BitTraceId);
}
boolean supportsJoin() {
return stream().allMatch(Propagation.Factory::supportsJoin);
}
List<Propagation<String>> get() {
return stream().map(Factory::get).toList();
}
Stream<Factory> stream() {
return this.factories.stream();
}
}
/**
* Creates a new W3C propagation factory.
* @param baggageManager baggage manager to use, or {@code null}
* @return the W3C propagation factory
* A composite {@link Propagation}.
*/
private static W3CPropagation w3c(BaggageManager baggageManager) {
return (baggageManager != null) ? new W3CPropagation(baggageManager, Collections.emptyList())
: new W3CPropagation();
private static class CompositePropagation implements Propagation<String> {
private final List<Propagation<String>> injectors;
private final List<Propagation<String>> extractors;
private final List<String> keys;
CompositePropagation(PropagationFactories injectorFactories, PropagationFactories extractorFactories) {
this.injectors = injectorFactories.get();
this.extractors = extractorFactories.get();
this.keys = Stream.concat(keys(this.injectors), keys(this.extractors)).distinct().toList();
}
private Stream<String> keys(List<Propagation<String>> propagations) {
return propagations.stream().flatMap((propagation) -> propagation.keys().stream());
}
@Override
public List<String> keys() {
return this.keys;
}
@Override
public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) {
return (traceContext, request) -> this.injectors.stream()
.map((propagation) -> propagation.injector(setter))
.forEach((injector) -> injector.inject(traceContext, request));
}
@Override
public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
return (request) -> this.extractors.stream()
.map((propagation) -> propagation.extractor(getter))
.map((extractor) -> extractor.extract(request))
.filter(Predicate.not(TraceContextOrSamplingFlags.EMPTY::equals))
.findFirst()
.orElse(TraceContextOrSamplingFlags.EMPTY);
}
}
}

@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -32,6 +33,8 @@ import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation.PropagationType;
/**
* {@link TextMapPropagator} which supports multiple tracing formats. It is able to
* configure different formats for injecting and for extracting.
@ -42,9 +45,9 @@ class CompositeTextMapPropagator implements TextMapPropagator {
private final Collection<TextMapPropagator> injectors;
private final Collection<TextMapPropagator> mutuallyExclusiveExtractors;
private final Collection<TextMapPropagator> extractors;
private final Collection<TextMapPropagator> alwaysRunningExtractors;
private final TextMapPropagator baggagePropagator;
private final Set<String> fields;
@ -54,19 +57,24 @@ class CompositeTextMapPropagator implements TextMapPropagator {
* @param mutuallyExclusiveExtractors the mutually exclusive extractors. They are
* applied in order, and as soon as an extractor extracts a context, the other
* extractors after it are no longer invoked
* @param alwaysRunningExtractors the always running extractors. They always run in
* order, regardless of the mutually exclusive extractors or whether the extractor
* before it has already extracted a context
* @param baggagePropagator the baggage propagator to use, or {@code null}
*/
CompositeTextMapPropagator(Collection<TextMapPropagator> injectors,
Collection<TextMapPropagator> mutuallyExclusiveExtractors,
Collection<TextMapPropagator> alwaysRunningExtractors) {
Collection<TextMapPropagator> mutuallyExclusiveExtractors, TextMapPropagator baggagePropagator) {
this.injectors = injectors;
this.mutuallyExclusiveExtractors = mutuallyExclusiveExtractors;
this.alwaysRunningExtractors = alwaysRunningExtractors;
this.fields = concat(this.injectors, this.mutuallyExclusiveExtractors, this.alwaysRunningExtractors)
.flatMap((entry) -> entry.fields().stream())
.collect(Collectors.toSet());
this.extractors = mutuallyExclusiveExtractors;
this.baggagePropagator = baggagePropagator;
Set<String> fields = new LinkedHashSet<>();
fields(this.injectors).forEach(fields::add);
fields(this.extractors).forEach(fields::add);
if (baggagePropagator != null) {
fields.addAll(baggagePropagator.fields());
}
this.fields = Collections.unmodifiableSet(fields);
}
private Stream<String> fields(Collection<TextMapPropagator> propagators) {
return propagators.stream().flatMap((propagator) -> propagator.fields().stream());
}
Collection<TextMapPropagator> getInjectors() {
@ -80,11 +88,8 @@ class CompositeTextMapPropagator implements TextMapPropagator {
@Override
public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
if (context == null || setter == null) {
return;
}
for (TextMapPropagator injector : this.injectors) {
injector.inject(context, carrier, setter);
if (context != null && setter != null) {
this.injectors.forEach((injector) -> injector.inject(context, carrier, setter));
}
}
@ -96,99 +101,81 @@ class CompositeTextMapPropagator implements TextMapPropagator {
if (getter == null) {
return context;
}
Context currentContext = context;
for (TextMapPropagator extractor : this.mutuallyExclusiveExtractors) {
Context extractedContext = extractor.extract(currentContext, carrier, getter);
if (extractedContext != currentContext) {
currentContext = extractedContext;
break;
}
Context result = this.extractors.stream()
.map((extractor) -> extractor.extract(context, carrier, getter))
.filter((extracted) -> extracted != context)
.findFirst()
.orElse(context);
if (this.baggagePropagator != null) {
result = this.baggagePropagator.extract(result, carrier, getter);
}
for (TextMapPropagator extractor : this.alwaysRunningExtractors) {
currentContext = extractor.extract(currentContext, carrier, getter);
}
return currentContext;
}
/**
* Creates a new {@link CompositeTextMapPropagator}, which uses the given
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @param injectionTypes the propagation types for injection
* @param extractionTypes the propagation types for extraction
* @return the {@link CompositeTextMapPropagator}
*/
static TextMapPropagator create(Collection<TracingProperties.Propagation.PropagationType> injectionTypes,
Collection<TracingProperties.Propagation.PropagationType> extractionTypes) {
return create(null, injectionTypes, extractionTypes);
return result;
}
/**
* Creates a new {@link CompositeTextMapPropagator}, which uses the given
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @param properties the tracing properties
* @param baggagePropagator the baggage propagator to use, or {@code null}
* @param injectionTypes the propagation types for injection
* @param extractionTypes the propagation types for extraction
* @return the {@link CompositeTextMapPropagator}
*/
static CompositeTextMapPropagator create(TextMapPropagator baggagePropagator,
Collection<TracingProperties.Propagation.PropagationType> injectionTypes,
Collection<TracingProperties.Propagation.PropagationType> extractionTypes) {
List<TextMapPropagator> injectors = injectionTypes.stream()
.map((injection) -> forType(injection, baggagePropagator != null))
static TextMapPropagator create(TracingProperties.Propagation properties, TextMapPropagator baggagePropagator) {
TextMapPropagatorMapper mapper = new TextMapPropagatorMapper(baggagePropagator != null);
List<TextMapPropagator> injectors = properties.getEffectiveProducedTypes()
.stream()
.map(mapper::map)
.collect(Collectors.toCollection(ArrayList::new));
if (baggagePropagator != null) {
injectors.add(baggagePropagator);
}
List<TextMapPropagator> extractors = extractionTypes.stream()
.map((extraction) -> forType(extraction, baggagePropagator != null))
.toList();
return new CompositeTextMapPropagator(injectors, extractors,
(baggagePropagator != null) ? List.of(baggagePropagator) : Collections.emptyList());
}
@SafeVarargs
private static <T> Stream<T> concat(Collection<T>... collections) {
Stream<T> result = Stream.empty();
for (Collection<T> collection : collections) {
result = Stream.concat(result, collection.stream());
}
return result;
List<TextMapPropagator> extractors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList();
return new CompositeTextMapPropagator(injectors, extractors, baggagePropagator);
}
/**
* Creates a new B3 propagator using a single B3 header.
* @return the B3 propagator
* Mapper used to create a {@link TextMapPropagator} from a {@link PropagationType}.
*/
private static TextMapPropagator b3Single() {
return B3Propagator.injectingSingleHeader();
}
private static class TextMapPropagatorMapper {
/**
* Creates a new B3 propagator using multiple B3 headers.
* @return the B3 propagator
*/
private static TextMapPropagator b3Multi() {
return B3Propagator.injectingMultiHeaders();
}
private final boolean baggage;
/**
* Creates a new W3C propagator.
* @param baggage whether baggage propagation should be supported
* @return the W3C propagator
*/
private static TextMapPropagator w3c(boolean baggage) {
if (!baggage) {
return W3CTraceContextPropagator.getInstance();
TextMapPropagatorMapper(boolean baggage) {
this.baggage = baggage;
}
TextMapPropagator map(PropagationType type) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c();
};
}
/**
* Creates a new B3 propagator using a single B3 header.
* @return the B3 propagator
*/
private TextMapPropagator b3Single() {
return B3Propagator.injectingSingleHeader();
}
/**
* Creates a new B3 propagator using multiple B3 headers.
* @return the B3 propagator
*/
private TextMapPropagator b3Multi() {
return B3Propagator.injectingMultiHeaders();
}
/**
* Creates a new W3C propagator.
* @return the W3C propagator
*/
private TextMapPropagator w3c() {
return (!this.baggage) ? W3CTraceContextPropagator.getInstance() : TextMapPropagator
.composite(W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance());
}
return TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance());
}
private static TextMapPropagator forType(TracingProperties.Propagation.PropagationType type, boolean baggage) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c(baggage);
};
}
}

@ -187,9 +187,7 @@ public class OpenTelemetryAutoConfiguration {
List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields();
BaggageTextMapPropagator baggagePropagator = new BaggageTextMapPropagator(remoteFields,
new OtelBaggageManager(otelCurrentTraceContext, remoteFields, Collections.emptyList()));
return CompositeTextMapPropagator.create(baggagePropagator,
this.tracingProperties.getPropagation().getEffectiveProducedTypes(),
this.tracingProperties.getPropagation().getEffectiveConsumedTypes());
return CompositeTextMapPropagator.create(this.tracingProperties.getPropagation(), baggagePropagator);
}
@Bean
@ -208,8 +206,7 @@ public class OpenTelemetryAutoConfiguration {
@Bean
TextMapPropagator textMapPropagator(TracingProperties properties) {
return CompositeTextMapPropagator.create(properties.getPropagation().getEffectiveProducedTypes(),
properties.getPropagation().getEffectiveConsumedTypes());
return CompositeTextMapPropagator.create(properties.getPropagation(), null);
}
}

@ -212,10 +212,7 @@ public class TracingProperties {
* @return the effective context propagation types produced by the application
*/
List<PropagationType> getEffectiveProducedTypes() {
if (this.type != null) {
return this.type;
}
return this.produce;
return (this.type != null) ? this.type : this.produce;
}
/**
@ -224,10 +221,7 @@ public class TracingProperties {
* @return the effective context propagation types consumed by the application
*/
List<PropagationType> getEffectiveConsumedTypes() {
if (this.type != null) {
return this.type;
}
return this.consume;
return (this.type != null) ? this.type : this.consume;
}
/**

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import brave.Span;
import brave.SpanCustomizer;
@ -131,8 +132,8 @@ class BraveAutoConfigurationTests {
this.contextRunner.run((context) -> {
assertThat(context).hasBean("propagationFactory");
Factory factory = context.getBean(Factory.class);
List<Factory> injectors = getInjectors(factory);
assertThat(injectors).extracting(Factory::getClass).containsExactly(W3CPropagation.class);
Stream<Class<?>> injectors = getInjectors(factory).stream().map(Object::getClass);
assertThat(injectors).containsExactly(W3CPropagation.class);
assertThat(context).hasSingleBean(BaggagePropagation.FactoryBuilder.class);
});
}
@ -165,8 +166,8 @@ class BraveAutoConfigurationTests {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> {
assertThat(context).hasBean("propagationFactory");
Factory factory = context.getBean(Factory.class);
List<Factory> injectors = getInjectors(factory);
assertThat(injectors).extracting(Factory::getClass).containsExactly(W3CPropagation.class);
Stream<Class<?>> injectors = getInjectors(factory).stream().map(Object::getClass);
assertThat(injectors).containsExactly(W3CPropagation.class);
assertThat(context).doesNotHaveBean(BaggagePropagation.FactoryBuilder.class);
});
}
@ -270,7 +271,7 @@ class BraveAutoConfigurationTests {
private List<Factory> getInjectors(Factory factory) {
assertThat(factory).as("factory").isNotNull();
if (factory instanceof CompositePropagationFactory compositePropagationFactory) {
return compositePropagationFactory.getInjectorFactories().stream().toList();
return compositePropagationFactory.getInjectors().toList();
}
Assertions.fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass()));
throw new AssertionError("Unreachable");

@ -25,6 +25,7 @@ import brave.internal.propagation.StringPropagationAdapter;
import brave.propagation.Propagation;
import brave.propagation.TraceContext;
import brave.propagation.TraceContextOrSamplingFlags;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@ -39,72 +40,80 @@ import static org.mockito.BDDMockito.given;
*/
class CompositePropagationFactoryTests {
@Test
void returnsAllKeys() {
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(field("a")), List.of(field("b")));
assertThat(factory.keys()).containsExactly("a", "b");
}
@Test
void supportsJoin() {
Propagation.Factory supportsJoin = Mockito.mock(Propagation.Factory.class);
given(supportsJoin.supportsJoin()).willReturn(true);
given(supportsJoin.get()).willReturn(new DummyPropagation("a"));
Propagation.Factory doesNotSupportsJoin = Mockito.mock(Propagation.Factory.class);
given(doesNotSupportsJoin.supportsJoin()).willReturn(false);
given(doesNotSupportsJoin.get()).willReturn(new DummyPropagation("a"));
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(supportsJoin),
List.of(doesNotSupportsJoin));
Propagation.Factory supported = Mockito.mock(Propagation.Factory.class);
given(supported.supportsJoin()).willReturn(true);
given(supported.get()).willReturn(new DummyPropagation("a"));
Propagation.Factory unsupported = Mockito.mock(Propagation.Factory.class);
given(unsupported.supportsJoin()).willReturn(false);
given(unsupported.get()).willReturn(new DummyPropagation("a"));
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(supported), List.of(unsupported));
assertThat(factory.supportsJoin()).isFalse();
}
@Test
void requires128BitTraceId() {
Propagation.Factory requires128BitTraceId = Mockito.mock(Propagation.Factory.class);
given(requires128BitTraceId.requires128BitTraceId()).willReturn(true);
given(requires128BitTraceId.get()).willReturn(new DummyPropagation("a"));
Propagation.Factory doesNotRequire128BitTraceId = Mockito.mock(Propagation.Factory.class);
given(doesNotRequire128BitTraceId.requires128BitTraceId()).willReturn(false);
given(doesNotRequire128BitTraceId.get()).willReturn(new DummyPropagation("a"));
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(requires128BitTraceId),
List.of(doesNotRequire128BitTraceId));
Propagation.Factory required = Mockito.mock(Propagation.Factory.class);
given(required.requires128BitTraceId()).willReturn(true);
given(required.get()).willReturn(new DummyPropagation("a"));
Propagation.Factory notRequired = Mockito.mock(Propagation.Factory.class);
given(notRequired.requires128BitTraceId()).willReturn(false);
given(notRequired.get()).willReturn(new DummyPropagation("a"));
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(required), List.of(notRequired));
assertThat(factory.requires128BitTraceId()).isTrue();
}
@Test
void inject() {
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(field("a"), field("b")),
List.of(field("c")));
TraceContext context = context();
Map<String, String> request = new HashMap<>();
factory.injector(new MapSetter()).inject(context, request);
assertThat(request).containsOnly(entry("a", "a-value"), entry("b", "b-value"));
}
@Nested
static class CompostePropagationTests {
@Test
void extractorStopsAfterSuccessfulExtraction() {
CompositePropagationFactory factory = new CompositePropagationFactory(Collections.emptyList(),
List.of(field("a"), field("b")));
Map<String, String> request = Map.of("a", "a-value", "b", "b-value");
TraceContextOrSamplingFlags context = factory.extractor(new MapGetter()).extract(request);
assertThat(context.context().extra()).containsExactly("a");
}
@Test
void keys() {
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(field("a")),
List.of(field("b")));
Propagation<String> propagation = factory.get();
assertThat(propagation.keys()).containsExactly("a", "b");
}
@Test
void returnsEmptyContextWhenNoExtractorMatches() {
CompositePropagationFactory factory = new CompositePropagationFactory(Collections.emptyList(),
Collections.emptyList());
Map<String, String> request = Collections.emptyMap();
TraceContextOrSamplingFlags context = factory.extractor(new MapGetter()).extract(request);
assertThat(context.context()).isNull();
}
@Test
void inject() {
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(field("a"), field("b")),
List.of(field("c")));
Propagation<String> propagation = factory.get();
TraceContext context = context();
Map<String, String> request = new HashMap<>();
propagation.injector(new MapSetter()).inject(context, request);
assertThat(request).containsOnly(entry("a", "a-value"), entry("b", "b-value"));
}
private static TraceContext context() {
return TraceContext.newBuilder().traceId(1).spanId(2).build();
}
@Test
void extractorWhenDelegateExtractsReturnsExtraction() {
CompositePropagationFactory factory = new CompositePropagationFactory(Collections.emptyList(),
List.of(field("a"), field("b")));
Propagation<String> propagation = factory.get();
Map<String, String> request = Map.of("a", "a-value", "b", "b-value");
TraceContextOrSamplingFlags context = propagation.extractor(new MapGetter()).extract(request);
assertThat(context.context().extra()).containsExactly("a");
}
@Test
void extractorWhenWhenNoExtractorMatchesReturnsEmptyContext() {
CompositePropagationFactory factory = new CompositePropagationFactory(Collections.emptyList(),
Collections.emptyList());
Propagation<String> propagation = factory.get();
Map<String, String> request = Collections.emptyMap();
TraceContextOrSamplingFlags context = propagation.extractor(new MapGetter()).extract(request);
assertThat(context.context()).isNull();
}
private static TraceContext context() {
return TraceContext.newBuilder().traceId(1).spanId(2).build();
}
private static DummyPropagation field(String field) {
return new DummyPropagation(field);
}
private static DummyPropagation field(String field) {
return new DummyPropagation(field);
}
private static final class MapSetter implements Propagation.Setter<Map<String, String>, String> {

@ -51,14 +51,14 @@ class CompositeTextMapPropagatorTests {
@Test
void collectsAllFields() {
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(List.of(field("a")), List.of(field("b")),
List.of(field("c")));
field("c"));
assertThat(propagator.fields()).containsExactly("a", "b", "c");
}
@Test
void injectAllFields() {
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(List.of(field("a"), field("b")),
Collections.emptyList(), Collections.emptyList());
Collections.emptyList(), null);
TextMapSetter<Object> setter = setter();
Object carrier = carrier();
propagator.inject(context(), carrier, setter);
@ -68,9 +68,9 @@ class CompositeTextMapPropagatorTests {
}
@Test
void extractMutuallyExclusive() {
void extractWithoutBaggagePropagator() {
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(Collections.emptyList(),
List.of(field("a"), field("b")), Collections.emptyList());
List.of(field("a"), field("b")), null);
Context context = context();
Map<String, String> carrier = Map.of("a", "a-value", "b", "b-value");
context = propagator.extract(context, carrier, new MapTextMapGetter());
@ -81,9 +81,9 @@ class CompositeTextMapPropagatorTests {
}
@Test
void extractAlwaysRunning() {
void extractWithBaggagePropagator() {
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(Collections.emptyList(),
List.of(field("a"), field("b")), List.of(field("c")));
List.of(field("a"), field("b")), field("c"));
Context context = context();
Map<String, String> carrier = Map.of("a", "a-value", "b", "b-value", "c", "c-value");
context = propagator.extract(context, carrier, new MapTextMapGetter());

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import io.micrometer.tracing.SpanCustomizer;
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext;
@ -174,9 +175,8 @@ class OpenTelemetryAutoConfigurationTests {
void shouldSupplyB3PropagationIfPropagationPropertySet() {
this.contextRunner.withPropertyValues("management.tracing.propagation.type=B3").run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).extracting(TextMapPropagator::getClass)
.containsExactly(B3Propagator.class, BaggageTextMapPropagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass);
assertThat(injectors).containsExactly(B3Propagator.class, BaggageTextMapPropagator.class);
});
}
@ -186,8 +186,8 @@ class OpenTelemetryAutoConfigurationTests {
.withPropertyValues("management.tracing.propagation.type=B3", "management.tracing.baggage.enabled=false")
.run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).extracting(TextMapPropagator::getClass).containsExactly(B3Propagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass);
assertThat(injectors).containsExactly(B3Propagator.class);
});
}
@ -208,9 +208,8 @@ class OpenTelemetryAutoConfigurationTests {
void shouldSupplyW3CPropagationWithoutBaggageWhenDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> {
TextMapPropagator propagator = context.getBean(TextMapPropagator.class);
List<TextMapPropagator> injectors = getInjectors(propagator);
assertThat(injectors).extracting(TextMapPropagator::getClass)
.containsExactly(W3CTraceContextPropagator.class);
Stream<Class<?>> injectors = getInjectors(propagator).stream().map(Object::getClass);
assertThat(injectors).containsExactly(W3CTraceContextPropagator.class);
});
}

Loading…
Cancel
Save