Skip to content

Commit da15179

Browse files
committed
Add support for B3 multi header propagation
- Order propagation extractors by priority (W3C, B3, B3 multi)
1 parent 0f10fcd commit da15179

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ static class BraveNoBaggageConfiguration {
176176
Factory propagationFactory(TracingProperties properties) {
177177
Iterable<Factory> injectorFactories = PropagationFactoryFactory
178178
.factoriesFor(properties.getPropagation().getType());
179-
Iterable<Factory> extractorFactories = PropagationFactoryFactory.factoriesFor(PropagationType.values());
179+
Iterable<Factory> extractorFactories = PropagationFactoryFactory
180+
.factoriesFor(PropagationType.orderedValues());
180181
return new CompositePropagationFactory(injectorFactories, extractorFactories);
181182
}
182183

@@ -199,7 +200,7 @@ BaggagePropagation.FactoryBuilder propagationFactoryBuilder(
199200
Iterable<Factory> injectorFactories = PropagationFactoryFactory.factoriesFor(BRAVE_BAGGAGE_MANAGER,
200201
this.tracingProperties.getPropagation().getType());
201202
Iterable<Factory> extractorFactories = PropagationFactoryFactory.factoriesFor(BRAVE_BAGGAGE_MANAGER,
202-
PropagationType.values());
203+
PropagationType.orderedValues());
203204
Factory delegate = new CompositePropagationFactory(injectorFactories, extractorFactories);
204205
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(delegate);
205206
baggagePropagationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
@@ -258,23 +259,31 @@ ScopeDecorator correlationScopeDecorator(CorrelationScopeDecorator.Builder build
258259
/**
259260
* Factory for {@link Factory propagation factories}.
260261
*/
261-
private static class PropagationFactoryFactory {
262+
private static final class PropagationFactoryFactory {
262263

263264
private PropagationFactoryFactory() {
264265
}
265266

266267
/**
267-
* Creates a new B3 propagation factory.
268-
* @return B3 propagation factory.
268+
* Creates a new B3 propagation factory using a single B3 header.
269+
* @return the B3 propagation factory
269270
*/
270-
private static Factory b3() {
271+
private static Factory b3Single() {
271272
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
272273
}
273274

275+
/**
276+
* Creates a new B3 propagation factory using multiple B3 headers.
277+
* @return the B3 propagation factory
278+
*/
279+
private static Factory b3Multi() {
280+
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
281+
}
282+
274283
/**
275284
* Creates a new W3C propagation factory.
276285
* @param baggageManager baggage manager to use, or {@code null}
277-
* @return W3C Propagation propagation factory
286+
* @return the W3C propagation factory
278287
*/
279288
private static W3CPropagation w3c(BaggageManager baggageManager) {
280289
return (baggageManager != null) ? new W3CPropagation(baggageManager, Collections.emptyList())
@@ -283,25 +292,18 @@ private static W3CPropagation w3c(BaggageManager baggageManager) {
283292

284293
private static Factory forType(BaggageManager baggageManager, PropagationType type) {
285294
return switch (type) {
286-
case B3 -> PropagationFactoryFactory.b3();
295+
case B3 -> PropagationFactoryFactory.b3Single();
296+
case B3_MULTI -> PropagationFactoryFactory.b3Multi();
287297
case W3C -> PropagationFactoryFactory.w3c(baggageManager);
288298
};
289299
}
290300

291301
private static List<Factory> factoriesFor(Collection<PropagationType> types) {
292-
return factoriesFor(null, types.toArray(PropagationType[]::new));
293-
}
294-
295-
private static List<Factory> factoriesFor(PropagationType... types) {
296302
return factoriesFor(null, types);
297303
}
298304

299305
private static List<Factory> factoriesFor(BaggageManager baggageManager, Collection<PropagationType> types) {
300-
return factoriesFor(baggageManager, types.toArray(PropagationType[]::new));
301-
}
302-
303-
private static List<Factory> factoriesFor(BaggageManager baggageManager, PropagationType... types) {
304-
List<Factory> result = new ArrayList<>(types.length);
306+
List<Factory> result = new ArrayList<>(types.size());
305307
for (PropagationType type : types) {
306308
result.add(forType(baggageManager, type));
307309
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositePropagationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) {
6868

6969
@Override
7070
public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
71-
return request -> {
71+
return (request) -> {
7272
for (Propagation<String> extractor : this.extractors) {
7373
TraceContextOrSamplingFlags extract = extractor.extractor(getter).extract(request);
7474
if (extract != TraceContextOrSamplingFlags.EMPTY) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/TracingProperties.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package org.springframework.boot.actuate.autoconfigure.tracing;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Comparator;
2022
import java.util.List;
21-
import java.util.Set;
2223

2324
import org.springframework.boot.context.properties.ConfigurationProperties;
2425

@@ -167,29 +168,54 @@ public void setFields(List<String> fields) {
167168
public static class Propagation {
168169

169170
/**
170-
* Tracing context propagation types.
171+
* Tracing context propagation types produced by the application.
171172
*/
172-
private Set<PropagationType> type = Set.of(PropagationType.W3C);
173+
private List<PropagationType> type = List.of(PropagationType.W3C);
173174

174-
public Set<PropagationType> getType() {
175+
public List<PropagationType> getType() {
175176
return this.type;
176177
}
177178

178-
public void setType(Set<PropagationType> type) {
179+
public void setType(List<PropagationType> type) {
179180
this.type = type;
180181
}
181182

182183
enum PropagationType {
183184

184185
/**
185-
* B3 propagation type.
186+
* <a href="https://github.com/openzipkin/b3-propagation#single-header">B3
187+
* single header</a> propagation.
186188
*/
187-
B3,
189+
B3(1),
188190

189191
/**
190-
* W3C propagation type.
192+
* <a href="https://github.com/openzipkin/b3-propagation#multiple-headers">B3
193+
* multiple headers</a> propagation.
191194
*/
192-
W3C
195+
B3_MULTI(2),
196+
197+
/**
198+
* W3C propagation.
199+
*/
200+
W3C(0);
201+
202+
/**
203+
* Order of the enum value. The {@link #orderedValues()} method returns the
204+
* enum values in ascending order.
205+
*/
206+
private final int order;
207+
208+
PropagationType(int order) {
209+
this.order = order;
210+
}
211+
212+
/**
213+
* Returns the ordered values.
214+
* @return the ordered values
215+
*/
216+
static List<PropagationType> orderedValues() {
217+
return Arrays.stream(values()).sorted(Comparator.comparing((value) -> value.order)).toList();
218+
}
193219

194220
}
195221

0 commit comments

Comments
 (0)