spring-cloud-sleuth-otel icon indicating copy to clipboard operation
spring-cloud-sleuth-otel copied to clipboard

Unable to add multiple baggage fields in HttpRequestParser

Open jarlehansen opened this issue 3 years ago • 7 comments

Describe the bug When calling tracer.createBaggage multiple times in HttpRequestParser, only the last field is added. The previously added fields are gone.

Sample https://github.com/jarlehansen/spring-sleuth-baggage

HttpRequestParser

@Bean(HttpServerRequestParser.NAME)
    fun sleuthHttpServerRequestParser(tracer: Tracer): HttpRequestParser {
        return HttpRequestParser { _, _, _ ->
            tracer.createBaggage("1", "1")
            tracer.createBaggage("2", "2")
            tracer.createBaggage("3", "3")
        }
    }

RestController

@GetMapping
    fun get() = tracer.allBaggage

Response

{"3":"3"}

In the TestController-file this problem is shown. Run the application and go to http://localhost:8080, it will print all added baggage fields. Even though 3 fields are added, only the last is returned from the endpoint.

jarlehansen avatar Oct 25 '22 10:10 jarlehansen

Thanks! Can you please rewrite the samples to Java?

marcingrzejszczak avatar Oct 25 '22 11:10 marcingrzejszczak

Here is the java example: https://github.com/jarlehansen/spring-sleuth-baggage/tree/java

jarlehansen avatar Oct 25 '22 11:10 jarlehansen

Any updates on this?

jarlehansen avatar Nov 04 '22 12:11 jarlehansen

I will look into that asap, currently didn't have time

marcingrzejszczak avatar Nov 04 '22 14:11 marcingrzejszczak

I did some debugging on this, and it seems like when the code is running inside the HttpRequestParser the span is invalid (traceid 0000000...). And therefore is the baggage not added. No idea why the span is invalid though.

Could it be that the HttpRequestParser is loaded before the span is started?

jarlehansen avatar Nov 10 '22 13:11 jarlehansen

This works sort of: tracer.createBaggage(...).set(context, ...);

Where the context is added to the set method. However, I now get this exception: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.sleuth.http.HttpServerRequest.header(String)" because "httpServerRequest" is null

For some reason it is always on the content-length header, not sure if that is a coincidence or not?

jarlehansen avatar Nov 11 '22 13:11 jarlehansen

I have updated the java example here with the same issue, and also here it happens for the content-length header. https://github.com/jarlehansen/spring-sleuth-baggage/tree/java-nullpointer

jarlehansen avatar Nov 14 '22 08:11 jarlehansen

Sleuth reached the end of its OSS support timeline. We encourage to migrate to Micrometer Tracing.

jonatan-ivanov avatar Jul 12 '24 18:07 jonatan-ivanov