/
BraveAutoConfigurationTests.java
382 lines (323 loc) · 13.1 KB
/
BraveAutoConfigurationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collections;
import brave.Span;
import brave.SpanCustomizer;
import brave.Tracer;
import brave.Tracing;
import brave.baggage.BaggagePropagation;
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
import brave.handler.SpanHandler;
import brave.propagation.CurrentTraceContext;
import brave.propagation.CurrentTraceContext.ScopeDecorator;
import brave.propagation.Propagation;
import brave.propagation.Propagation.Factory;
import brave.sampler.Sampler;
import io.micrometer.tracing.brave.bridge.BraveBaggageManager;
import io.micrometer.tracing.brave.bridge.BraveSpanCustomizer;
import io.micrometer.tracing.brave.bridge.BraveTracer;
import io.micrometer.tracing.brave.bridge.CompositeSpanHandler;
import io.micrometer.tracing.brave.bridge.W3CPropagation;
import io.micrometer.tracing.exporter.SpanExportingPredicate;
import io.micrometer.tracing.exporter.SpanFilter;
import io.micrometer.tracing.exporter.SpanReporter;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.tracing.BraveAutoConfigurationTests.SpanHandlerConfiguration.AdditionalSpanHandler;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link BraveAutoConfiguration}.
*
* @author Moritz Halbritter
*/
class BraveAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(BraveAutoConfiguration.class));
@Test
void shouldSupplyDefaultBeans() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(BraveAutoConfiguration.class);
assertThat(context).hasSingleBean(Tracing.class);
assertThat(context).hasSingleBean(Tracer.class);
assertThat(context).hasSingleBean(CurrentTraceContext.class);
assertThat(context).hasSingleBean(Factory.class);
assertThat(context).hasSingleBean(Sampler.class);
assertThat(context).hasSingleBean(BraveTracer.class);
assertThat(context).hasSingleBean(Propagation.Factory.class);
assertThat(context).hasSingleBean(BaggagePropagation.FactoryBuilder.class);
assertThat(context).hasSingleBean(BraveTracer.class);
assertThat(context).hasSingleBean(CompositeSpanHandler.class);
assertThat(context).hasSingleBean(SpanCustomizer.class);
assertThat(context).hasSingleBean(BraveSpanCustomizer.class);
});
}
@Test
void shouldBackOffOnCustomBeans() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
assertThat(context).hasBean("customTracing");
assertThat(context).hasSingleBean(Tracing.class);
assertThat(context).hasBean("customTracer");
assertThat(context).hasSingleBean(Tracer.class);
assertThat(context).hasBean("customCurrentTraceContext");
assertThat(context).hasSingleBean(CurrentTraceContext.class);
assertThat(context).hasBean("customFactory");
assertThat(context).hasSingleBean(Factory.class);
assertThat(context).hasBean("customSampler");
assertThat(context).hasSingleBean(Sampler.class);
assertThat(context).hasBean("customMicrometerTracer");
assertThat(context).hasSingleBean(io.micrometer.tracing.Tracer.class);
assertThat(context).hasBean("customBraveBaggageManager");
assertThat(context).hasSingleBean(BraveBaggageManager.class);
assertThat(context).hasBean("customCompositeSpanHandler");
assertThat(context).hasSingleBean(CompositeSpanHandler.class);
assertThat(context).hasBean("customSpanCustomizer");
assertThat(context).hasSingleBean(SpanCustomizer.class);
assertThat(context).hasBean("customMicrometerSpanCustomizer");
assertThat(context).hasSingleBean(io.micrometer.tracing.SpanCustomizer.class);
});
}
@Test
void shouldSupplyMicrometerBeans() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(BraveTracer.class));
}
@Test
void shouldNotSupplyBeansIfBraveIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader("brave"))
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
}
@Test
void shouldNotSupplyBeansIfMicrometerIsMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader("io.micrometer"))
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
}
@Test
void shouldSupplyW3CPropagationFactoryByDefault() {
this.contextRunner.run((context) -> {
assertThat(context).hasBean("propagationFactory");
assertThat(context).hasSingleBean(W3CPropagation.class);
assertThat(context).hasSingleBean(BaggagePropagation.FactoryBuilder.class);
});
}
@Test
void shouldSupplyB3PropagationFactoryViaProperty() {
this.contextRunner.withPropertyValues("management.tracing.propagation.type=B3").run((context) -> {
assertThat(context).hasBean("propagationFactory");
assertThat(context.getBean(Factory.class).toString()).isEqualTo("B3Propagation");
assertThat(context).hasSingleBean(BaggagePropagation.FactoryBuilder.class);
});
}
@Test
void shouldNotSupplyBeansIfTracingIsDisabled() {
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
}
@Test
void shouldNotSupplyCorrelationScopeDecoratorIfBaggageDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean("correlationScopeDecorator"));
}
@Test
void shouldSupplyW3CWithoutBaggageByDefaultIfBaggageDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false").run((context) -> {
assertThat(context).hasBean("propagationFactory");
assertThat(context).hasSingleBean(W3CPropagation.class);
assertThat(context).doesNotHaveBean(BaggagePropagation.FactoryBuilder.class);
});
}
@Test
void shouldSupplyB3WithoutBaggageIfBaggageDisabledAndB3Picked() {
this.contextRunner.withPropertyValues("management.tracing.baggage.enabled=false",
"management.tracing.propagation.type=B3").run((context) -> {
assertThat(context).hasBean("propagationFactory");
assertThat(context.getBean(Factory.class).toString()).isEqualTo("B3Propagation");
assertThat(context).doesNotHaveBean(BaggagePropagation.FactoryBuilder.class);
});
}
@Test
void shouldNotApplyCorrelationFieldsIfBaggageCorrelationDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.correlation.enabled=false",
"management.tracing.baggage.correlation.fields=alpha,bravo").run((context) -> {
ScopeDecorator scopeDecorator = context.getBean(ScopeDecorator.class);
assertThat(scopeDecorator)
.extracting("fields", InstanceOfAssertFactories.array(SingleCorrelationField[].class))
.hasSize(2);
});
}
@Test
void shouldApplyCorrelationFieldsIfBaggageCorrelationEnabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.correlation.enabled=true",
"management.tracing.baggage.correlation.fields=alpha,bravo").run((context) -> {
ScopeDecorator scopeDecorator = context.getBean(ScopeDecorator.class);
assertThat(scopeDecorator)
.extracting("fields", InstanceOfAssertFactories.array(SingleCorrelationField[].class))
.hasSize(4);
});
}
@Test
void shouldSupplyMdcCorrelationScopeDecoratorIfBaggageCorrelationDisabled() {
this.contextRunner.withPropertyValues("management.tracing.baggage.correlation.enabled=false")
.run((context) -> assertThat(context).hasBean("mdcCorrelationScopeDecoratorBuilder"));
}
@Test
void shouldHave128BitTraceId() {
this.contextRunner.run((context) -> {
Tracing tracing = context.getBean(Tracing.class);
Span span = tracing.tracer().nextSpan();
assertThat(span.context().traceIdString()).hasSize(32);
});
}
@Test
void shouldNotSupportJoinedSpans() {
this.contextRunner.run((context) -> {
Tracing tracing = context.getBean(Tracing.class);
Span parentSpan = tracing.tracer().nextSpan();
Span childSpan = tracing.tracer().joinSpan(parentSpan.context());
assertThat(parentSpan.context().traceIdString()).isEqualTo(childSpan.context().traceIdString());
assertThat(parentSpan.context().spanIdString()).isEqualTo(childSpan.context().parentIdString());
assertThat(parentSpan.context().spanIdString()).isNotEqualTo(childSpan.context().spanIdString());
});
}
@Test
@SuppressWarnings("rawtypes")
void compositeSpanHandlerShouldBeFirstSpanHandler() {
this.contextRunner.withUserConfiguration(SpanHandlerConfiguration.class).run((context) -> {
Tracing tracing = context.getBean(Tracing.class);
assertThat(tracing).extracting("tracer.spanHandler.delegate.handlers")
.asInstanceOf(InstanceOfAssertFactories.array(SpanHandler[].class))
.extracting((handler) -> (Class) handler.getClass())
.containsExactly(CompositeSpanHandler.class, AdditionalSpanHandler.class);
});
}
@Test
void compositeSpanHandlerUsesFilterPredicateAndReportersInOrder() {
this.contextRunner.withUserConfiguration(CompositeSpanHandlerComponentsConfiguration.class).run((context) -> {
CompositeSpanHandlerComponentsConfiguration components = context
.getBean(CompositeSpanHandlerComponentsConfiguration.class);
CompositeSpanHandler composite = context.getBean(CompositeSpanHandler.class);
assertThat(composite).extracting("spanFilters").asList().containsExactly(components.filter1,
components.filter2);
assertThat(composite).extracting("filters").asList().containsExactly(components.predicate2,
components.predicate1);
assertThat(composite).extracting("reporters").asList().containsExactly(components.reporter1,
components.reporter3, components.reporter2);
});
}
@Configuration(proxyBeanMethods = false)
static class CompositeSpanHandlerComponentsConfiguration {
private final SpanFilter filter1 = mock(SpanFilter.class);
private final SpanFilter filter2 = mock(SpanFilter.class);
private final SpanExportingPredicate predicate1 = mock(SpanExportingPredicate.class);
private final SpanExportingPredicate predicate2 = mock(SpanExportingPredicate.class);
private final SpanReporter reporter1 = mock(SpanReporter.class);
private final SpanReporter reporter2 = mock(SpanReporter.class);
private final SpanReporter reporter3 = mock(SpanReporter.class);
@Bean
@Order(1)
SpanFilter filter1() {
return this.filter1;
}
@Bean
@Order(2)
SpanFilter filter2() {
return this.filter2;
}
@Bean
@Order(2)
SpanExportingPredicate predicate1() {
return this.predicate1;
}
@Bean
@Order(1)
SpanExportingPredicate predicate2() {
return this.predicate2;
}
@Bean
@Order(1)
SpanReporter reporter1() {
return this.reporter1;
}
@Bean
@Order(3)
SpanReporter reporter2() {
return this.reporter2;
}
@Bean
@Order(2)
SpanReporter reporter3() {
return this.reporter3;
}
}
@Configuration(proxyBeanMethods = false)
static class SpanHandlerConfiguration {
@Bean
SpanHandler additionalSpanHandler() {
return new AdditionalSpanHandler();
}
static class AdditionalSpanHandler extends SpanHandler {
}
}
@Configuration(proxyBeanMethods = false)
private static class CustomConfiguration {
@Bean
Tracing customTracing() {
return mock(Tracing.class);
}
@Bean
Tracer customTracer() {
return mock(Tracer.class);
}
@Bean
CurrentTraceContext customCurrentTraceContext() {
return mock(CurrentTraceContext.class);
}
@Bean
Factory customFactory() {
return mock(Factory.class);
}
@Bean
Sampler customSampler() {
return mock(Sampler.class);
}
@Bean
io.micrometer.tracing.Tracer customMicrometerTracer() {
return mock(io.micrometer.tracing.Tracer.class);
}
@Bean
BraveBaggageManager customBraveBaggageManager() {
return mock(BraveBaggageManager.class);
}
@Bean
CompositeSpanHandler customCompositeSpanHandler() {
return new CompositeSpanHandler(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
}
@Bean
SpanCustomizer customSpanCustomizer() {
return mock(SpanCustomizer.class);
}
@Bean
io.micrometer.tracing.SpanCustomizer customMicrometerSpanCustomizer() {
return mock(io.micrometer.tracing.SpanCustomizer.class);
}
}
}