Spring-cloud-sleuth: Add Propagated Properties to MDC for logging

Created on 20 Feb 2018  Â·  17Comments  Â·  Source: spring-cloud/spring-cloud-sleuth

Currently, I would like to log the properties added to the configuration option spring.sleuth.propagation-keys in all my logs, like this:

Current
service1.log:2016-02-26 11:15:47.561 INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true]

After Change
service1.log:2016-02-26 11:15:47.561 INFO [service1,2485ec27856c56f4,2485ec27856c56f4,true, MyPropagatedProperty]

From my initial investigation, I've found that this would require a change to Slf4jCurrentTraceContext to include custom propagated properties in the MDC, but this class is currently final, and also a change to the auto-config class TraceEnvironmentPostProcessor to include the properties in the log pattern.

I think this can also be a configuration field of whether to log the propagated properties or not. Probably spring.sleuth.log-propagation-keys which can be either true or false.

I am not sure if there is a better way to do this or not right now.

question

Most helpful comment

OTOH it's a maintainability cost for us to open classes for an extension. You are the first one to request this, if there are more people asking for the same requirement then we might consider opening the class. Until then you'll have to make a copy and alter it to your needs.

All 17 comments

You're partly right. You don't have to change the TraceEnvironmentPostProcessor. It's enough to pass a custom logging.pattern.level property.

Yes Slf4jCurrentTraceContext is final. You can copy the class and alter it as you want, or you can work via delegation. It's up to you. Just remember to register it as a bean.

I am just concerned about the maintainability costs which could arise from copying this class, would that make much of an issue?

OTOH it's a maintainability cost for us to open classes for an extension. You are the first one to request this, if there are more people asking for the same requirement then we might consider opening the class. Until then you'll have to make a copy and alter it to your needs.

Got it then

I would be interested in this feature as well. Currently I'm using the same approach of copying Slf4jCurrentTraceContext.

Can someone share their code? It will be helpful should this feature get
more interest.

On 27 Feb 2018 9:09 pm, "Joke de Buhr" notifications@github.com wrote:

I would be interested in this feature as well. Currently I'm using the
same approach of copying Slf4jCurrentTraceContext.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/spring-cloud/spring-cloud-sleuth/issues/864#issuecomment-368867942,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAD613YQmKpizYWL6h_ECpTs4gmxnKMoks5tY_6hgaJpZM4SLVOq
.

I am also interested in this feature. We need custom fields in the logs. So please open it. As of now, I am gonna copy the class and use it.

Same here. Need to customize log output

thanks for the replies.. they aren't unheard. just there's no cycles quite
yet

PS

I'm working on zipkin UI to work with v2 read endpoints which is needed to
complete half-completed transition work before starting some new thing
https://github.com/openzipkin/zipkin/issues/2047

Kudos and faster completion on this to those who help clear work in front
of me :D

Hi, i need this feature too. I copy the class and configured a @Configuration:

@Configuration
@AutoConfigureBefore({SleuthLogAutoConfiguration.class, TraceAutoConfiguration.class})

...
 @Bean
  public CurrentTraceContext customSlf4jSpanLogger() {
    return CustomSlf4jCurrentTraceContext.create();
  }

But i get this error:
Parameter 2 of method tracing in org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration required a single bean, but 2 were found:
Anyone else run in this problem? (WebFlux, Reactive, Sleuth 2.0.0.RELEASE)

Thank you

Can you provide more code?

Thank you for your fast response - tomorrow I will setup a repo and write again :).

Hi, have we implemented this? Or do we still have to copy and modify Slf4jCurrentTraceContext?

In order to automatically set the baggage values to Slf4j’s MDC, you have to set the spring.sleuth.log.slf4j.whitelisted-mdc-keys property with a list of whitelisted baggage keys. E.g. spring.sleuth.log.slf4j.whitelisted-mdc-keys=foo will set the value of the foo baggage into MDC.

Hi @marcingrzejszczak , thanks for that. Do we have an approximate idea of when it might be in a release?

Was this page helpful?
0 / 5 - 0 ratings