spring-cloud-sleuth: Manually set baggage is not automatically cleaned up from MDC

I have configured:

spring.sleuth:
  propagation-keys:
    - x-session-id
  log:
    slf4j:
      whitelisted-mdc-keys: x-session-id 

If I pass this propagation key with a value to my app as HTTP header then after the request is completed it is automatically removed from MDC by Slf4jScopeDecorator.

But if I don’t pass it as header but manually set the value somewhere during processing of the request then I have to manually set it to two places:

ExtraFieldPropagation.set(continuedSpan.context(), "x-session-id", "mySession123");
MDC.put("x-session-id", "mySession123");

and I also have to create an Interceptor and manually wipe it from MDC after the request processing has completed by calling: MDC.remove("my-key")

This is because Slf4jScopeDecorator only wipes any baggage that arrived with the request.

I propose to create a mechanism to register the value with single call (so it gets set in both to ExtraFieldPropagation and to MDC) and also after a value has been registered it would be automatically wiped from MDC after the request has completed.

Related StackOverflow post.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (18 by maintainers)

Commits related to this issue

Most upvoted comments

This feature is not released yet I think.

No. sleuth cannot clear MDC. In current implementation, previousMdc’s key set is consist of whited list keys that have non-null value. But, if you call ExtraFieldPropagation.set(key, value) after TraceWebFilter.filter(), there is no value in ExtraFieldPropagation when Slf4jScopeDecorator decorate current span.

Can you check the pr I’ve created that should solve the problem?