question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Headers not always propagated to MDC (with sample)

See original GitHub issue

I want to read header values, and have them available on MDC. One single request works, but not when a test does many requests after each other.

Complete project that reproduces the error every time (on my computer, Java 17). Most often after only 6-7 iterations:

https://github.com/anderius/spring-sleuth-problem-demo

Excerpt from the sample:

@RestController
@SpringBootApplication
class DemoApplication {

    @Bean
    fun sleuthHttpServerRequestParser() = HttpRequestParser { request, context, _ ->
        request.header("customHeader")
            ?.let { BaggageField.create("customField").updateValue(context, it) }
    }

    @Bean
    fun spanHandler(): SpanHandler = SpanHandler.NOOP

    @GetMapping("/mdc")
    fun getMdc(): Map<String, String> = MDC.getCopyOfContextMap()
}

This issue might be related to https://github.com/spring-cloud/spring-cloud-sleuth/issues/2064.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
anderiuscommented, Jan 17, 2022

@marcingrzejszczak Thank you for looking into this!

That means Spring Cloud Sleuth should not be used for any headers other than the ones used for sampling/tracing, unless we sample everything.

I had the idea the sampling was only how much was sent to zipkin (or similar), but it is also how much is instrumentated.

1reaction
anderiuscommented, Jan 13, 2022

@marcingrzejszczak Maven and Java is ready! 😃

Project is same place: https://github.com/anderius/spring-sleuth-problem-demo

The setup in java:

@RestController
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    HttpRequestParser sleuthHttpServerRequestParser() {
        return (request, context, span) -> {
            String headerValue = request.header("customHeader");
            if (headerValue != null) {
                BaggageField.create("customField").updateValue(context, headerValue);
            }
        };
    }

    @Bean
    SpanHandler spanHandler() {
        return SpanHandler.NOOP;
    }

    @GetMapping("/mdc")
    Map<String, String> getMdc() {
        return MDC.getCopyOfContextMap();
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

WebClient MDC data not propagated · Issue #4089 - GitHub
A workaround for this is to manually propagate the MDC data using context. E.g. In your filter do something like this: String ws...
Read more >
How does Spring Cloud Sleuth propagate the MDC context in ...
I need to do something similar to with other parars from some request headers so I created that puts it into the MDC...
Read more >
5. Propagation - Spring Cloud
The most common propagation approach is to copy a trace context from a client by sending an RPC request to a server receiving...
Read more >
Passing HTTP headers to another microservice and put them ...
In a microservice architecture, it is common to pass a custom HTTP header, let's say: “Correlation-ID”, from one service to another.
Read more >
Context Propagation in Quarkus
Many Quarkus extensions require those contextual objects to operate properly: RESTEasy Reactive, ArC and Transaction for example.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found