16
16
17
17
package org .springframework .boot .actuate .autoconfigure .tracing ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .Collection ;
19
21
import java .util .Collections ;
20
22
import java .util .List ;
21
23
52
54
53
55
import org .springframework .beans .factory .ObjectProvider ;
54
56
import org .springframework .boot .SpringBootVersion ;
57
+ import org .springframework .boot .actuate .autoconfigure .tracing .TracingProperties .Propagation .PropagationType ;
55
58
import org .springframework .boot .autoconfigure .AutoConfiguration ;
56
59
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
57
60
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
66
69
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
67
70
*
68
71
* @author Moritz Halbritter
72
+ * @author Marcin Grzejszczak
69
73
* @since 3.0.0
70
74
*/
71
75
@ AutoConfiguration (before = MicrometerTracingAutoConfiguration .class )
@@ -185,22 +189,17 @@ static class BaggageConfiguration {
185
189
}
186
190
187
191
@ Bean
188
- @ ConditionalOnProperty (prefix = "management.tracing.propagation" , name = "type" , havingValue = "W3C" ,
189
- matchIfMissing = true )
190
- TextMapPropagator w3cTextMapPropagatorWithBaggage (OtelCurrentTraceContext otelCurrentTraceContext ) {
192
+ TextMapPropagator textMapPropagatorWithBaggage (OtelCurrentTraceContext otelCurrentTraceContext ) {
191
193
List <String > remoteFields = this .tracingProperties .getBaggage ().getRemoteFields ();
192
- return TextMapPropagator .composite (W3CTraceContextPropagator .getInstance (),
193
- W3CBaggagePropagator .getInstance (), new BaggageTextMapPropagator (remoteFields ,
194
- new OtelBaggageManager (otelCurrentTraceContext , remoteFields , Collections .emptyList ())));
195
- }
196
-
197
- @ Bean
198
- @ ConditionalOnProperty (prefix = "management.tracing.propagation" , name = "type" , havingValue = "B3" )
199
- TextMapPropagator b3BaggageTextMapPropagator (OtelCurrentTraceContext otelCurrentTraceContext ) {
200
- List <String > remoteFields = this .tracingProperties .getBaggage ().getRemoteFields ();
201
- return TextMapPropagator .composite (B3Propagator .injectingSingleHeader (),
202
- new BaggageTextMapPropagator (remoteFields ,
203
- new OtelBaggageManager (otelCurrentTraceContext , remoteFields , Collections .emptyList ())));
194
+ BaggageTextMapPropagator baggagePropagator = new BaggageTextMapPropagator (remoteFields ,
195
+ new OtelBaggageManager (otelCurrentTraceContext , remoteFields , Collections .emptyList ()));
196
+ List <TextMapPropagator > injectors = new ArrayList <>(
197
+ TextMapPropagatorFactory .forTypes (this .tracingProperties .getPropagation ().getType (), true ));
198
+ injectors .add (baggagePropagator );
199
+ List <TextMapPropagator > extractors = new ArrayList <>(
200
+ TextMapPropagatorFactory .forTypes (PropagationType .orderedValues (), true ));
201
+ extractors .add (baggagePropagator );
202
+ return new CompositeTextMapPropagator (injectors , extractors );
204
203
}
205
204
206
205
@ Bean
@@ -218,18 +217,12 @@ Slf4JBaggageEventListener otelSlf4JBaggageEventListener() {
218
217
static class NoBaggageConfiguration {
219
218
220
219
@ Bean
221
- @ ConditionalOnMissingBean
222
- @ ConditionalOnProperty (prefix = "management.tracing.propagation" , name = "type" , havingValue = "B3" )
223
- B3Propagator b3TextMapPropagator () {
224
- return B3Propagator .injectingSingleHeader ();
225
- }
226
-
227
- @ Bean
228
- @ ConditionalOnMissingBean
229
- @ ConditionalOnProperty (prefix = "management.tracing.propagation" , name = "type" , havingValue = "W3C" ,
230
- matchIfMissing = true )
231
- W3CTraceContextPropagator w3cTextMapPropagatorWithoutBaggage () {
232
- return W3CTraceContextPropagator .getInstance ();
220
+ TextMapPropagator textMapPropagator (TracingProperties properties ) {
221
+ List <TextMapPropagator > injectors = TextMapPropagatorFactory .forTypes (properties .getPropagation ().getType (),
222
+ false );
223
+ List <TextMapPropagator > extractors = TextMapPropagatorFactory .forTypes (PropagationType .orderedValues (),
224
+ false );
225
+ return new CompositeTextMapPropagator (injectors , extractors );
233
226
}
234
227
235
228
}
@@ -251,4 +244,59 @@ public void publishEvent(Object event) {
251
244
252
245
}
253
246
247
+ /**
248
+ * Factory for {@link TextMapPropagator TextMapPropagators}.
249
+ */
250
+ private static final class TextMapPropagatorFactory {
251
+
252
+ private TextMapPropagatorFactory () {
253
+ }
254
+
255
+ /**
256
+ * Creates a new B3 propagator using a single B3 header.
257
+ * @return the B3 propagator
258
+ */
259
+ private static TextMapPropagator b3Single () {
260
+ return B3Propagator .injectingSingleHeader ();
261
+ }
262
+
263
+ /**
264
+ * Creates a new B3 propagator using multiple B3 headers.
265
+ * @return the B3 propagator
266
+ */
267
+ private static TextMapPropagator b3Multi () {
268
+ return B3Propagator .injectingMultiHeaders ();
269
+ }
270
+
271
+ /**
272
+ * Creates a new W3C propagator.
273
+ * @param baggage whether baggage propagation should be supported
274
+ * @return the W3C propagator
275
+ */
276
+ private static TextMapPropagator w3c (boolean baggage ) {
277
+ if (!baggage ) {
278
+ return W3CTraceContextPropagator .getInstance ();
279
+ }
280
+ return TextMapPropagator .composite (W3CTraceContextPropagator .getInstance (),
281
+ W3CBaggagePropagator .getInstance ());
282
+ }
283
+
284
+ private static TextMapPropagator forType (PropagationType type , boolean baggage ) {
285
+ return switch (type ) {
286
+ case B3 -> b3Single ();
287
+ case B3_MULTI -> b3Multi ();
288
+ case W3C -> w3c (baggage );
289
+ };
290
+ }
291
+
292
+ private static List <TextMapPropagator > forTypes (Collection <PropagationType > types , boolean baggage ) {
293
+ List <TextMapPropagator > result = new ArrayList <>(types .size ());
294
+ for (PropagationType type : types ) {
295
+ result .add (forType (type , baggage ));
296
+ }
297
+ return result ;
298
+ }
299
+
300
+ }
301
+
254
302
}
0 commit comments