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.

TraceId does not propagate between threads

See original GitHub issue

Describe the bug TraceId does not propagate between threads since version 2.2.2.RELESE of sleuth (Spring Cloud Hoxton.SR3) I’ve tested with verison 2.2.1.RELEASE (Hoxton.SR2) - works correctly On newest version - 2.2.5.RELEASE issue still persists.

Sample Here’s test Controller:

` package app.testing;

import brave.Span;
import brave.Tracer;
import brave.propagation.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.Optional;

@RestController
public class TraceIdController {
    private Tracer tracer;

    @Autowired
    public TraceIdController(Tracer tracer) {
        this.tracer = tracer;
    }

    @GetMapping(value = "/api/v1/traceId/test")
    public Mono<TraceIdDto> get() {
        String traceId = getTraceId().orElse(null);
        return Mono.defer(() -> Mono.just(getTraceId()))
                .subscribeOn(Schedulers.elastic())
                .map(elasticThreadId -> new TraceIdDto(traceId, elasticThreadId.orElse(null)));
    }

    private Optional<String> getTraceId() {
        return Optional.of(tracer)
                .map(Tracer::currentSpan)
                .map(Span::context)
                .map(TraceContext::traceIdString);
    }

}

package app.testing;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class TraceIdDto {
    private String nativeThreadTraceId;
    private String elasticThreadTraceId;
}

`

And junit test that shows the problem: ` package app.testing;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TraceIdControllerTest {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void testTraceId() {

        TraceIdDto traceIdDto = webTestClient.get().uri("/api/v1/traceId/test")
                .exchange().expectStatus().isOk()
                .expectBody(TraceIdDto.class)
                .returnResult().getResponseBody();

        assertThat(traceIdDto.getNativeThreadTraceId()).isEqualTo(traceIdDto.getElasticThreadTraceId());
    }

}

`

Proposed solution I’ve investiagted sleuth code a little bit, and I think the problem is in SleuthContextListener. I’ve compered this class with ContextRefreshedListener that existed in 2.2.1 version. It seems like SleuthContextListener sets refreshed and closed flags to false, when appropriate event comes, instead to true. ... listener.refreshed.compareAndSet(false, event instanceof ContextRefreshedEvent); listener.closed.compareAndSet(false, event instanceof ContextClosedEvent); ...

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
jacekmgcommented, Dec 3, 2020

@marcingrzejszczak here you go: https://github.com/jacekmg/sleuth-trace-id-propagation-test Let me know if that’s sufficient for you.

1reaction
jacekmgcommented, Feb 22, 2021

@vinodnholkar I didn’t find any new solutions, besides manual scope update that @marcingrzejszczak proposed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Spring Cloud Sleuth v2.2.3 how to propagate TraceId to ...
Use LazyTraceExecutor . This class propagates traceIds to new threads and create new spanIds in the process. For more details see.
Read more >
spring-cloud/spring-cloud-sleuth - Gitter
I am using sleuth to inject values into MDC using extrafield propagation. I added this code in a WebFilter. Filter is getting called...
Read more >
Spring Cloud Sleuth
Trace Info propagation. Each color of a note signifies a span (7 spans - from A to G). If you have such information...
Read more >
Easy Distributed Tracing with Spring Cloud Sleuth
There is no single log file, with a request spread across ... A trace ID is the unique identifier that an entire request...
Read more >
Spring Cloud Sleuth - Single Application - Baeldung
This class comes from the Sleuth library and is a special kind of executor that will propagate our traceIds to new threads and...
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