Search code examples
springspring-cloud-sleuthzipkin

Spring Sleuth Zipkin Extra Field Propagation


I am new to distributed logging and I need help around the propagation of extra fields across Http Request and Messaging Request. Currently, I am able to propagate the traceId and spanId, but I need to pass correlationId to be propagated across all the microservices.

spring:
   sleuth:
      correlation-fields:
       - x-correlation-id
      remote-fields:
      - x-correlation-id

logback.xml

%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p [${appName},%X{traceId:-},%X{parentId:-},%X{spanId:-},%X{correlation-id:-}]) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%t]){faint}  %clr(%logger{20}:%line){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}

I am a bit curious about how to pass the correlation id to other services.

In case the message starts from Service A - 
Service A (Message Started,CorrelationID-123) -> ServiceB (CorrelationID-123) -> ServiceC(CorrelationID-123)
In case if it started with Service B
Service B (Message Started,CorrelationID-123) -> ServiceA (CorrelationID-123) -> ServiceC(CorrelationID-123)

  1. How the correlation id will be passed to Kafka messages?
  2. How the correlation id will be passed to Http requests?
  3. Is it possible to use existing tracedId from other service?

Solution

  • I think what you call correlationId is in fact the traceId, if you are new to distributed tracing, I highly recommend reading the docs of spring-cloud-sleuth, the introduction section will give you a basic understanding while the propagation will tell you well, how your fields are propagated across services.
    I also recommend this talk: Distributed Tracing: Latency Analysis for Your Microservices - Grzejszczak, Krishna.

    To answer your exact questions:

    How the correlation id will be passed to Kafka messages?

    Kafka has headers, I assume the fields are propagated through Kafka headers.

    How the correlation id will be passed to Http requests?

    Through HTTP Headers.

    Is it possible to use existing tracedId from other service?

    Not just possible, Sleuth does this for you out of the box. If there is a traceId in the incoming request/message/event/etc. Sleuth will not create a new one but it will use it (see the docs I linked above).