最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Baggage field isn't propagated from EventListener of ApplicationReadyEvent - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

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:

  • 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
发布评论

评论列表(0)

  1. 暂无评论