spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.5k stars 40.53k forks source link

Context not propagated in a reactive programming, OpenTelemetry context is not available #42031

Closed tadlakha9 closed 2 weeks ago

tadlakha9 commented 2 weeks ago

We are facing an issue with spring boot 3.3.1, TraceContext is not propagated while calling one microservice with another via retrofit.

java.lang.NullPointerException: Cannot invoke "io.micrometer.tracing.Span.context()" because the return value of "io.micrometer.tracing.Tracer.currentSpan()" is null

Our Observation

When we call an api in microservice 1, tracer.currentSpan().context() is available after calling another microservice via retrofit, TraceContext would not get propagated and a new context with new traceId would get started.

Sample projects

philwebb commented 2 weeks ago

Spring Boot doesn't provide any integration with Retrofit and it looks like this might be a general issue rather than a Spring Boot bug. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

I would suggest posting a question on Stack Overflow or asking in the Micrometer Slack. I'm afraid we don't really have the expertise to help you here.

Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

tadlakha9 commented 2 weeks ago

Its not more specific to Retrofit, actually when we call an api, we get the tracer with currentSpan later currentSpan got lost in between. We have similar issue in the past as well.

philwebb commented 2 weeks ago

This is not a Spring Boot bug IMO. You have no micrometer integration with the OKHttp3 client being used by Retrofit. I think you need something like this in your RetrofitConfig:

    @Bean
    public Retrofit retrofit(ObservationRegistry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(OkHttpObservationInterceptor.builder(registry, "test").build())
                .addInterceptor(tracingInterceptor)
                .build();

       ....
jonatan-ivanov commented 2 weeks ago

Sorry for being a bit late but we have a working Retrofit with OKHttp sample you might be able to use to create your own configuration.