0

I have the following Spring Cloud Sleuth configuration:

application.yml file:

  sleuth:
    baggage:
      remote-fields: userId
      correlation-fields: userId

Configuration class:

@Configuration
public class SleuthConfiguration {

    @Bean
    public BaggageField userIdBaggageField() {
        return BaggageField.create("userId");
    }

    @Bean
    public CurrentTraceContext.ScopeDecorator mdcScopeDecorator() {
        return MDCScopeDecorator.newBuilder()
            .clear()
            .add(CorrelationScopeConfig.SingleCorrelationField
                .newBuilder(userIdBaggageField())
                .flushOnUpdate()
                .build())
            .build();
    }
}

When updating the BaggageField in the controller, the getValue() method always returns null, although I configured the corresponding BaggageField to flush on update (.flushOnupdate() documentation):

@RestController
@Slf4j
static class SampleController {

    private final BaggageField userIdBaggageField;

    SampleController(BaggageField userIdBaggageField) {
        this.userIdBaggageField = userIdBaggageField;
    }

    @GetMapping("/sample")
    public void periodes(@RequestParam String userId) {
        log.info("Setting BaggageField to {}", userId);
        this.userIdBaggageField.updateValue(userId);
        log.info("BaggageField value {}", this.userIdBaggageField.getValue());
    }
}

Logs:

Setting BaggageField to 1234
BaggageField value null

What did I possibly miss?

Thanks.

Florian Lopes
  • 1,093
  • 1
  • 13
  • 20
  • `updateValue(String value)` method is returning a boolean, apparently it's failing to update the value, maybe because the currentTraceContext is null, try to get the traceContext yourself and use this `this.userIdBaggageField.updateValue(context, value)` , And check the returned boolean of it to make sure the value was updated. – Mohamed Ali RACHID Mar 16 '23 at 12:25
  • Thanks for your answer. After debugging, I found that the issue was caused by a custom propagator. The `updateValue(String value)` method returns false due to this configuration. I will answer my question. – Florian Lopes Mar 17 '23 at 08:07
  • @MohamedAliRACHID I am facing a similar issue where currentTraceContext is always null, any pointers as to why that may be happening and how I can fix it? – Dfarrelly May 09 '23 at 14:43
  • @Dfarrelly Can you show a bit of your code ? that could help to detect the reason. – Mohamed Ali RACHID May 09 '23 at 21:46
  • @MohamedAliRACHID I made another post here https://stackoverflow.com/questions/76226277/brave-baggage-not-working-after-switching-from-spring-cloud-sleuth-to-micrometer if you can take a look – Dfarrelly May 11 '23 at 09:47

1 Answers1

0

The issue was due to a configured custom propagator interfering with default baggage propagation.

Removing it fixes this issue.

Florian Lopes
  • 1,093
  • 1
  • 13
  • 20