-
Notifications
You must be signed in to change notification settings - Fork 781
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
Comments
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 Current:
Desired:
So, I would vote for removing all the legacy keys in the 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? |
Is there some relatively easy way to remove duplicated legacy key-values pairs from MDC? |
@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 package org.springframework.cloud.sleuth.log;
import brave.internal.Nullable;
import brave.propagation.TraceContext;
import org.slf4j.MDC;
import org.springframework.cloud.sleuth.autoconfig.SleuthProperties;
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(
"X-Span-Export",
"X-B3-ParentSpanId",
"X-B3-TraceId",
"X-B3-SpanId",
"spanExportable"
));
private final ScopeDecorator decorator;
public CustomSlf4ScopeDecorator(SleuthProperties sleuthProperties, SleuthSlf4jProperties sleuthSlf4jProperties) {
this.decorator = new Slf4jScopeDecorator(sleuthProperties, sleuthSlf4jProperties);
}
@Override
public Scope decorateScope(@Nullable TraceContext currentSpan, Scope scope) {
Scope decoratedScope = decorator.decorateScope(currentSpan, scope);
UNDESIRED_MDC_KEYS.forEach(MDC::remove);
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: @Configuration
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true)
@AutoConfigureBefore(TraceAutoConfiguration.class)
public class CustomSleuthLogAutoConfiguration {
/**
* Configuration for Slf4j.
*/
@Configuration
@ConditionalOnClass(MDC.class)
@EnableConfigurationProperties(SleuthSlf4jProperties.class)
public static class CustomSlf4jConfiguration {
@Bean
@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 {
@Override
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): org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yourcompany...CustomSleuthLogAutoConfiguration |
You can create sth like |
Thanks for help @marcingrzejszczak @odin-delrio |
@szczepanskikrs |
We will remove the legacy MDC entries and message headers in the Ilford (Sleuth 3.0) release |
This is partially done by @adriancole since all Sleuth code related to MDC got removed and migrated to using Brave natively. |
No description provided.
The text was updated successfully, but these errors were encountered: