I have the following demo application Spring Boot 3.4.2:
@SpringBootApplication
@RestController
public class BaggageTestApplication {
public static void main(String[] args) {
SpringApplication.run(BaggageTestApplication.class, args);
}
private static final Log LOGGER = LogFactory.getLog(BaggageTestApplication.class);
private final WebClient webClient;
private final Tracer tracer;
public BaggageTestApplication(WebClient webClient, Tracer tracer) {
this.webClient = webClient;
this.tracer = tracer;
}
@RequestMapping("/")
ResponseEntity<Void> home(@RequestHeader Map<String, String> headers) {
headers.forEach((key, value) -> LOGGER.info(key + ": " + value));
return ResponseEntity.ok().build();
}
@EventListener
public void onApplicationEvent(ApplicationReadyEvent event) {
try (BaggageInScope scope = this.tracer.createBaggageInScope("baggage1", "value1")) {
webClient.get()
.retrieve()
.toBodilessEntity()
.block();
}
}
}
@Configuration
class WebclientConfiguration {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder.
baseUrl("http://localhost:8080")
.build();
}
}
and application.yml
:
spring:
application:
name: baggage-test
management:
tracing:
sampling:
probability: 1.0
baggage:
remote-fields: baggage1
enabled: true
enabled: true
I expected the new custom header, baggage1
, to be automatically added during the webClient
request, but only the following were available:
accept-encoding: gzip
user-agent: ReactorNetty/1.2.2
host: localhost:8080
accept: */*
traceparent: 00-67b20fe3ed21c20685f6746f77ef8e74-85f6746f77ef8e74-01
content-length: 0
For some reason, the baggage field wasn't propagated at all.
It happens because traceContext
is null inside onApplicationEvent
.
traceContext
is set by default if you make this call from another endpoint.
So if you call /call-home
, then headers will be sent as you expect:
@RequestMapping("/call-home")
void callHome() {
try (BaggageInScope scope = this.tracer.createBaggageInScope("baggage1", "value1")) {
webClient.get()
.retrieve()
.toBodilessEntity()
.block();
}
}
Or if you want to make a call from onApplicationEvent
, then I suggest following in order to send headers as you expect:
@EventListener
public void onApplicationEvent(ApplicationReadyEvent event) {
// it will set traceContext
Span span = tracer.startScopedSpan("on-application-event")
try (BaggageInScope scope = this.tracer.createBaggageInScope("baggage1", "value1")) {
webClient.get()
.retrieve()
.toBodilessEntity()
.block();
}
span.end()
}
How I diagnosed the problem:
io.micrometer.tracing
Managed to update the baggage on make current [false]
false
indicates that the baggage was not updated