spring-cloud-stream channel instrumentation with ExtraFieldPropagation
See original GitHub issueHi,
I want to add ExtraFieldPropagation which also should be logged via spring-cloud-stream with Rabbit binders. Now (almost) everything works fine out of the box.
I defined the MDC Data in application.yml logging.pattern.level
I provide my own CustomSlf4jCurrentTraceContext extends CurrentTraceContext which puts my custom Baggage into MDC in its newScope() method and it gets propagated to my downlink µservices.
Only thing left is, that my source is called by a spring scheduler and activated/called by spring-cloud-stream:
@SendTo(SourceChannels.OUTPUT)
public void timerMessageSource() {
ExtraFieldPropagation.set(sleuthTracer.currentSpan().context(), CustomSlf4jCurrentTraceContext.CUSTOM_SLEUTH_BAGGAGE_BUSINESS_PROCESS_NAME, "myCustomBpName");
ExtraFieldPropagation.set(sleuthTracer.currentSpan().context(), CustomSlf4jCurrentTraceContext.CUSTOM_SLEUTH_BAGGAGE_BUSINESS_PROCESS_SUCC, "sink1;somethingElse");
MessageDTO messageDTO = new MessageDTO();
messageDTO.datetime = new Date().toString();
messageDTO.message = "fromSource";
log.info("[{}]Produced: '{}'", instanceIndex, messageDTO);
sourceOutputMessageChannel.send(MessageBuilder.withPayload(messageDTO).build());
}
now at the time I insert my custom baggage, the initial Span has already been started by spring-cloud-stream message instrumentation (or sleuth scheduler support?), and at the point I add my ExtraFieldPropagation it will not get into the currentSpan’s MDC, as the span is already started and my CustomSlf4jCurrentTraceContext has already finished being called by the instrumentation.
Hence propagation is working fine, but my log.info() inside the sender does not contain/log my extra baggage.
Is there a simple way to achieve this?
How about an Annotation which I can put on my spring-cloud-stream @SendTo(SourceChannels.OUTPUT) and @StreamListener(Tier1Channels.INPUT) methods with key/value pairs that are then accessible in its currentTraceContext so that I can do with them anything I want to do, e.g. inside my CustomSlf4jCurrentTraceContext putting them into the MDC so that already spring-cloud-stream adapter log messages for my scheduled method contain the MDC log data I want to be in there.
This way I could act on my custom baggage and alter it for logging at the point in time where spring-cloud-stream does create the new span for me and not afterwards when my business code has to try to fiddle it in backward. Plus on the receiving side, it could alter the custom baggage to be logged with the altered custom values of the receiver already on receiving by spring-cloud-stream.
thanx for feedback.
Issue Analytics
- State:
- Created 5 years ago
- Comments:21 (6 by maintainers)

Top Related StackOverflow Question
While a best solution might not make it for 2.0, we might be able special case ExtraFieldsPropagation meanwhile and achieve the same https://github.com/openzipkin/brave/issues/682
Added here https://github.com/spring-cloud/spring-cloud-sleuth/wiki/Spring-Cloud-Sleuth-3.0-Migration-Guide