Search code examples
spring-micrometermicrometer-tracing

How can I propagate a dynamic header downstream with micrometer tracing?


I have:

  • Spring Boot 3.3.+
  • io.micrometer:micrometer-tracing-bridge-brave

Currently, we can set management.tracing.baggage.remote-fields=f-user and expect that the header f-user will be propagated downstream using RestTemplate, RestClient or WebClient.

How can I set up the framework to propagate any header by regexp f-.*?

update:

One obvious way is to do it with MDC but this one is not thread-safe really, it will require configuration for async tasks

The same question I asked on GitHub


Solution

  • I could find a good way to use micrometer-tracing abstractions and I did like this:

    • I use local management.tracing.baggage.local-fields: combined-field

    • In OncePerRequestFilter and WebFilter (reactive) I set all the headers by the pattern f-.* to combined-field baggage local field, splitting by ;

    • In ClientHttpRequestInterceptor and WebClientCustomizer I take from the baggage local-field combined-field and reverse back to N of headers setting to the downstream HTTP request