Skip to content

Commit 532227d

Browse files
committed
Refactor injector and extractor code into CompositeTextMapPropagator
1 parent 09b9a28 commit 532227d

File tree

2 files changed

+84
-78
lines changed

2 files changed

+84
-78
lines changed

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

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.tracing;
1818

19+
import java.util.ArrayList;
20+
import java.util.Arrays;
1921
import java.util.Collection;
2022
import java.util.Collections;
23+
import java.util.List;
2124
import java.util.stream.Collectors;
2225
import java.util.stream.Stream;
2326
import java.util.stream.StreamSupport;
2427

28+
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
29+
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
2530
import io.opentelemetry.context.Context;
2631
import io.opentelemetry.context.propagation.TextMapGetter;
2732
import io.opentelemetry.context.propagation.TextMapPropagator;
2833
import io.opentelemetry.context.propagation.TextMapSetter;
34+
import io.opentelemetry.extension.trace.propagation.B3Propagator;
2935

3036
/**
3137
* {@link TextMapPropagator} which supports multiple tracing formats. It is able to
@@ -114,13 +120,85 @@ public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter)
114120
return currentContext;
115121
}
116122

123+
/**
124+
* Creates a new {@link CompositeTextMapPropagator}, which uses the given
125+
* {@code injectionTypes} for injection and all supported types for extraction.
126+
* @param injectionTypes the propagation types for injection
127+
* @return the {@link CompositeTextMapPropagator}
128+
*/
129+
static TextMapPropagator create(List<TracingProperties.Propagation.PropagationType> injectionTypes) {
130+
return create(null, injectionTypes);
131+
}
132+
133+
/**
134+
* Creates a new {@link CompositeTextMapPropagator}, which uses the given
135+
* {@code injectionTypes} for injection and all supported types for extraction.
136+
* @param baggagePropagator the baggage propagator to use, or {@code null}
137+
* @param injectionTypes the propagation types for injection
138+
* @return the {@link CompositeTextMapPropagator}
139+
*/
140+
static CompositeTextMapPropagator create(TextMapPropagator baggagePropagator,
141+
Iterable<TracingProperties.Propagation.PropagationType> injectionTypes) {
142+
List<TextMapPropagator> injectors = stream(injectionTypes)
143+
.map((injection) -> forType(injection, baggagePropagator != null))
144+
.collect(Collectors.toCollection(ArrayList::new));
145+
if (baggagePropagator != null) {
146+
injectors.add(baggagePropagator);
147+
}
148+
List<TextMapPropagator> extractors = Arrays.stream(TracingProperties.Propagation.PropagationType.values())
149+
.map((extraction) -> forType(extraction, baggagePropagator != null))
150+
.toList();
151+
return new CompositeTextMapPropagator(injectors, extractors,
152+
(baggagePropagator != null) ? List.of(baggagePropagator) : Collections.emptyList());
153+
}
154+
117155
@SafeVarargs
118-
private static <T> Stream<T> concat(Iterable<T>... iterable) {
156+
private static <T> Stream<T> concat(Iterable<T>... iterables) {
119157
Stream<T> result = Stream.empty();
120-
for (Iterable<T> anIterable : iterable) {
121-
result = Stream.concat(result, StreamSupport.stream(anIterable.spliterator(), false));
158+
for (Iterable<T> iterable : iterables) {
159+
result = Stream.concat(result, stream(iterable));
122160
}
123161
return result;
124162
}
125163

164+
private static <T> Stream<T> stream(Iterable<T> iterable) {
165+
return StreamSupport.stream(iterable.spliterator(), false);
166+
}
167+
168+
/**
169+
* Creates a new B3 propagator using a single B3 header.
170+
* @return the B3 propagator
171+
*/
172+
private static TextMapPropagator b3Single() {
173+
return B3Propagator.injectingSingleHeader();
174+
}
175+
176+
/**
177+
* Creates a new B3 propagator using multiple B3 headers.
178+
* @return the B3 propagator
179+
*/
180+
private static TextMapPropagator b3Multi() {
181+
return B3Propagator.injectingMultiHeaders();
182+
}
183+
184+
/**
185+
* Creates a new W3C propagator.
186+
* @param baggage whether baggage propagation should be supported
187+
* @return the W3C propagator
188+
*/
189+
private static TextMapPropagator w3c(boolean baggage) {
190+
if (!baggage) {
191+
return W3CTraceContextPropagator.getInstance();
192+
}
193+
return TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance());
194+
}
195+
196+
private static TextMapPropagator forType(TracingProperties.Propagation.PropagationType type, boolean baggage) {
197+
return switch (type) {
198+
case B3 -> b3Single();
199+
case B3_MULTI -> b3Multi();
200+
case W3C -> w3c(baggage);
201+
};
202+
}
203+
126204
}

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

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.tracing;
1818

19-
import java.util.ArrayList;
20-
import java.util.Arrays;
21-
import java.util.Collection;
2219
import java.util.Collections;
2320
import java.util.List;
2421

