tracing: CurrentTraceContext.context()t is null in downstream

I have a code like this and injected CurrentTraceContext from Micrometer Tracing:

        dataSourceClient.retrieveCount(categoryConfig.getCountEndpoint()) <-- this invokes WebClient from Reactor
                .map(CountResponseDTO::getTotalSize) <- here currentTraceContext.context() exists
                .flatMap(count -> createIndex(config, timestamp, count) <- starting from here it returns a `null`
                        .map(indexName -> SyncContext.builder()
                                .config(config)
                               .build()))
                .flatMap(this::publishMessages)
                .flatMap(this::doSomethingElse)
                .subscribe();

Config and dependencies:

@Configuration
public class TracingConfig {

    @Bean
    public CurrentTraceContext currentTraceContext(brave.propagation.CurrentTraceContext currentTraceContext) {
        return new BraveCurrentTraceContext(currentTraceContext);
    }

    @Bean
    public Tracer tracer(CurrentTraceContext currentTraceContext, Tracing tracing) {
        brave.Tracer braveTracer = tracing.tracer();
        BraveTracer braveTracer1 = new BraveTracer(braveTracer, currentTraceContext, new BraveBaggageManager());
        ContextRegistry.getInstance().registerThreadLocalAccessor(new ObservationAwareSpanThreadLocalAccessor(braveTracer1));
        return braveTracer1;
    }

}
       <dependency>
                <groupId>io.projectreactor</groupId>
                <artifactId>reactor-bom</artifactId>
                <version>2022.0.8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.projectreactor</groupId>
                <artifactId>reactor-core-micrometer</artifactId>
                <version>1.0.7</version>
            </dependency>
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-tracing-bom</artifactId>
                <version>1.1.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-tracing-bridge-brave</artifactId>
                <version>1.1.2</version>
            </dependency>

We use Spring Boot 3.1.1 with Reactor framework.

Any idea why currentTraceContext.context() is null in the first flatMap operator?

From what I can see on debug the Observation is closed after first http call, but I don’t know if this is normal behaviour and I should still expect context available.

Thanks for help!

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

That’s what I expected 😃 I see two options:

  • consider Spring Data ElasticSearch (ReactiveElasticsearchClient)

  • change the below code in ReactiveIndicesClient#createIndex:

      // Mono.fromFuture(indicesClient.create(request)) ->
      Mono.fromFuture(indicesClient.create(request)).contextWrite(Function.identity())