21
21
import brave .Tracing ;
22
22
import brave .Tracing .Builder ;
23
23
import brave .TracingCustomizer ;
24
+ import brave .baggage .BaggageField ;
25
+ import brave .baggage .BaggagePropagation ;
26
+ import brave .baggage .BaggagePropagationConfig ;
27
+ import brave .baggage .BaggagePropagationCustomizer ;
28
+ import brave .baggage .CorrelationScopeConfig ;
29
+ import brave .baggage .CorrelationScopeCustomizer ;
30
+ import brave .baggage .CorrelationScopeDecorator ;
31
+ import brave .context .slf4j .MDCScopeDecorator ;
24
32
import brave .handler .SpanHandler ;
25
33
import brave .http .HttpClientHandler ;
26
34
import brave .http .HttpClientRequest ;
33
41
import brave .propagation .CurrentTraceContext ;
34
42
import brave .propagation .CurrentTraceContext .ScopeDecorator ;
35
43
import brave .propagation .CurrentTraceContextCustomizer ;
44
+ import brave .propagation .Propagation ;
36
45
import brave .propagation .Propagation .Factory ;
37
46
import brave .propagation .ThreadLocalCurrentTraceContext ;
38
47
import brave .sampler .Sampler ;
39
48
import io .micrometer .tracing .brave .bridge .BraveBaggageManager ;
40
49
import io .micrometer .tracing .brave .bridge .BraveCurrentTraceContext ;
41
50
import io .micrometer .tracing .brave .bridge .BraveHttpClientHandler ;
42
51
import io .micrometer .tracing .brave .bridge .BraveHttpServerHandler ;
52
+ import io .micrometer .tracing .brave .bridge .BravePropagator ;
43
53
import io .micrometer .tracing .brave .bridge .BraveTracer ;
54
+ import io .micrometer .tracing .brave .bridge .W3CPropagation ;
55
+ import org .slf4j .MDC ;
44
56
57
+ import org .springframework .beans .factory .ObjectProvider ;
45
58
import org .springframework .boot .autoconfigure .AutoConfiguration ;
46
59
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
60
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
47
61
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
48
62
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
63
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingClass ;
64
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
49
65
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
50
66
import org .springframework .context .annotation .Bean ;
51
67
import org .springframework .context .annotation .Configuration ;
55
71
* {@link EnableAutoConfiguration Auto-configuration} for Brave.
56
72
*
57
73
* @author Moritz Halbritter
74
+ * @author Marcin Grzejszczak
58
75
* @since 3.0.0
59
76
*/
60
77
@ AutoConfiguration (before = MicrometerTracingAutoConfiguration .class )
@@ -105,12 +122,6 @@ public CurrentTraceContext braveCurrentTraceContext(List<CurrentTraceContext.Sco
105
122
return builder .build ();
106
123
}
107
124
108
- @ Bean
109
- @ ConditionalOnMissingBean
110
- public Factory bravePropagationFactory () {
111
- return B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
112
- }
113
-
114
125
@ Bean
115
126
@ ConditionalOnMissingBean
116
127
public Sampler braveSampler (TracingProperties properties ) {
@@ -135,21 +146,35 @@ public HttpClientHandler<HttpClientRequest, HttpClientResponse> httpClientHandle
135
146
return HttpClientHandler .create (httpTracing );
136
147
}
137
148
149
+ @ Configuration (proxyBeanMethods = false )
150
+ @ ConditionalOnMissingClass ("io.micrometer.tracing.brave.bridge.BraveTracer" )
151
+ static class BraveMicrometerMissing {
152
+
153
+ @ Bean
154
+ @ ConditionalOnMissingBean
155
+ @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "B3" , matchIfMissing = true )
156
+ Factory bravePropagationFactory () {
157
+ return B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
158
+ }
159
+
160
+ }
161
+
138
162
@ Configuration (proxyBeanMethods = false )
139
163
@ ConditionalOnClass (BraveTracer .class )
140
164
static class BraveMicrometer {
141
165
166
+ private static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager ();
167
+
142
168
@ Bean
143
169
@ ConditionalOnMissingBean
144
- BraveTracer braveTracerBridge (brave .Tracer tracer , CurrentTraceContext currentTraceContext ,
145
- BraveBaggageManager braveBaggageManager ) {
146
- return new BraveTracer (tracer , new BraveCurrentTraceContext (currentTraceContext ), braveBaggageManager );
170
+ BraveTracer braveTracerBridge (brave .Tracer tracer , CurrentTraceContext currentTraceContext ) {
171
+ return new BraveTracer (tracer , new BraveCurrentTraceContext (currentTraceContext ), BRAVE_BAGGAGE_MANAGER );
147
172
}
148
173
149
174
@ Bean
150
175
@ ConditionalOnMissingBean
151
- BraveBaggageManager braveBaggageManager ( ) {
152
- return new BraveBaggageManager ( );
176
+ BravePropagator bravePropagator ( Tracing tracing ) {
177
+ return new BravePropagator ( tracing );
153
178
}
154
179
155
180
@ Bean
@@ -166,6 +191,101 @@ BraveHttpClientHandler braveHttpClientHandler(
166
191
return new BraveHttpClientHandler (httpClientHandler );
167
192
}
168
193
194
+ @ Configuration (proxyBeanMethods = false )
195
+ @ ConditionalOnProperty (value = "management.tracing.baggage.enabled" , havingValue = "false" ,
196
+ matchIfMissing = true )
197
+ static class BraveNoBaggageConfiguration {
198
+
199
+ @ Bean
200
+ @ ConditionalOnMissingBean
201
+ @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "W3C" ,
202
+ matchIfMissing = true )
203
+ Factory w3cPropagationNoBaggageFactory () {
204
+ return new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ()); // TODO: Use
205
+ // snapshots
206
+ // of
207
+ // tracing
208
+ // to not
209
+ // use
210
+ // baggage
211
+ // for W3C
212
+ }
213
+
214
+ @ Bean
215
+ @ ConditionalOnMissingBean
216
+ @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "B3" )
217
+ Factory b3PropagationNoBaggageFactory () {
218
+ return B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ();
219
+ }
220
+
221
+ }
222
+
223
+ @ Configuration (proxyBeanMethods = false )
224
+ @ ConditionalOnProperty (value = "management.tracing.baggage.enabled" , matchIfMissing = true )
225
+ static class BraveBaggageConfiguration {
226
+
227
+ @ Bean
228
+ @ ConditionalOnMissingBean
229
+ @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "W3C" ,
230
+ matchIfMissing = true )
231
+ BaggagePropagation .FactoryBuilder w3cPropagationFactory () {
232
+ return BaggagePropagation .newFactoryBuilder (new W3CPropagation (BRAVE_BAGGAGE_MANAGER , List .of ()));
233
+ }
234
+
235
+ @ Bean
236
+ @ ConditionalOnMissingBean
237
+ @ ConditionalOnProperty (value = "management.tracing.propagation.type" , havingValue = "B3" )
238
+ BaggagePropagation .FactoryBuilder b3PropagationFactory () {
239
+ return BaggagePropagation .newFactoryBuilder (
240
+ B3Propagation .newFactoryBuilder ().injectFormat (B3Propagation .Format .SINGLE_NO_PARENT ).build ());
241
+ }
242
+
243
+ @ Bean
244
+ @ ConditionalOnMissingBean
245
+ Propagation .Factory micrometerTracingPropagationWithBaggage (
246
+ BaggagePropagation .FactoryBuilder factoryBuilder , TracingProperties tracingProperties ,
247
+ ObjectProvider <List <BaggagePropagationCustomizer >> baggagePropagationCustomizers ) {
248
+ List <String > remoteFields = tracingProperties .getBaggage ().getRemoteFields ();
249
+ for (String fieldName : remoteFields ) {
250
+ factoryBuilder
251
+ .add (BaggagePropagationConfig .SingleBaggageField .remote (BaggageField .create (fieldName )));
252
+ }
253
+ baggagePropagationCustomizers .ifAvailable (
254
+ (customizers ) -> customizers .forEach ((customizer ) -> customizer .customize (factoryBuilder )));
255
+ return factoryBuilder .build ();
256
+ }
257
+
258
+ @ Bean
259
+ @ ConditionalOnMissingBean (CorrelationScopeDecorator .class )
260
+ @ ConditionalOnBean (CorrelationScopeDecorator .Builder .class )
261
+ @ ConditionalOnProperty (value = "management.tracing.baggage.correlation-enabled" , matchIfMissing = true )
262
+ ScopeDecorator correlationScopeDecorator (TracingProperties properties ,
263
+ ObjectProvider <List <CorrelationScopeCustomizer >> correlationScopeCustomizers ,
264
+ CorrelationScopeDecorator .Builder builder ) {
265
+ List <String > correlationFields = properties .getBaggage ().getCorrelationFields ();
266
+ for (String field : correlationFields ) {
267
+ builder .add (CorrelationScopeConfig .SingleCorrelationField .newBuilder (BaggageField .create (field ))
268
+ .flushOnUpdate ().build ());
269
+ }
270
+ correlationScopeCustomizers .ifAvailable (
271
+ (customizers ) -> customizers .forEach ((customizer ) -> customizer .customize (builder )));
272
+ return builder .build ();
273
+ }
274
+
275
+ }
276
+
277
+ }
278
+
279
+ @ Configuration (proxyBeanMethods = false )
280
+ static class CorrelationScopeDecoratorConfiguration {
281
+
282
+ @ Bean
283
+ @ ConditionalOnMissingBean
284
+ @ ConditionalOnClass (MDC .class )
285
+ CorrelationScopeDecorator .Builder mdcCorrelationScopeDecoratorBuilder () {
286
+ return MDCScopeDecorator .newBuilder ();
287
+ }
288
+
169
289
}
170
290
171
291
}
0 commit comments