Akka是否会将MDC从来源演员复制到其他演员和期货? [英] Does akka copy MDC from source actor to other actors and futures?

查看:120
本文介绍了Akka是否会将MDC从来源演员复制到其他演员和期货?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在akka规范中阅读的那样,它支持mdc 在演员中。例如。我可以将unic信息放入mdc,然后在actor中使用它。但是未来呢? Akka是否提供任何担保,以保证在actor中启动的未来将具有相同的MDC?另外,发送给其他参与者的消息也是如此-默认情况下会复制MDC吗?

As I read in akka specification it supports mdc in actors. E.g. I can put unic infomation in mdc and then use it in actor. But what about futures? Does akka provide any guaranties that a future which is initiated in actor will have the same mdc? Also what about message that send to other actors - is MDC copied by default?

注意

对我来说,这很奇怪,我只能在一个参与者代码中使用MDC。

For me it looks very strange, that I can use MDC only inside one actor code.

推荐答案

他们实际上没有。当您调用LoggingAdapter的成员时,实际上是调用actor的成员:

They could but they actually don't. When you call members of LoggingAdapter you actually call member of actor:

package akka.event
trait LoggingAdapter {// and it's implementations DagnosticLoggingAdapter, BusLoggingAdapter

  type MDC = Logging.MDC
  def mdc = Logging.emptyMDC
  def notifyError(message: String): Unit = bus.publish(Error(logSource, logClass, message, mdc))
  ...
}

所以您在这里访问演员的成员。每次处理请求之前都会设置此成员:

So you're accessing actor's member here. This member is set every time before processing request:

package akka.actor
trait DiagnosticActorLogging extends Actor {
  ...

  override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = try {
    log.mdc(mdc(msg))
    super.aroundReceive(receive, msg)
  } finally {
    log.clearMDC()
  }
}

因此,如果您将来要访问它(另一个线程可能与其他消息同时运行),则不能保证您为自己选择了mdc信息。与接收器存在相同的问题,但问题更深,因为您无法轻松捕获其他的mdc信息。

So if you're accessing it from the future (another thread which might be run simultaneously with some other message) - there is no guarantee that you pick up mdc for your message. Same problem as for receiver, but more deep as you can't capture additional mdc info easy way.

P.S。 Akka可能会更聪明并以隐式方式获取mdc信息,因此您可以将其捕获为闭包,诸如此类:

P.S. Akka could be smarter and acquire mdc information as implicit, so you could capture it as a closure, something like that:

  implicit val metaMdc = getMetaMdc
  Future {

    log.warning(...)
  }

这里的问题是,每次Slog4J的Mdc为本地线程,因此每个线程都不同。因此,Akka不知道您要使用哪个物理MDC来执行 log.warning(...)

The problem here is that Akka would have to attach this metaMdc to current thread's mdc every time you log (or you have to initialize it some way) as SLF4J's Mdc is thread-local, so it's different for every thread. So, Akka doesn't know with which exactly physical MDC you're gonna execute your log.warning(...).

这篇关于Akka是否会将MDC从来源演员复制到其他演员和期货?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