[play 2.2.x scala] Logback MDC and ExecutionContext

592 views
Skip to first unread message

Yann Simon

unread,
Sep 11, 2013, 3:30:08 AM9/11/13
to play-fr...@googlegroups.com
Hi,

I'm trying to support logback MDC with play 2.2.0-RC1

I choose the way to write my own ExecutionContext that propagates the MDC context from one thread to another:


It works for Action compositions.
It works for Iteratee as Iteratees for play >= 2.2.0 can use a custom execution context.

But it does not work in a simple Action like this:
def simpleAction = {
  MDC.get("key") // works here
  Action { request =>
    MDC.get("key") // does not work here
    ...
  }
}

I know that Action { }  constructs an action that will be asynchronously executed.
And I suspect that this asynchronous execution uses the internal play execution context, and not the custom one that propagates the MDC context.

Is my understanding right?
Is it possible to use a custom ExecutionContext for all asynchronous callbacks?
Maybe in the akka configuration?

Thanks in advance,
Yann

Yann Simon

unread,
Sep 17, 2013, 10:42:49 AM9/17/13
to play-fr...@googlegroups.com
up, any ideas, pointers?


2013/9/11 Yann Simon <yann.s...@gmail.com>

Yann Simon

unread,
May 5, 2014, 10:08:35 AM5/5/14
to play-fr...@googlegroups.com

Jaap Taal

unread,
May 5, 2014, 2:28:40 PM5/5/14
to play-fr...@googlegroups.com
This looks really great, maybe typesafe could review these kind of things and even port them to play itself. I think these rich contexts are really needed for things like logging etc.
How is the performance of this code? Could you show that the overhead is not too big?

Martin Grotzke

unread,
May 5, 2014, 7:31:15 PM5/5/14
to play-fr...@googlegroups.com

Great, thanks for sharing @Yann!

Cheers,
Martin

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

EduP

unread,
May 6, 2014, 12:49:47 PM5/6/14
to play-fr...@googlegroups.com
The idea (and post) are really valuable, thanks for sharing. A more transparent solution should come from adapting to Akka 2.3 ActorWithMDC:

Yann Simon

unread,
May 8, 2014, 1:31:47 PM5/8/14
to play-fr...@googlegroups.com
Thanks everyone for the feedback!

I'll try to answer all the questions:
@Jaap Taal:
> This looks really great, maybe typesafe could review these kind of things and even port them to play itself. I think these rich contexts are really needed for things like logging etc.

Yes, it would be good if some expert eyes could review this code.

Concerning porting this functionality into play itself, I have mixed feeling:
On one hand, MDC is very convenient very logging.
On the other hand, using a ThreadLocal is a severe limitation of the
default implementation. Managing ThreadLocal variables in asynchronous
context can be quite tricky.
-> Maybe we can open an issue to discuss that on github.
Otherwise, I'd take time to package this code as library.

Anyways, I'm very happy that the play framework is flexible enough to
allow custom ExecutionContext. AFAIK this was not possible with the
first versions. Achieving that was a huge amount of work, especially
in the Iteratee area. Hats off to the team for that!

> How is the performance of this code? Could you show that the overhead is not too big?

I've not performed any scientific measurements. I'd say the
performance overhead is very light.
Actually, a similar custom ExecutionContext is already used for java
projects for the HttpContext:
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/core/j/HttpExecutionContext.scala


@EduP:
> The idea (and post) are really valuable, thanks for sharing. A more
> transparent solution should come from adapting to Akka 2.3 ActorWithMDC:
>
> https://groups.google.com/forum/#!topic/play-framework/N3XXBZBLuVc

Thanks for the pointer!
If any transparent solution works, I'd glad to delete my code.
As far as I can understand the custom MDC in Akka, the values are
applied to the current actor.
They are not propagated from actor to actor.
And they are not propagated if you use a Future with an
ExecutionContext neither.
This is quite limiting IMO.
Or do I understand the Akka custom MDC wrong?

Cheers,
Yann

gitted

unread,
May 8, 2014, 1:40:37 PM5/8/14
to play-fr...@googlegroups.com
You can I believe wrap it with a future call and explicitly set the execution context for the future call.

Action.async {
  Future {

  }(customEc)

EduP

unread,
Dec 16, 2014, 5:55:05 AM12/16/14
to play-fr...@googlegroups.com
In case anyone lands here, @Yann added a more transparent solution based on a smarter Dispatcher

Reply all
Reply to author
Forward
0 new messages