2

We are currently using sleuth 2.2.3.RELEASE, and we couldn't see the field userId passed in http headers are not propagating. Below is our code.

BaggageField REQUEST_ID = BaggageField.create("x-vcap-request-id");
    BaggageField USER_ID = BaggageField.create("userId");
    
    Tracing.newBuilder().propagationFactory(
            BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
            .add(SingleBaggageField.remote(REQUEST_ID))
            .add(SingleBaggageField.newBuilder(USER_ID).addKeyName("baggage-user-id").build())
            .build());

We doubt that some issue in YML file. We tried with all the below options but none is working.

#1 baggage-keys: baggage-user-id 
#2 propagation-keys: baggage-user-id
#3 baggage-keys: user-id 

In logback:

%X{baggage-user-id:-}

We are passing userId as in http header.

user3474541
  • 157
  • 3
  • 13

1 Answers1

5

Please do not create your own instance of tracing. You can create beans that will end up inside the tracing bean.

Here you have an example that uses the latest 3.x api

spring:
 sleuth:
  baggage:
   correlation-fields:
    - TEST-COMMUNICATION-TYPE
   remote-fields:
    - TEST-COMMUNICATION-TYPE

Old, deprecated api

spring:
  application:
    name: service1
  sleuth:
    baggage-keys:
      - baggage
      - key
    log.slf4j.whitelisted-mdc-keys:
      - key

How we retrieve baggage

log.info("Service2: Baggage for [key] is [" + BaggageField.getByName("key") + "]");

How we set the baggage

String baggageKey = "key";
    String baggageValue = "foo";
    BaggageField baggageField = BaggageField.create(baggageKey);
    baggageField.updateValue(baggageValue);
Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Thanks. It worked between 2 rest components(spring boot). So, no code is required in interceptor. But, why these fields are not propagating to Kafka(latest stable version:2.x) and we are not using any spring cloud kafka stream, using simple spring-kafka. Do we need to change any config? – user3474541 Jul 07 '20 at 11:59
  • This might be an issue with kafka instrumentation maybe. Can you file an issue with preferably a sample that replicates yhe problem? – Marcin Grzejszczak Jul 07 '20 at 12:00
  • 1
    exactly this answer provided _the_ solution for me after hours of fiddling around. – sschrass Feb 16 '21 at 14:22
  • @MarcinGrzejszczak but adding baggage like this doesn't make it available for logging after adding the key in log4j pattern, my guess is its not getting added to MDC (Im using log4j2) Do you have any idea about this? – pacman Feb 25 '22 at 07:46
  • @user3474541 where you able to figureout how to solve issue with kafka – pacman Feb 25 '22 at 07:47