Skip to content

Instantly share code, notes, and snippets.

@ktonga
Created November 21, 2013 14:32
Show Gist options
  • Save ktonga/7582529 to your computer and use it in GitHub Desktop.
Save ktonga/7582529 to your computer and use it in GitHub Desktop.
Not working example of aroundRecive usage. Hook mdc(msg) is called after Contextual decoration.
trait MessageContext {
implicit var msgCtx: Option[MsgCtx] = None
def doWithContext(message: Any)(f: Any => Unit): Unit = {
val (ctx, msg) = message match {
case wrapper: Msg[_] => (wrapper.ctx, wrapper.msg)
case other => (None, other)
}
msgCtx = ctx
f(msg)
msgCtx = None
}
}
// DiagnosticActorLogging already overrides aroundReceive
trait Logging extends DiagnosticActorLogging {
this: MessageContext =>
override def mdc(currentMessage: Any): event.Logging.MDC = {
// this happens after ContextualActor decoration,
// so message is still wrapped and context is None
msgCtx.map(c => Map("requestId" -> c.attr)).getOrElse(Map())
}
}
//------- In Akka space --------//
package akka
trait ContextualActor extends Actor with MessageContext {
protected[akka] override def aroundReceive(receive: Actor.Receive, msg: Any): Unit = {
doWithContext(msg) {m => super.aroundReceive(receive, m)}
}
}
//------------------------------//
trait BaseActor extends akka.ContextualActor with Logging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment