Merge branch '3.0.x' into 3.1.x

This commit is contained in:
Phillip Webb 2023-06-16 13:54:01 -07:00
commit baddf4c857
13 changed files with 351 additions and 335 deletions

View File

@ -187,8 +187,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);
}
}
@ -207,10 +206,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;
}

View File

@ -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> {
class CompositePropagationFactory extends Propagation.Factory {
private final Collection<Propagation.Factory> injectorFactories;
private final PropagationFactories injectors;
private final Collection<Propagation.Factory> extractorFactories;
private final PropagationFactories extractors;
private final List<Propagation<String>> injectors;
private final List<Propagation<String>> extractors;
private final boolean supportsJoin;
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();
this.injectors = new PropagationFactories(injectorFactories);
this.extractors = new PropagationFactories(extractorFactories);
this.propagation = new CompositePropagation(this.injectors, this.extractors);
}
Collection<Factory> getInjectorFactories() {
return this.injectorFactories;
}
@Override
public List<String> keys() {
return this.keys;
}
@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);
}
};
}
@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;
};
}
@Override
@SuppressWarnings("deprecation")
public <K> Propagation<K> create(KeyFactory<K> keyFactory) {
return StringPropagationAdapter.create(this, keyFactory);
Stream<Factory> getInjectors() {
return this.injectors.stream();
}
@Override
public boolean supportsJoin() {
return this.supportsJoin;
return this.injectors.supportsJoin() && this.extractors.supportsJoin();
}
@Override
public boolean requires128BitTraceId() {
return this.requires128BitTraceId;
return this.injectors.requires128BitTraceId() || this.extractors.requires128BitTraceId();
}
@Override
@SuppressWarnings("deprecation")
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 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();
}
private static Factory factoryForType(BaggageManager baggageManager,
TracingProperties.Propagation.PropagationType type) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c(baggageManager);
};
}
/**
* Creates a new B3 propagation factory using a single B3 header.
* @return the B3 propagation factory
* A collection of propagation factories.
*/
private static Factory b3Single() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).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 B3 propagation factory using multiple B3 headers.
* @return the B3 propagation factory
* A composite {@link Propagation}.
*/
private static Factory b3Multi() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
}
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);
}
/**
* Creates a new W3C propagation factory.
* @param baggageManager baggage manager to use, or {@code null}
* @return the W3C propagation factory
*/
private static W3CPropagation w3c(BaggageManager baggageManager) {
return (baggageManager != null) ? new W3CPropagation(baggageManager, Collections.emptyList())
: new W3CPropagation();
}
}

View File

@ -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;
}
}
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);
}
/**
* Creates a new {@link CompositeTextMapPropagator}, which uses the given
* {@code injectionTypes} for injection and {@code extractionTypes} for extraction.
* @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))
.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());
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);
}
return result;
}
/**
* Creates a new B3 propagator using a single B3 header.
* @return the B3 propagator
* 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}
* @return the {@link CompositeTextMapPropagator}
*/
private static TextMapPropagator b3Single() {
return B3Propagator.injectingSingleHeader();
}
/**
* Creates a new B3 propagator using multiple B3 headers.
* @return the B3 propagator
*/
private static TextMapPropagator b3Multi() {
return B3Propagator.injectingMultiHeaders();
}
/**
* 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();
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);
}
return TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance());
List<TextMapPropagator> extractors = properties.getEffectiveProducedTypes().stream().map(mapper::map).toList();
return new CompositeTextMapPropagator(injectors, extractors, baggagePropagator);
}
private static TextMapPropagator forType(TracingProperties.Propagation.PropagationType type, boolean baggage) {
return switch (type) {
case B3 -> b3Single();
case B3_MULTI -> b3Multi();
case W3C -> w3c(baggage);
};
/**
* Mapper used to create a {@link TextMapPropagator} from a {@link PropagationType}.
*/
private static class TextMapPropagatorMapper {
private final boolean baggage;
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());
}
}
}

View File

@ -195,9 +195,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
@ -216,8 +214,7 @@ public class OpenTelemetryAutoConfiguration {
@Bean
TextMapPropagator textMapPropagator(TracingProperties properties) {
return CompositeTextMapPropagator.create(properties.getPropagation().getEffectiveProducedTypes(),
properties.getPropagation().getEffectiveConsumedTypes());
return CompositeTextMapPropagator.create(properties.getPropagation(), null);
}
}

