16
16
17
17
package org .springframework .boot .actuate .autoconfigure .tracing ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .Collections ;
19
21
import java .util .List ;
20
22
21
23
import brave .CurrentSpanCustomizer ;
34
36
import brave .baggage .CorrelationScopeDecorator ;
35
37
import brave .context .slf4j .MDCScopeDecorator ;
36
38
import brave .handler .SpanHandler ;
39
+ import brave .propagation .B3Propagation ;
37
40
import brave .propagation .CurrentTraceContext ;
38
41
import brave .propagation .CurrentTraceContext .ScopeDecorator ;
39
42
import brave .propagation .CurrentTraceContextCustomizer ;
40
43
import brave .propagation .Propagation .Factory ;
41
44
import brave .propagation .ThreadLocalCurrentTraceContext ;
42
45
import brave .sampler .Sampler ;
46
+ import io .micrometer .tracing .BaggageManager ;
43
47
import io .micrometer .tracing .brave .bridge .BraveBaggageManager ;
44
48
import io .micrometer .tracing .brave .bridge .BraveCurrentTraceContext ;
45
49
import io .micrometer .tracing .brave .bridge .BravePropagator ;
46
50
import io .micrometer .tracing .brave .bridge .BraveSpanCustomizer ;
47
51
import io .micrometer .tracing .brave .bridge .BraveTracer ;
48
52
import io .micrometer .tracing .brave .bridge .CompositeSpanHandler ;
53
+ import io .micrometer .tracing .brave .bridge .W3CPropagation ;
49
54
import io .micrometer .tracing .exporter .SpanExportingPredicate ;
50
55
import io .micrometer .tracing .exporter .SpanFilter ;
51
56
import io .micrometer .tracing .exporter .SpanReporter ;
76
81
@ ConditionalOnEnabledTracing
77
82
public class BraveAutoConfiguration {
78
83
79
- private static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager ();
80
-
81
84
/**
82
85
* Default value for application name if {@code spring.application.name} is not set.
83
86
*/
@@ -92,19 +95,25 @@ CompositeSpanHandler compositeSpanHandler(ObjectProvider<SpanExportingPredicate>
92
95
filters .orderedStream ().toList ());
93
96
}
94
97
98
+ @ Bean
99
+ @ ConditionalOnMissingBean
100
+ BraveBaggageManager braveBaggageManager () {
101
+ return new BraveBaggageManager ();
102
+ }
103
+
95
104
@ Bean
96
105
@ ConditionalOnMissingBean
97
106
public Tracing braveTracing (Environment environment , List <SpanHandler > spanHandlers ,
98
107
List <TracingCustomizer > tracingCustomizers , CurrentTraceContext currentTraceContext ,
99
108
Factory propagationFactory , Sampler sampler ) {
100
109
String applicationName = environment .getProperty ("spring.application.name" , DEFAULT_APPLICATION_NAME );
101
110
Builder builder = Tracing .newBuilder ()
102
- .currentTraceContext (currentTraceContext )
103
- .traceId128Bit (true )
104
- .supportsJoin (false )
105
- .propagationFactory (propagationFactory )
106
- .sampler (sampler )
107
- .localServiceName (applicationName );
111
+ .currentTraceContext (currentTraceContext )
112
+ .traceId128Bit (true )
113
+ .supportsJoin (false )
114
+ .propagationFactory (propagationFactory )
115
+ .sampler (sampler )
116
+ .localServiceName (applicationName );
108
117
spanHandlers .forEach (builder ::addSpanHandler );
109
118
for (TracingCustomizer tracingCustomizer : tracingCustomizers ) {
110
119
tracingCustomizer .customize (builder );
@@ -138,8 +147,9 @@ public Sampler braveSampler(TracingProperties properties) {
138
147
139
148
@ Bean
140
149
@ ConditionalOnMissingBean (io .micrometer .tracing .Tracer .class )
141
- BraveTracer braveTracerBridge (brave .Tracer tracer , CurrentTraceContext currentTraceContext ) {
142
- return new BraveTracer (tracer , new BraveCurrentTraceContext (currentTraceContext ), BRAVE_BAGGAGE_MANAGER );
150
+ BraveTracer braveTracerBridge (brave .Tracer tracer , CurrentTraceContext currentTraceContext ,
151
+ BraveBaggageManager braveBaggageManager ) {
152
+ return new BraveTracer (tracer , new BraveCurrentTraceContext (currentTraceContext ), braveBaggageManager );
143
153
}
144
154
145
155
@ Bean
@@ -166,8 +176,20 @@ static class BraveNoBaggageConfiguration {
166
176
167
177
@ Bean
168
178
@ ConditionalOnMissingBean
169
- Factory propagationFactory (TracingProperties tracing ) {
170
- return new CompositePropagationFactory (tracing .getPropagation ().getType (), null , injectorFactories , extractorFactories );
179
+ ExtractorFactories extractorFactories () {
180
+ return ExtractorFactories .of (PropagationFactoryFactory .factoriesFor (PropagationType .values ()));
181
+ }
182
+
183
+ @ Bean
184
+ @ ConditionalOnMissingBean
185
+ InjectorFactories injectorFactories (TracingProperties tracing ) {
186
+ return InjectorFactories .of (PropagationFactoryFactory .factoriesFor (tracing .getPropagation ().getType ()));
187
+ }
188
+
189
+ @ Bean
190
+ @ ConditionalOnMissingBean
191
+ Factory propagationFactory (InjectorFactories injectorFactories , ExtractorFactories extractorFactories ) {
192
+ return new CompositePropagationFactory (injectorFactories , extractorFactories );
171
193
}
172
194
173
195
}
@@ -182,11 +204,26 @@ static class BraveBaggageConfiguration {
182
204
this .tracingProperties = tracingProperties ;
183
205
}
184
206
207
+ @ Bean
208
+ @ ConditionalOnMissingBean
209
+ ExtractorFactories extractorFactories (BraveBaggageManager braveBaggageManager ) {
210
+ return ExtractorFactories
211
+ .of (PropagationFactoryFactory .factoriesFor (braveBaggageManager , PropagationType .values ()));
212
+ }
213
+
214
+ @ Bean
215
+ @ ConditionalOnMissingBean
216
+ InjectorFactories injectorFactories (BraveBaggageManager braveBaggageManager ) {
217
+ return InjectorFactories .of (PropagationFactoryFactory .factoriesFor (braveBaggageManager ,
218
+ this .tracingProperties .getPropagation ().getType ()));
219
+ }
220
+
185
221
@ Bean
186
222
@ ConditionalOnMissingBean
187
223
BaggagePropagation .FactoryBuilder propagationFactoryBuilder (
188
- ObjectProvider <BaggagePropagationCustomizer > baggagePropagationCustomizers ) {
189
- Factory delegate = new CompositePropagationFactory (this .tracingProperties .getPropagation ().getType (), BRAVE_BAGGAGE_MANAGER , injectorFactories , extractorFactories );
224
+ ObjectProvider <BaggagePropagationCustomizer > baggagePropagationCustomizers ,
225
+ InjectorFactories injectorFactories , ExtractorFactories extractorFactories ) {
226
+ Factory delegate = new CompositePropagationFactory (injectorFactories , extractorFactories );
190
227
FactoryBuilder builder = BaggagePropagation .newFactoryBuilder (delegate );
191
228
baggagePropagationCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (builder ));
192
229
return builder ;
@@ -227,8 +264,8 @@ CorrelationScopeCustomizer correlationFieldsCorrelationScopeCustomizer() {
227
264
List <String > correlationFields = this .tracingProperties .getBaggage ().getCorrelation ().getFields ();
228
265
for (String field : correlationFields ) {
229
266
builder .add (CorrelationScopeConfig .SingleCorrelationField .newBuilder (BaggageField .create (field ))
230
- .flushOnUpdate ()
231
- .build ());
267
+ .flushOnUpdate ()
268
+ .build ());
232
269
}
233
270
};
234
271
}
@@ -241,4 +278,105 @@ ScopeDecorator correlationScopeDecorator(CorrelationScopeDecorator.Builder build
241
278
242
279
}
243
280
281
+ /**
282
+ * A collection of {@link Factory factories} used for trace context extraction.
283
+ *
284
+ * @author Moritz Halbritter
285
+ * @since 3.0.8
286
+ */
287
+ public interface ExtractorFactories extends Iterable <Factory > {
288
+
289
+ /**
290
+ * Creates an {@link ExtractorFactories} object from the given factories.
291
+ * @param factories factories
292
+ * @return {@link ExtractorFactories} object
293
+ */
294
+ static ExtractorFactories of (Iterable <Factory > factories ) {
295
+ return factories ::iterator ;
296
+ }
297
+
298
+ static ExtractorFactories of (PropagationType ... types ) {
299
+ return of (PropagationFactoryFactory .factoriesFor (types ));
300
+ }
301
+
302
+ static ExtractorFactories of (BaggageManager baggageManager , PropagationType ... types ) {
303
+ return of (PropagationFactoryFactory .factoriesFor (baggageManager , types ));
304
+ }
305
+
306
+ }
307
+
308
+ /**
309
+ * A collection of {@link Factory factories} used for trace context injection.
310
+ *
311
+ * @author Moritz Halbritter
312
+ * @since 3.0.8
313
+ */
314
+ public interface InjectorFactories extends Iterable <Factory > {
315
+
316
+ /**
317
+ * Creates an {@link InjectorFactories} object from the given factories.
318
+ * @param factories factories
319
+ * @return {@link InjectorFactories} object
320
+ */
321
+ static InjectorFactories of (Iterable <Factory > factories ) {
322
+ return factories ::iterator ;
323
+ }
324
+
325
+ static InjectorFactories of (PropagationType ... types ) {
326
+ return of (PropagationFactoryFactory .factoriesFor (types ));
327
+ }
328
+
329
+ static InjectorFactories of (BaggageManager baggageManager , PropagationType ... types ) {
330
+ return of (PropagationFactoryFactory .factoriesFor (baggageManager , types ));
331
+ }
332
+
333
+ }
334
+
335
+ /**
336
+ * Factory for {@link Factory propagation factories}.
337
+ */
338
+ private static class PropagationFactoryFactory {
339
+
340
+ private PropagationFactoryFactory () {
341
+ }
342
+
343
+ /**
344
+ * Creates a new B3 propagation factory.
345
+ * @return B3 propagation factory.
346
+ */
347
+ private static Factory b3 () {
348
+ return B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
349
+ }
350
+
351
+ /**
352
+ * Creates a new W3C propagation factory.
353
+ * @param baggageManager baggage manager to use, or {@code null}
354
+ * @return W3C Propagation propagation factory
355
+ */
356
+ private static W3CPropagation w3c (BaggageManager baggageManager ) {
357
+ return (baggageManager != null ) ? new W3CPropagation (baggageManager , Collections .emptyList ())
358
+ : new W3CPropagation ();
359
+ }
360
+
361
+ private static Factory forType (BaggageManager baggageManager , PropagationType type ) {
362
+ return switch (type ) {
363
+ case B3 -> PropagationFactoryFactory .b3 ();
364
+ case W3C -> PropagationFactoryFactory .w3c (baggageManager );
365
+ };
366
+ }
367
+
368
+ private static List <Factory > factoriesFor (PropagationType ... types ) {
369
+ return factoriesFor (null , types );
370
+ }
371
+
372
+ private static List <Factory > factoriesFor (BaggageManager baggageManager , PropagationType ... types ) {
373
+ List <Factory > result = new ArrayList <>(types .length );
374
+ for (PropagationType type : types ) {
375
+ result .add (forType (baggageManager , type ));
376
+ }
377
+ return result ;
378
+ }
379
+
380
+ }
381
+
244
382
}
0 commit comments