Skip to content

Commit a0e5e2a

Browse files
committed
Polish
1 parent 532227d commit a0e5e2a

File tree

6 files changed

+49
-60
lines changed

6 files changed

+49
-60
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositePropagationFactory.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package org.springframework.boot.actuate.autoconfigure.tracing;
1818

1919
import java.util.Arrays;
20+
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.stream.Stream;
23-
import java.util.stream.StreamSupport;
2424

2525
import brave.internal.propagation.StringPropagationAdapter;
2626
import brave.propagation.B3Propagation;
@@ -39,9 +39,9 @@
3939
*/
4040
class CompositePropagationFactory extends Propagation.Factory implements Propagation<String> {
4141

42-
private final Iterable<Propagation.Factory> injectorFactories;
42+
private final Collection<Propagation.Factory> injectorFactories;
4343

44-
private final Iterable<Propagation.Factory> extractorFactories;
44+
private final Collection<Propagation.Factory> extractorFactories;
4545

4646
private final List<Propagation<String>> injectors;
4747

@@ -51,27 +51,30 @@ class CompositePropagationFactory extends Propagation.Factory implements Propaga
5151

5252
private final boolean requires128BitTraceId;
5353

54-
CompositePropagationFactory(Iterable<Factory> injectorFactories, Iterable<Factory> extractorFactories) {
54+
private final List<String> keys;
55+
56+
CompositePropagationFactory(Collection<Factory> injectorFactories, Collection<Factory> extractorFactories) {
5557
this.injectorFactories = injectorFactories;
5658
this.extractorFactories = extractorFactories;
57-
this.injectors = stream(this.injectorFactories).map(Factory::get).toList();
58-
this.extractors = stream(extractorFactories).map(Factory::get).toList();
59-
this.supportsJoin = Stream.concat(stream(this.injectorFactories), stream(this.extractorFactories))
59+
this.injectors = this.injectorFactories.stream().map(Factory::get).toList();
60+
this.extractors = this.extractorFactories.stream().map(Factory::get).toList();
61+
this.supportsJoin = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream())
6062
.allMatch(Factory::supportsJoin);
61-
this.requires128BitTraceId = Stream.concat(stream(this.injectorFactories), stream(this.extractorFactories))
63+
this.requires128BitTraceId = Stream.concat(this.injectorFactories.stream(), this.extractorFactories.stream())
6264
.anyMatch(Factory::requires128BitTraceId);
65+
this.keys = Stream.concat(this.injectors.stream(), this.extractors.stream())
66+
.flatMap((entry) -> entry.keys().stream())
67+
.distinct()
68+
.toList();
6369
}
6470

65-
Iterable<Factory> getInjectorFactories() {
71+
Collection<Factory> getInjectorFactories() {
6672
return this.injectorFactories;
6773
}
6874

6975
@Override
7076
public List<String> keys() {
71-
return Stream.concat(this.injectors.stream(), this.extractors.stream())
72-
.flatMap((entry) -> entry.keys().stream())
73-
.distinct()
74-
.toList();
77+
return this.keys;
7578
}
7679

7780
@Override
@@ -137,8 +140,9 @@ public TraceContext decorate(TraceContext context) {
137140
* @return the {@link CompositePropagationFactory}
138141
*/
139142
static CompositePropagationFactory create(BaggageManager baggageManager,
140-
Iterable<TracingProperties.Propagation.PropagationType> injectionTypes) {
141-
List<Factory> injectors = stream(injectionTypes).map((injection) -> factoryForType(baggageManager, injection))
143+
Collection<TracingProperties.Propagation.PropagationType> injectionTypes) {
144+
List<Factory> injectors = injectionTypes.stream()
145+
.map((injection) -> factoryForType(baggageManager, injection))
142146
.toList();
143147
List<Factory> extractors = Arrays.stream(TracingProperties.Propagation.PropagationType.values())
144148
.map((extraction) -> factoryForType(baggageManager, extraction))
@@ -152,14 +156,11 @@ static CompositePropagationFactory create(BaggageManager baggageManager,
152156
* @param injectionTypes the propagation types for injection
153157
* @return the {@link CompositePropagationFactory}
154158
*/
155-
static CompositePropagationFactory create(Iterable<TracingProperties.Propagation.PropagationType> injectionTypes) {
159+
static CompositePropagationFactory create(
160+
Collection<TracingProperties.Propagation.PropagationType> injectionTypes) {
156161
return create(null, injectionTypes);
157162
}
158163

159-
private static <T> Stream<T> stream(Iterable<T> iterable) {
160-
return StreamSupport.stream(iterable.spliterator(), false);
161-
}
162-
163164
private static Factory factoryForType(BaggageManager baggageManager,
164165
TracingProperties.Propagation.PropagationType type) {
165166
return switch (type) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagator.java

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Set;
2425
import java.util.stream.Collectors;
2526
import java.util.stream.Stream;
26-
import java.util.stream.StreamSupport;
2727

2828
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
2929
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
@@ -41,11 +41,13 @@
4141
*/
4242
class CompositeTextMapPropagator implements TextMapPropagator {
4343

44-
private final Iterable<TextMapPropagator> injectors;
44+
private final Collection<TextMapPropagator> injectors;
4545

46-
private final Iterable<TextMapPropagator> mutuallyExclusiveExtractors;
46+
private final Collection<TextMapPropagator> mutuallyExclusiveExtractors;
4747

48-
private final Iterable<TextMapPropagator> alwaysRunningExtractors;
48+
private final Collection<TextMapPropagator> alwaysRunningExtractors;
49+
50+
private final Set<String> fields;
4951

5052
/**
5153
* Creates a new {@link CompositeTextMapPropagator}.
@@ -57,35 +59,24 @@ class CompositeTextMapPropagator implements TextMapPropagator {
5759
* order, regardless of the mutually exclusive extractors or whether the extractor
5860
* before it has already extracted a context
5961
*/
60-
CompositeTextMapPropagator(Iterable<TextMapPropagator> injectors,
61-
Iterable<TextMapPropagator> mutuallyExclusiveExtractors,
62-
Iterable<TextMapPropagator> alwaysRunningExtractors) {
62+
CompositeTextMapPropagator(Collection<TextMapPropagator> injectors,
63+
Collection<TextMapPropagator> mutuallyExclusiveExtractors,
64+
Collection<TextMapPropagator> alwaysRunningExtractors) {
6365
this.injectors = injectors;
6466
this.mutuallyExclusiveExtractors = mutuallyExclusiveExtractors;
6567
this.alwaysRunningExtractors = alwaysRunningExtractors;
68+
this.fields = concat(this.injectors, this.mutuallyExclusiveExtractors, this.alwaysRunningExtractors)
69+
.flatMap((entry) -> entry.fields().stream())
70+
.collect(Collectors.toSet());
6671
}
6772

68-
/**
69-
* Creates a new {@link CompositeTextMapPropagator}.
70-
* @param injectors the injectors
71-
* @param mutuallyExclusiveExtractors the mutually exclusive extractors. They are
72-
* applied in order, and as soon as an extractor extracts a context, the other
73-
* extractors after it are no longer invoked
74-
*/
75-
CompositeTextMapPropagator(Iterable<TextMapPropagator> injectors,
76-
Iterable<TextMapPropagator> mutuallyExclusiveExtractors) {
77-
this(injectors, mutuallyExclusiveExtractors, Collections.emptyList());
78-
}
79-
80-
Iterable<TextMapPropagator> getInjectors() {
73+
Collection<TextMapPropagator> getInjectors() {
8174
return this.injectors;
8275
}
8376

8477
@Override
8578
public Collection<String> fields() {
86-
return concat(this.injectors, this.mutuallyExclusiveExtractors, this.alwaysRunningExtractors)
87-
.flatMap((entry) -> entry.fields().stream())
88-
.collect(Collectors.toSet());
79+
return this.fields;
8980
}
9081

9182
@Override
@@ -126,7 +117,7 @@ public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter)
126117
* @param injectionTypes the propagation types for injection
127118
* @return the {@link CompositeTextMapPropagator}
128119
*/
129-
static TextMapPropagator create(List<TracingProperties.Propagation.PropagationType> injectionTypes) {
120+
static TextMapPropagator create(Collection<TracingProperties.Propagation.PropagationType> injectionTypes) {
130121
return create(null, injectionTypes);
131122
}
132123

@@ -138,8 +129,8 @@ static TextMapPropagator create(List<TracingProperties.Propagation.PropagationTy
138129
* @return the {@link CompositeTextMapPropagator}
139130
*/
140131
static CompositeTextMapPropagator create(TextMapPropagator baggagePropagator,
141-
Iterable<TracingProperties.Propagation.PropagationType> injectionTypes) {
142-
List<TextMapPropagator> injectors = stream(injectionTypes)
132+
Collection<TracingProperties.Propagation.PropagationType> injectionTypes) {
133+
List<TextMapPropagator> injectors = injectionTypes.stream()
143134
.map((injection) -> forType(injection, baggagePropagator != null))
144135
.collect(Collectors.toCollection(ArrayList::new));
145136
if (baggagePropagator != null) {
@@ -153,18 +144,14 @@ static CompositeTextMapPropagator create(TextMapPropagator baggagePropagator,
153144
}
154145

155146
@SafeVarargs
156-
private static <T> Stream<T> concat(Iterable<T>... iterables) {
147+
private static <T> Stream<T> concat(Collection<T>... collections) {
157148
Stream<T> result = Stream.empty();
158-
for (Iterable<T> iterable : iterables) {
159-
result = Stream.concat(result, stream(iterable));
149+
for (Collection<T> collection : collections) {
150+
result = Stream.concat(result, collection.stream());
160151
}
161152
return result;
162153
}
163154

164-
private static <T> Stream<T> stream(Iterable<T> iterable) {
165-
return StreamSupport.stream(iterable.spliterator(), false);
166-
}
167-
168155
/**
169156
* Creates a new B3 propagator using a single B3 header.
170157
* @return the B3 propagator

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Collections;
2020
import java.util.List;
21-
import java.util.stream.StreamSupport;
2221

2322
import brave.Span;
2423
import brave.SpanCustomizer;
@@ -271,8 +270,7 @@ void compositeSpanHandlerUsesFilterPredicateAndReportersInOrder() {
271270
private List<Factory> getInjectors(Factory factory) {
272271
assertThat(factory).as("factory").isNotNull();
273272
if (factory instanceof CompositePropagationFactory compositePropagationFactory) {
274-
return StreamSupport.stream(compositePropagationFactory.getInjectorFactories().spliterator(), false)
275-
.toList();
273+
return compositePropagationFactory.getInjectorFactories().stream().toList();
276274
}
277275
Assertions.fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass()));
278276
throw new AssertionError("Unreachable");

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositePropagationFactoryTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ void returnsAllKeys() {
4949
void supportsJoin() {
5050
Propagation.Factory supportsJoin = Mockito.mock(Propagation.Factory.class);
5151
given(supportsJoin.supportsJoin()).willReturn(true);
52+
given(supportsJoin.get()).willReturn(new DummyPropagation("a"));
5253
Propagation.Factory doesNotSupportsJoin = Mockito.mock(Propagation.Factory.class);
5354
given(doesNotSupportsJoin.supportsJoin()).willReturn(false);
55+
given(doesNotSupportsJoin.get()).willReturn(new DummyPropagation("a"));
5456
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(supportsJoin),
5557
List.of(doesNotSupportsJoin));
5658
assertThat(factory.supportsJoin()).isFalse();
@@ -60,8 +62,10 @@ void supportsJoin() {
6062
void requires128BitTraceId() {
6163
Propagation.Factory requires128BitTraceId = Mockito.mock(Propagation.Factory.class);
6264
given(requires128BitTraceId.requires128BitTraceId()).willReturn(true);
65+
given(requires128BitTraceId.get()).willReturn(new DummyPropagation("a"));
6366
Propagation.Factory doesNotRequire128BitTraceId = Mockito.mock(Propagation.Factory.class);
6467
given(doesNotRequire128BitTraceId.requires128BitTraceId()).willReturn(false);
68+
given(doesNotRequire128BitTraceId.get()).willReturn(new DummyPropagation("a"));
6569
CompositePropagationFactory factory = new CompositePropagationFactory(List.of(requires128BitTraceId),
6670
List.of(doesNotRequire128BitTraceId));
6771
assertThat(factory.requires128BitTraceId()).isTrue();

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositeTextMapPropagatorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void collectsAllFields() {
5858
@Test
5959
void injectAllFields() {
6060
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(List.of(field("a"), field("b")),
61-
Collections.emptyList());
61+
Collections.emptyList(), Collections.emptyList());
6262
TextMapSetter<Object> setter = setter();
6363
Object carrier = carrier();
6464
propagator.inject(context(), carrier, setter);
@@ -70,7 +70,7 @@ void injectAllFields() {
7070
@Test
7171
void extractMutuallyExclusive() {
7272
CompositeTextMapPropagator propagator = new CompositeTextMapPropagator(Collections.emptyList(),
73-
List.of(field("a"), field("b")));
73+
List.of(field("a"), field("b")), Collections.emptyList());
7474
Context context = context();
7575
Map<String, String> carrier = Map.of("a", "a-value", "b", "b-value");
7676
context = propagator.extract(context, carrier, new MapTextMapGetter());

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfigurationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21-
import java.util.stream.StreamSupport;
2221

2322
import io.micrometer.tracing.SpanCustomizer;
2423
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext;
@@ -218,7 +217,7 @@ void shouldSupplyW3CPropagationWithoutBaggageWhenDisabled() {
218217
private List<TextMapPropagator> getInjectors(TextMapPropagator propagator) {
219218
assertThat(propagator).as("propagator").isNotNull();
220219
if (propagator instanceof CompositeTextMapPropagator compositePropagator) {
221-
return StreamSupport.stream(compositePropagator.getInjectors().spliterator(), false).toList();
220+
return compositePropagator.getInjectors().stream().toList();
222221
}
223222
fail("Expected CompositeTextMapPropagator, found %s".formatted(propagator.getClass()));
224223
throw new AssertionError("Unreachable");

0 commit comments

Comments
 (0)