Search code examples
springspring-bootspring-cloudspring-cloud-sleuth

Spring Cloud Sleuth different trace-ID integrate with Kafka


I'm using Kafka for Asyng calls between microservices, and i'm using Spring Sleuth for logging. The logging is ok, but when there is a message from Microservice1 to Microservice2, the logging's messages have different Trace-ID. Don't they have to have the same trace-Id but a different SpanId? is there any special configuration?


Solution

  • Message headers by default will not be transported by Spring Cloud Kafka binder, you have to set it via spring.cloud.stream.kafka.binder.headers manually as described in the Spring Cloud Stream Reference Guide. And then check if those tracing related headers been sent properly.

    You can set Zipkin headers as following in your application.yml:

    spring:
      cloud:
        stream:
          kafka:
            binder:
              headers:
                - X-B3-TraceId
                - X-B3-SpanId
                - X-B3-Sampled
                - X-B3-ParentSpanId
                - X-Span-Name
                - X-Span-Export
    

    Or in your application.properties:

    spring.cloud.stream.kafka.binder.headers[0]=X-B3-TraceId
    spring.cloud.stream.kafka.binder.headers[1]=X-B3-SpanId
    spring.cloud.stream.kafka.binder.headers[2]=B3-Sampled
    spring.cloud.stream.kafka.binder.headers[3]=X-B3-ParentSpanId
    spring.cloud.stream.kafka.binder.headers[4]=X-Span-Name
    spring.cloud.stream.kafka.binder.headers[5]=X-Span-Export
    

    Or in a comma-separated list:

    spring.cloud.stream.kafka.binder.headers=X-B3-TraceId,X-B3-SpanId,B3-Sampled,\
        X-B3-ParentSpanId,X-Span-Name,X-Span-Export