2

I have two microservices using Kafka as a broker for the communications between them and using Spring Cloud Sleuth for tracing and both of them have the same Spring boot version 2.7.2.

I needed to pass extra information (e.g. correlationId) for the Kafka messages but I tried to do so by adding remote baggage and it didn't work.

Example code (Service A):

          Span newSpan = sleuthTracer.nextSpan().start();
          try (SpanInScope spanInScope = sleuthTracer.withSpan(newSpan);) {
            sleuthTracer.createBaggage("correlation-id").set(UUID.randomUUID().toString());
           
            String key = product.getData().getMarketplaceGroupId();
            var listenableFuture = kafkaTemplate.send(topic, key, message);
            futures.add(listenableFuture.completable());
            processThePublishingResults(
                listenableFuture, topic, key, resource);
          } finally {
            newSpan.end();
          }

Example code (Service B):

In the Kafka consumer for the topic produced by Service A


String correlationIdBaggage = sleuthTracer.getBaggage("correlation-id").get();
log.info("Received Sleuth baggage with correlation-id = " + correlationIdBaggage);

Both service contain the configurations below in application.yaml:

spring:
  sleuth:
    traceId128: true
    baggage:
      remote-fields:
        - correlation-id
      tag-fields:
        - correlation-id
      correlation-enabled: true
      correlation-fields:
        - correlation-id

The question is, does this only work with REST and not intended to work with Kafka? Knowing that the continuity of tracing is working fine with Kafka and Sleuth in our logs between the producer and consumer.

Thanks.

Ahmed Ali
  • 105
  • 1
  • 8
  • You got any solution I am also looking for way to propagate custom correlation id in API and event chain say Microservice X has one API which is triggering someEvent received by Microservice Y which again calling some API and so on. I need to propagate correlation id through out. Can we use sleuth baggage for this? – user2425109 Feb 15 '23 at 05:12

0 Answers0