24
24
import brave .TracingCustomizer ;
25
25
import brave .baggage .BaggageField ;
26
26
import brave .baggage .BaggagePropagation ;
27
+ import brave .baggage .BaggagePropagation .FactoryBuilder ;
27
28
import brave .baggage .BaggagePropagationConfig ;
28
29
import brave .baggage .BaggagePropagationCustomizer ;
29
30
import brave .baggage .CorrelationScopeConfig ;
42
43
import brave .propagation .CurrentTraceContext ;
43
44
import brave .propagation .CurrentTraceContext .ScopeDecorator ;
44
45
import brave .propagation .CurrentTraceContextCustomizer ;
45
- import brave .propagation .Propagation ;
46
46
import brave .propagation .Propagation .Factory ;
47
47
import brave .propagation .ThreadLocalCurrentTraceContext ;
48
48
import brave .sampler .Sampler ;
53
53
import io .micrometer .tracing .brave .bridge .BravePropagator ;
54
54
import io .micrometer .tracing .brave .bridge .BraveTracer ;
55
55
import io .micrometer .tracing .brave .bridge .W3CPropagation ;
56
- import org .slf4j .MDC ;
57
56
58
57
import org .springframework .beans .factory .ObjectProvider ;
59
58
import org .springframework .boot .autoconfigure .AutoConfiguration ;
60
59
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
61
- import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
62
60
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
63
61
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
64
62
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
65
63
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
66
64
import org .springframework .context .annotation .Bean ;
67
65
import org .springframework .context .annotation .Configuration ;
66
+ import org .springframework .core .annotation .Order ;
68
67
import org .springframework .core .env .Environment ;
69
68
70
69
/**
@@ -175,29 +174,17 @@ BraveHttpClientHandler braveHttpClientHandler(
175
174
}
176
175
177
176
@ Configuration (proxyBeanMethods = false )
178
- @ ConditionalOnProperty (value = "management.tracing.baggage.enabled" , havingValue = "false" , matchIfMissing = true )
177
+ @ ConditionalOnProperty (value = "management.tracing.baggage.enabled" , havingValue = "false" )
179
178
static class BraveNoBaggageConfiguration {
180
179
181
180
@ Bean
182
181
@ ConditionalOnMissingBean
183
- @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "W3C" ,
184
- matchIfMissing = true )
185
- Factory w3cPropagationNoBaggageFactory () {
186
- return new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ()); // TODO: Use
187
- // snapshots
188
- // of
189
- // tracing
190
- // to not
191
- // use
192
- // baggage
193
- // for W3C
194
- }
195
-
196
- @ Bean
197
- @ ConditionalOnMissingBean
198
- @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "B3" )
199
- Factory b3PropagationNoBaggageFactory () {
200
- return B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
182
+ Factory propagationFactory (TracingProperties tracing ) {
183
+ return switch (tracing .getPropagation ().getType ()) {
184
+ case B3 ->
185
+ B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
186
+ case W3C -> new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ());
187
+ };
201
188
}
202
189
203
190
}
@@ -206,74 +193,70 @@ Factory b3PropagationNoBaggageFactory() {
206
193
@ ConditionalOnProperty (value = "management.tracing.baggage.enabled" , matchIfMissing = true )
207
194
static class BraveBaggageConfiguration {
208
195
196
+ private final TracingProperties tracingProperties ;
197
+
198
+ BraveBaggageConfiguration (TracingProperties tracingProperties ) {
199
+ this .tracingProperties = tracingProperties ;
200
+ }
201
+
209
202
@ Bean
210
203
@ ConditionalOnMissingBean
211
- @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "W3C" ,
212
- matchIfMissing = true )
213
- BaggagePropagation .FactoryBuilder w3cPropagationFactory () {
214
- return BaggagePropagation .newFactoryBuilder (new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ()));
204
+ BaggagePropagation .FactoryBuilder propagationFactoryBuilder (
205
+ ObjectProvider <BaggagePropagationCustomizer > baggagePropagationCustomizers ) {
206
+ Factory delegate = switch (this .tracingProperties .getPropagation ().getType ()) {
207
+ case B3 ->
208
+ B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
209
+ case W3C -> new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ());
210
+ };
211
+ FactoryBuilder builder = BaggagePropagation .newFactoryBuilder (delegate );
212
+ baggagePropagationCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (builder ));
213
+ return builder ;
215
214
}
216
215
217
216
@ Bean
218
- @ ConditionalOnMissingBean
219
- @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "B3" )
220
- BaggagePropagation .FactoryBuilder b3PropagationFactory () {
221
- return BaggagePropagation .newFactoryBuilder (
222
- B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ());
217
+ @ Order (0 )
218
+ BaggagePropagationCustomizer remoteFieldsBaggagePropagationCustomizer () {
219
+ return (builder ) -> {
220
+ List <String > remoteFields = this .tracingProperties .getBaggage ().getRemoteFields ();
221
+ for (String fieldName : remoteFields ) {
222
+ builder .add (BaggagePropagationConfig .SingleBaggageField .remote (BaggageField .create (fieldName )));
223
+ }
224
+ };
223
225
}
224
226
225
227
@ Bean
226
228
@ ConditionalOnMissingBean
227
- Propagation .Factory micrometerTracingPropagationWithBaggage (BaggagePropagation .FactoryBuilder factoryBuilder ,
228
- TracingProperties tracingProperties ,
229
- ObjectProvider <List <BaggagePropagationCustomizer >> baggagePropagationCustomizers ) {
230
- List <String > remoteFields = tracingProperties .getBaggage ().getRemoteFields ();
231
- for (String fieldName : remoteFields ) {
232
- factoryBuilder .add (BaggagePropagationConfig .SingleBaggageField .remote (BaggageField .create (fieldName )));
233
- }
234
- baggagePropagationCustomizers .ifAvailable (
235
- (customizers ) -> customizers .forEach ((customizer ) -> customizer .customize (factoryBuilder )));
229
+ Factory propagationFactory (BaggagePropagation .FactoryBuilder factoryBuilder ) {
236
230
return factoryBuilder .build ();
237
231
}
238
232
239
233
@ Bean
240
- @ ConditionalOnMissingBean (CorrelationScopeDecorator .class )
241
- @ ConditionalOnBean (CorrelationScopeDecorator .Builder .class )
242
- @ ConditionalOnProperty (value = "management.tracing.baggage.correlation.enabled" , matchIfMissing = true )
243
- ScopeDecorator correlationFieldsCorrelationScopeDecorator (TracingProperties properties ,
244
- ObjectProvider <List <CorrelationScopeCustomizer >> correlationScopeCustomizers ,
245
- CorrelationScopeDecorator .Builder builder ) {
246
- List <String > correlationFields = properties .getBaggage ().getCorrelation ().getFields ();
247
- for (String field : correlationFields ) {
248
- builder .add (CorrelationScopeConfig .SingleCorrelationField .newBuilder (BaggageField .create (field ))
249
- .flushOnUpdate ().build ());
250
- }
251
- correlationScopeCustomizers
252
- .ifAvailable ((customizers ) -> customizers .forEach ((customizer ) -> customizer .customize (builder )));
253
- return builder .build ();
234
+ @ ConditionalOnMissingBean
235
+ CorrelationScopeDecorator .Builder mdcCorrelationScopeDecoratorBuilder (
236
+ ObjectProvider <CorrelationScopeCustomizer > correlationScopeCustomizers ) {
237
+ CorrelationScopeDecorator .Builder builder = MDCScopeDecorator .newBuilder ();
238
+ correlationScopeCustomizers .forEach ((customizer ) -> customizer .customize (builder ));
239
+ return builder ;
254
240
}
255
241
256
242
@ Bean
257
- @ ConditionalOnMissingBean (CorrelationScopeDecorator .class )
258
- @ ConditionalOnBean (CorrelationScopeDecorator .Builder .class )
259
- @ ConditionalOnProperty (value = "management.tracing.baggage.correlation.enabled" , havingValue = "false" )
260
- ScopeDecorator noCorrelationFieldsCorrelationScopeDecorator (CorrelationScopeDecorator .Builder builder ,
261
- ObjectProvider <List <CorrelationScopeCustomizer >> correlationScopeCustomizers ) {
262
- correlationScopeCustomizers
263
- .ifAvailable ((customizers ) -> customizers .forEach ((customizer ) -> customizer .customize (builder )));
264
- return builder .build ();
243
+ @ Order (0 )
244
+ @ ConditionalOnProperty (prefix = "management.tracing.baggage.correlation" , name = "enabled" ,
245
+ matchIfMissing = true )
246
+ CorrelationScopeCustomizer correlationFieldsCorrelationScopeCustomizer () {
247
+ return (builder ) -> {
248
+ List <String > correlationFields = this .tracingProperties .getBaggage ().getCorrelation ().getFields ();
249
+ for (String field : correlationFields ) {
250
+ builder .add (CorrelationScopeConfig .SingleCorrelationField .newBuilder (BaggageField .create (field ))
251
+ .flushOnUpdate ().build ());
252
+ }
253
+ };
265
254
}
266
255
267
- }
268
-
269
- @ Configuration (proxyBeanMethods = false )
270
- static class CorrelationScopeDecoratorConfiguration {
271
-
272
256
@ Bean
273
- @ ConditionalOnMissingBean
274
- @ ConditionalOnClass (MDC .class )
275
- CorrelationScopeDecorator .Builder mdcCorrelationScopeDecoratorBuilder () {
276
- return MDCScopeDecorator .newBuilder ();
257
+ @ ConditionalOnMissingBean (CorrelationScopeDecorator .class )
258
+ ScopeDecorator correlationScopeDecorator (CorrelationScopeDecorator .Builder builder ) {
259
+ return builder .build ();
277
260
}
278
261
279
262
}
0 commit comments