MDC propagation across Scala Futures

621 views
Skip to first unread message

jsw...@ebay.com

unread,
Feb 1, 2018, 5:09:11 PM2/1/18
to kamon-user
Is there documentation on how to setup MDC propagation across Scala Futures (specifically, in a Play 2.6 app)?

I wish to add data to the MDC that is output via logback config.

Is Kamon appropriate for this use case?

thanks
jeff

Ivan Topolnjak

unread,
Feb 1, 2018, 6:13:03 PM2/1/18
to kamon...@googlegroups.com
Hey Jeff,

Please help me understand what you want to achieve.. do you want to have some key/value pairs (typically something like a user id, correlation id or some other request-specific data) to be propagated across Future transformations and then be able to include them in your log patterns, ensuring the right value will be there regardless of the thread where it happens executing, right? 

If that's the case, yes, Kamon has instrumentation for that! Just the solution is not through MDC, instead of that we use our own Context instance and you can at any moment pull values from the current context into your logs, we even have instrumentation to keep this when using the async appender :). As a example, this is how we can add a trace ID to the logs using a logback converter: https://github.com/kamon-io/kamon-logback/blob/master/src/main/scala/kamon/logback/LogbackTraceIDConverter.scala

Let me know if your expectations match the above and I'll put together a example :)







--
You received this message because you are subscribed to the Google Groups "kamon-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kamon-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jsw...@ebay.com

unread,
Feb 1, 2018, 7:48:38 PM2/1/18
to kamon-user
Hi Ivan,

That's correct.

We currently use a Play request filter to add a unique requestId to the MDC.
The application logs via slf4j, with logback configured to include the MDC.
Our approach for propagating the MDC across thread boundaries is based on the first solution presented in this blog post, though it is fragile, and relies on a deprecated api (ExecutionContext.prepare)

https://yanns.github.io/blog/2014/05/04/slf4j-mapped-diagnostic-context-mdc-with-play-framework/

The solution has evolved a bit, and the actual details are represented in this gist (You can ignore AccessLogFilter)

https://gist.github.com/jsw/ef5be06d628a3efe3185a7c4bbe96ef5

Ideally, application code wouldn't need to interact with Kamon at all, and everything can be setup via filter, logging config, build dependencies, and jre runtime parameters to include the java agent.

I would love to see an example that demonstrates the capability.

I would also be interested if you could also share what the performance cost of using Kamon is. I'm not sure how much overhead there is using a java agent in general. At this point, I'm just interested in MDC propagation.

thanks!
jeff
> To unsubscribe from this group and stop receiving emails from it, send an email to kamon-user+...@googlegroups.com.

Ivan Topolnjak

unread,
Feb 2, 2018, 1:45:31 PM2/2/18
to kamon...@googlegroups.com
Hey Jeff,

I'll get back to you over the weekend with a example. It is possible to do it with Kamon 1.0 as-is, but when we merge #509 [1] it will be a lot simpler to do it. Waiting a bit for feedback and to test it together with kamon-logback. 

jsw...@ebay.com

unread,
Feb 12, 2018, 12:13:11 PM2/12/18
to kamon-user
Hi Ivan,

Have you had a chance to make progress on this?

thanks
jeff

Ivan Topolnjak

unread,
Feb 16, 2018, 7:55:30 AM2/16/18
to kamon...@googlegroups.com
Hello Jeff!

There is some progress, but still need to publish a release of core and fix an issue we have with the SBT plugin for Play 2.6 [1].. I'm sorry that I offered something and couldn't deliver just yet, as soon as the release is published I'll get the example for you, hopefully this weekend will be *the* weekend it actually happens, thanks for the patience!


--
You received this message because you are subscribed to the Google Groups "kamon-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kamon-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages