3

referring to quesition/answer in How to log MDC with Spring Sleuth? I think this has/will change(d) with spring-cloud 2.0 as there is no SpanLogger or Slf4jSpanLogger anymore (or I don't find it)

Wouldn't it be nice if application properties spring.sleuth.baggage-keys and spring.sleuth.propagation-keys if set would also be put in MDC I think inside Slf4jCurrentTraceContext (as this class is currently final I cannot subclass it)

If not, how could I achieve this with spring-cloud 2.0 accordingly?

Dirk Hoffmann
  • 1,444
  • 17
  • 35

2 Answers2

4

We don't want to put all entries in MDC (that really doesn't make a lot of sense). You can however either copy the Slf4jCurrentTraceContext and extend it in the way you want to (and register it as a bean) or maybe create your own implementation of CurrentTraceContext that would wrap the existing CurrentTraceContext via a Bean Post Processor and perform additional logic. I guess the first option is more preferable.

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
3

In version 2.1.0, Slf4jScopeDecorator was introduced and it will automatically add baggage values to MDC as long as they are whitelisted in the spring.sleuth.log.slf4j.whitelisted-mdc-keys configuration.

For example, if you have the following configuration:

spring.sleuth.baggage-keys=key1,key2
spring.sleuth.log.slf4j.whitelisted-mdc-keys=key2

Only the value of key2 will be automatically added MDC, but not the value of key1.

For more info, see: https://cloud.spring.io/spring-cloud-sleuth/reference/html/#prefixed-fields

Luis Rocha
  • 1,369
  • 10
  • 7