I already manage to do so in Spring Boot 2.7.x but is a little bit different in Spring Boot 3.
I added the following to initialize the baggage field if there is no related HTTP header
public class FAPIAutoConfiguration {
private static final BaggageField FIELD = BaggageField.create("x-fapi-interaction-id");
@Bean
TracingCustomizer fapiTracingCustomizer(UniqueIdGenerator generator) {
return builder -> builder
.alwaysSampleLocal()
.addSpanHandler(new SpanHandler() {
@Override
public boolean begin(TraceContext context, MutableSpan span, TraceContext parent) {
var value = FIELD.getValue(context);
if (value == null || value.trim().isEmpty()) {
FIELD.updateValue(context, generator.next());
}
return super.begin(context, span, parent);
}
});
}
@Bean
BaggagePropagationCustomizer fapiBaggagePropagationCustomizer() {
return builder -> builder.add(BaggagePropagationConfig.SingleBaggageField.remote(FIELD));
}
}
The problems is that I do not know if this is even the best solution or if .alwaysSampleLocal()
has a negative performance impact. Without .alwaysSampleLocal()
the initialization only happens from time to time.