@@ -35,14 +32,11 @@
3532
import io.micrometer.tracing.otel.bridge.Slf4JEventListener;
3633
import io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator;
3734
import io.opentelemetry.api.OpenTelemetry;
38-
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
3935
import io.opentelemetry.api.common.Attributes;
4036
import io.opentelemetry.api.trace.Tracer;
41-
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
4237
import io.opentelemetry.context.ContextStorage;
4338
import io.opentelemetry.context.propagation.ContextPropagators;
4439
import io.opentelemetry.context.propagation.TextMapPropagator;
45-
import io.opentelemetry.extension.trace.propagation.B3Propagator;
4640
import io.opentelemetry.sdk.OpenTelemetrySdk;
4741
import io.opentelemetry.sdk.resources.Resource;
4842
import io.opentelemetry.sdk.trace.SdkTracerProvider;
@@ -55,7 +49,6 @@
5549

5650
import org.springframework.beans.factory.ObjectProvider;
5751
import org.springframework.boot.SpringBootVersion;
58-
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Propagation.PropagationType;
5952
import org.springframework.boot.autoconfigure.AutoConfiguration;
6053
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
6154
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -194,11 +187,8 @@ TextMapPropagator textMapPropagatorWithBaggage(OtelCurrentTraceContext otelCurre
194187
List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields();
195188
BaggageTextMapPropagator baggagePropagator = new BaggageTextMapPropagator(remoteFields,
196189
new OtelBaggageManager(otelCurrentTraceContext, remoteFields, Collections.emptyList()));
197-
List<TextMapPropagator> injectors = new ArrayList<>(
198-
TextMapPropagatorFactory.forTypes(this.tracingProperties.getPropagation().getType(), true));
199-
injectors.add(baggagePropagator);
200-
List<TextMapPropagator> extractors = TextMapPropagatorFactory.forTypes(PropagationType.values(), true);
201-
return new CompositeTextMapPropagator(injectors, extractors, List.of(baggagePropagator));
190+
return CompositeTextMapPropagator.create(baggagePropagator,
191+
this.tracingProperties.getPropagation().getType());
202192
}
203193

204194
@Bean
@@ -217,10 +207,7 @@ static class NoBaggageConfiguration {
217207

218208
@Bean
219209
TextMapPropagator textMapPropagator(TracingProperties properties) {
220-
List<TextMapPropagator> injectors = TextMapPropagatorFactory.forTypes(properties.getPropagation().getType(),
221-
false);
222-
List<TextMapPropagator> extractors = TextMapPropagatorFactory.forTypes(PropagationType.values(), false);
223-
return new CompositeTextMapPropagator(injectors, extractors);
210+
return CompositeTextMapPropagator.create(properties.getPropagation().getType());
224211
}
225212

226213
}
@@ -242,63 +229,4 @@ public void publishEvent(Object event) {
242229

243230
}
244231

245-
/**
246-
* Factory for {@link TextMapPropagator TextMapPropagators}.
247-
*/
248-
private static final class TextMapPropagatorFactory {
249-
250-
private TextMapPropagatorFactory() {
251-
}
252-
253-
/**
254-
* Creates a new B3 propagator using a single B3 header.
255-
* @return the B3 propagator
256-
*/
257-
private static TextMapPropagator b3Single() {
258-
return B3Propagator.injectingSingleHeader();
259-
}
260-
261-
/**
262-
* Creates a new B3 propagator using multiple B3 headers.
263-
* @return the B3 propagator
264-
*/
265-
private static TextMapPropagator b3Multi() {
266-
return B3Propagator.injectingMultiHeaders();
267-
}
268-
269-
/**
270-
* Creates a new W3C propagator.
271-
* @param baggage whether baggage propagation should be supported
272-
* @return the W3C propagator
273-
*/
274-
private static TextMapPropagator w3c(boolean baggage) {
275-
if (!baggage) {
276-
return W3CTraceContextPropagator.getInstance();
277-
}
278-
return TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(),
279-
W3CBaggagePropagator.getInstance());
280-
}
281-
282-
private static TextMapPropagator forType(PropagationType type, boolean baggage) {
283-
return switch (type) {
284-
case B3 -> b3Single();
285-
case B3_MULTI -> b3Multi();
286-
case W3C -> w3c(baggage);
287-
};
288-
}
289-
290-
private static List<TextMapPropagator> forTypes(Collection<PropagationType> types, boolean baggage) {
291-
List<TextMapPropagator> result = new ArrayList<>(types.size());
292-
for (PropagationType type : types) {
293-
result.add(forType(type, baggage));
294-
}
295-
return result;
296-
}
297-
298-
private static List<TextMapPropagator> forTypes(PropagationType[] types, boolean baggage) {
299-
return forTypes(Arrays.stream(types).toList(), baggage);
300-
}
301-
302-
}
303-
304232
}

0 commit comments

Comments
 (0)