MDC context inter thread propagation in Play 2.6

Hello Play team,

We have microservice infrastructure with business services written on play 2.6. There are some flows that includes inter-service communication, actor based flows, java based completable futures usage, therefore we have to apply some activity id tracking for rich and explanatory logs.
In order to include activityid to ordinary logs we use MDC extension for logback which works fine in single thread environment. But since play framework is async one under the hood, we need to have some inter thread mdc propagation solution to cover the above cases.
Cinnamon looks like cool solution for that purpose. I’ve already done PoC on cinnamon and tested these cases (which ultimately should work with not-cinnamon solution):

  1. MDC propagation between http filter and http controller
  2. MDC propagation between http controller and akka actor
  3. MDC propagation between akka actors
  4. MDC propagation between http controller and async thread

However, cinnamon solution is not free which doesn’t suit our customer needs. I also tried this and and a couple of others and none of them worked for the above cases.

Could you please point me out to right direction in this problem or suggest some tool for achieving our purpose?

Thanks a lot in advance

Although I have not used it myself, but I believe the recommendation is to use the new Logging Marker API. I don’t know if it can be an adequate alternative for what you are doing, but perhaps worth looking into.

Hi
Just for information, managed to fix the issues by having custom execution context like this

Then creating Actor System with custom execution context like:

ActorSystem
      .create("customActorSystem", null, null,
          MDCHttpExecutionContext.fromThread(play.api.libs.concurrent.Execution.defaultContext()))

After that one can instantiate actors via custom actor system via actorOf method.

For async computation, custom executor should be specified:

return CompletableFuture.supplyAsync(() -> {
//... business logic
    }, MDCHttpExecutionContext.fromThread(play.api.libs.concurrent.Execution.defaultContext()))

Hope it will help someone
Thanks,
Mykhailo

1 Like

This is pretty useful. Thanks

There is a related pull request: https://github.com/playframework/playframework/pull/8391