0

I have a Parent that calls another microservice (child) with webclient.

public Mono<Product> productFromIntegrator() {

        String url = "http://localhost:8282/product";

    return Mono.deferContextual(contextView -> {
        try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
                ObservationThreadLocalAccessor.KEY)) {
            LOG.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
            LOG.info("Service Called");
            return webClient.get().uri(url)
                    .retrieve().bodyToMono(Product.class).log(LOG.getName(), Level.FINE)
                    .onErrorMap(WebClientException.class, ex -> {
                        LOG.warn("Error retrieving Product");
                        return ex;
                    });
        }
    });
}

Parent Trace Id:

RestController from Child:

 @Autowired
private final Service service;
private final Tracer tracer;

public Controller(Service service, Tracer tracer) {
    this.service = service;
    this.tracer = tracer;
}

@GetMapping(value = "/product")
public Mono<Product> getProduct() {

    return Mono.deferContextual(contextView -> {
        try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
                ObservationThreadLocalAccessor.KEY)) {
            LOG.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
            LOG.info("Service Called");
            return this.service.getProduct();
        }
    });
}

Child Trace

The Child is not getting the TraceId from Parent, it is creating a new Obsevation Object. Any suggestion/example on how to propagate the TraceId from Parent to Child?

msuzuki
  • 105
  • 2
  • 15
  • Google is your friend: https://tanzu.vmware.com/developer/guides/observability-reactive-spring-boot-3/#reactive-stream-observation - As you kind of hint, the trace id has to be propagated. – Augusto Feb 22 '23 at 19:36

2 Answers2

2

Add this dependency:

        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-micrometer</artifactId>
        </dependency>
0

Found it: How to get trace-id in spring-cloud-gateway using micrometer-brave

Added Hooks in the psvm.

import reactor.core.publisher.Hooks;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        Hooks.enableContextLossTracking();
        SpringApplication.run(DemoApplication.class, args);
    }
}
msuzuki
  • 105
  • 2
  • 15