admin管理员组文章数量:1415100
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 set the baggage and send a request inside the ApplicationReadyEvent
listener, in a method annotated with @EventListener
.
And 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.
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 set the baggage and send a request inside the ApplicationReadyEvent
listener, in a method annotated with @EventListener
.
And 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.
Share Improve this question edited Mar 18 at 13:36 evandongen 2,06518 silver badges21 bronze badges asked Feb 16 at 16:31 user471011user471011 7,38617 gold badges72 silver badges105 bronze badges 1- The issue specifically relates to where the baggage is set. I hope you don’t mind if I highlight that in your question. – Geba Commented Mar 6 at 21:53
1 Answer
Reset to default 1It 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:
- set the trace level for logs in
io.micrometer.tracing
- observed the following log entry:
Managed to update the baggage on make current [false]
- The value
false
indicates that the baggage was not updated
- The value
本文标签: spring bootBaggage field isn39t propagated from EventListener of ApplicationReadyEventStack Overflow
版权声明:本文标题:spring boot - Baggage field isn't propagated from EventListener of ApplicationReadyEvent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745189482a2646844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论