title: Spring Sleuth trace/span IDs not available in HttpTraceRepository only if Mono response has body
body: <p>I have logging in an Actuator <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-http-tracing.html" rel="nofollow noreferrer"><code>HttpTraceRepository</code></a> bean which is invoked after a Mono response has been written. If the response has no body (only status) then I get the trace/span IDs in the log message. If however the response has a body, the log messages do not have the trace/span IDs <strong>unless</strong> they are handled by the same NIO thread that handled a "no body" request previously.</p>
  
  <p>Here is a greatly simplified example that exposes the issue.</p>
  
  <pre><code>@SpringBootApplication
  public class DemoApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  
      @Bean
      public WebClient webClient() {
          return WebClient.create();
      }
  }
  
  @Component
  class AccessLoggingHttpTraceRepository implements HttpTraceRepository {
  
      @Override
      public List&lt;HttpTrace&gt; findAll() {
          return null;
      }
  
      @Override
      public void add(HttpTrace trace) {
          LogFactory.getLog("netty.Access").info("ACCESS");
      }
  }
  
  @RestController
  @RequestMapping("/test")
  class PublicationFamilyEndpoint {
  
      @Autowired
      private WebClient webClient;
  
      @GetMapping("/body")
      public Mono&lt;String&gt; testWithBody() {
          return callWebClient();
      }
  
      @GetMapping("/nobody")
      public Mono&lt;ResponseEntity&lt;Object&gt;&gt; testWithoutBody() {
          return callWebClient().map(s -&gt; ResponseEntity.ok().build());
      }
  
      private Mono&lt;String&gt; callWebClient() { // doesn't matter what happens here - this is just an example of a reactive call
          return webClient.get().uri("http://example.com").retrieve().bodyToMono(String.class);
      }
  }
  </code></pre>
  
  <p>When I call </p>
  
  <pre><code>curl -sS 'http://localhost:8080/test/nobody'
  </code></pre>
  
  <p>I get logs</p>
  
  <pre><code>[-,ff016a7a5a9aadbb,ff016a7a5a9aadbb,false] 5741 --- [ctor-http-nio-4] netty.Access: ACCESS
  </code></pre>
  
  <p>When I call </p>
  
  <pre><code>curl -sS 'http://localhost:8080/test/body'
  </code></pre>
  
  <p>I get logs</p>
  
  <pre><code>[-,,,] 5741 --- [ctor-http-nio-1] netty.Access: ACCESS
  </code></pre>
  
  <p>See however this series of request logs. The first two are to the <code>/nobody</code> endpoint and the rest are to the <code>/body</code> endpoint. If the <code>/body</code> request is handled by the same thread as a <code>/nobody</code> request previously, the trace/span IDs are present.</p>
  
  <pre><code>[-,78bbe5342dbcd517,78bbe5342dbcd517,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
  [-,941cc2e3afc9bbc4,941cc2e3afc9bbc4,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-1] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-2] netty.Access: ACCESS
  [-,5728dcc1559f64ae,5728dcc1559f64ae,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-4] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-1] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-2] netty.Access: ACCESS
  [-,00241261eb9931bd,00241261eb9931bd,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
  [-,,,] 7091 --- [ctor-http-nio-4] netty.Access: ACCESS
  </code></pre>
  
  <p>Is this a bug? Can I work around it in some way?</p>
  
  <p>Versions: Spring Boot 2.0.4.RELEASE/2.1.0.BUILD-SNAPSHOT, Spring Cloud Finchley.SR1</p>
  
  <p>Dependencies: spring-boot-starter-actuator, spring-boot-starter-webflux, spring-cloud-starter-sleuth</p>