View File

@ -222,10 +222,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;
}
/**
@ -234,10 +231,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;
}
/**

View File

@ -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;
@ -134,8 +135,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);
});
}
@ -168,8 +169,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);
});
}
@ -321,7 +322,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");

View File

@ -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> {

View File

@ -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());

View File

@ -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;
@ -177,9 +178,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);
});
}
@ -189,8 +189,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);
});
}
@ -211,9 +211,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);
});
}

View File

@ -2,19 +2,15 @@
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-5.0.xsd">
<instance-name>spring-boot</instance-name>
<connection-strategy>
<connection-retry>
<cluster-connect-timeout-millis>60000</cluster-connect-timeout-millis>
</connection-retry>
</connection-strategy>
<network>
<cluster-members>
<address>${address}</address>
</cluster-members>
</network>
</hazelcast-client>

View File

@ -135,7 +135,6 @@ class ConfigurationPropertiesBinder {
: new IgnoreTopLevelConverterNotFoundBindHandler();
}
@SuppressWarnings("unchecked")
private List<Validator> getValidators(Bindable<?> target) {
List<Validator> validators = new ArrayList<>(3);
if (this.configurationPropertiesValidator != null) {
@ -144,17 +143,25 @@ class ConfigurationPropertiesBinder {
if (this.jsr303Present && target.getAnnotation(Validated.class) != null) {
validators.add(getJsr303Validator());
}
if (target.getValue() != null) {
if (target.getValue().get() instanceof Validator validator) {
validators.add(validator);
}
}
else if (Validator.class.isAssignableFrom(target.getType().resolve())) {
validators.add(new SelfValidatingConstructorBoundBindableValidator((Bindable<? extends Validator>) target));
Validator selfValidator = getSelfValidator(target);
if (selfValidator != null) {
validators.add(selfValidator);
}
return validators;
}
private Validator getSelfValidator(Bindable<?> target) {
if (target.getValue() != null) {
Object value = target.getValue().get();
return (value instanceof Validator validator) ? validator : null;
}
Class<?> type = target.getType().resolve();
if (Validator.class.isAssignableFrom(type)) {
return new SelfValidatingConstructorBoundBindableValidator(type);
}
return null;
}
private Validator getJsr303Validator() {
if (this.jsr303Validator == null) {
this.jsr303Validator = new ConfigurationPropertiesJsr303Validator(this.applicationContext);
@ -263,15 +270,15 @@ class ConfigurationPropertiesBinder {
*/
static class SelfValidatingConstructorBoundBindableValidator implements Validator {
private final Bindable<? extends Validator> bindable;
private final Class<?> type;
SelfValidatingConstructorBoundBindableValidator(Bindable<? extends Validator> bindable) {
this.bindable = bindable;
SelfValidatingConstructorBoundBindableValidator(Class<?> type) {
this.type = type;
}
@Override
public boolean supports(Class<?> clazz) {
return clazz.isAssignableFrom(this.bindable.getType().resolve());
public boolean supports(Class<?> candidate) {
return candidate.isAssignableFrom(this.type);
}
@Override

View File

@ -392,11 +392,9 @@ class JavaBeanBinder implements DataObjectBinder {
}
private boolean isUninitializedKotlinProperty(Exception ex) {
if (ex instanceof InvocationTargetException ite) {
return "kotlin.UninitializedPropertyAccessException"
.equals(ite.getTargetException().getClass().getName());
}
return false;
return (ex instanceof InvocationTargetException invocationTargetException)
&& "kotlin.UninitializedPropertyAccessException"
.equals(invocationTargetException.getTargetException().getClass().getName());
}
boolean isSettable() {

View File

@ -16,6 +16,7 @@
package org.springframework.boot.image.assertions;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -106,8 +107,14 @@ public class ContainerConfigAssert extends AbstractAssert<ContainerConfigAssert,
return this.actual.extractingJsonPathArrayValue("$.processes[?(@.type=='%s')]", type)
.singleElement()
.extracting("command", "args")
.flatMap((list) -> (list != null) ? ((List<?>) list).stream().map(Objects::toString).toList()
: Collections.emptyList());
.flatMap(this::getArgs);
}
private Collection<String> getArgs(Object obj) {
if (obj instanceof List<?> list) {
return list.stream().map(Objects::toString).toList();
}
return Collections.emptyList();
}
}