Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the legacy MDC entries and remain with the Brave MDC ones only #1221

marcingrzejszczak opened this issue Feb 26, 2019 · 8 comments


Copy link

No description provided.

Copy link

odin-delrio commented Jun 27, 2019

Hi! I'm suffering the noise of having the legacy keys added in the MDC and I would like to keep my log4j pattern printing everything in the MDC by using%X.


2019-06-27 19:36:11,774 INFO {X-B3-SpanId=e30b6a75bcff782b, X-B3-TraceId=e30b6a75bcff782b, X-Span-Export=false, spanExportable=false, spanId=e30b6a75bcff782b, traceId=e30b6a75bcff782b} some log!


2019-06-27 19:36:11,774 INFO {spanExportable=false, spanId=e30b6a75bcff782b, traceId=e30b6a75bcff782b} some log!

So, I would vote for removing all the legacy keys in the Slf4jScopeDecorator class.

As this can be a breaking change, what do you think about adding a config key for optionally avoid adding these legacy keys to the MDC?
If you think this is a good idea I can make a PR ;)

Copy link

Is there some relatively easy way to remove duplicated legacy key-values pairs from MDC?

Copy link

@szczepanskikrs Not that easy, but I solved this in the following way and add it to an autoconfiguration library so I solved it for several projects.

First I created a class implementing the ScopeDecorator interface, but the trick is to do it using the original package, why? Because this class wraps the original spring scope decorator, and since this one has a "package protected" constructor, there is no other way to instantiate it, so:


import brave.internal.Nullable;
import brave.propagation.TraceContext;
import org.slf4j.MDC;

import java.util.HashSet;
import java.util.Set;

import static brave.propagation.CurrentTraceContext.Scope;
import static brave.propagation.CurrentTraceContext.ScopeDecorator;
import static java.util.Arrays.asList;

public class CustomSlf4ScopeDecorator implements ScopeDecorator {

    public static final Set<String> UNDESIRED_MDC_KEYS = new HashSet<>(asList(

    private final ScopeDecorator decorator;

    public CustomSlf4ScopeDecorator(SleuthProperties sleuthProperties, SleuthSlf4jProperties sleuthSlf4jProperties) {
        this.decorator = new Slf4jScopeDecorator(sleuthProperties, sleuthSlf4jProperties);

    public Scope decorateScope(@Nullable TraceContext currentSpan, Scope scope) {
        Scope decoratedScope = decorator.decorateScope(currentSpan, scope);

        return decoratedScope;

then you need to instruct spring to use that scope decorator instead of the original one, so you need to first add this configuration class:

@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
public class CustomSleuthLogAutoConfiguration {

     * Configuration for Slf4j.
    public static class CustomSlf4jConfiguration {

        @ConditionalOnProperty(value = "spring.sleuth.log.slf4j.enabled", matchIfMissing = true)
        public CurrentTraceContext.ScopeDecorator slf4jSpanDecorator(
                SleuthProperties sleuthProperties,
                SleuthSlf4jProperties sleuthSlf4jProperties
        ) {
            return new CustomSlf4ScopeDecorator(sleuthProperties, sleuthSlf4jProperties);

and then instruct spring to not load the original autoconfig class, so you need to register this filter:

public class SleuthLogAutoConfigurationExclusionFilter implements AutoConfigurationImportFilter {

    public boolean[] match(String[] classNames, AutoConfigurationMetadata metadata) {
        boolean[] matches = new boolean[classNames.length];

        for (int i = 0; i < classNames.length; i++) {
            matches[i] = !SleuthLogAutoConfiguration.class.getName().equals(classNames[i]);

        return matches;

For registering this filter, I'm doing it through a spring.factories file located in "resources/META-INF/spring.factories", with this content (maybe there is other way for registering it):


Copy link
Contributor Author

You can create sth like Slf4jScopeDecorator that will do the cleaning

Copy link

Thanks for help @marcingrzejszczak @odin-delrio

Copy link

Will this feature be available in the next release of sleuth?

Copy link
Contributor Author

We will remove the legacy MDC entries and message headers in the Ilford (Sleuth 3.0) release

Copy link
Contributor Author

This is partially done by @adriancole since all Sleuth code related to MDC got removed and migrated to using Brave natively.

@marcingrzejszczak marcingrzejszczak added this to To do in 2020.0.0-M4 via automation Aug 6, 2020
2020.0.0-M4 automation moved this from To do to Done Aug 6, 2020
@marcingrzejszczak marcingrzejszczak added this to the 3.0.0-M4 milestone Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
No open projects

No branches or pull requests

4 participants