简体   繁体   English

akka是否将MDC从源角色复制到其他演员和期货?

[英]Does akka copy MDC from source actor to other actors and futures?

As I read in akka specification it supports mdc in actors. 正如我在akka规范中所读到的,它支持演员中的mdc Eg I can put unic infomation in mdc and then use it in actor. 例如,我可以在mdc中放入unic信息,然后在actor中使用它。 But what about futures ? 期货怎么样? Does akka provide any guaranties that a future which is initiated in actor will have the same mdc? akka是否提供任何保证,在演员中启动的未来将具有相同的mdc? Also what about message that send to other actors - is MDC copied by default? 那么发送给其他参与者的消息是什么 - 默认情况下MDC被复制了?

Note 注意

For me it looks very strange, that I can use MDC only inside one actor code. 对我来说,它看起来很奇怪,我只能在一个演员代码中使用MDC。

They could but they actually don't. 他们可以,但他们实际上没有。 When you call members of LoggingAdapter you actually call member of actor: 当您调用LoggingAdapter的成员时,您实际上调用了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()
  }
}

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. 因此,如果您将来访问它(另一个可能与其他消息同时运行的线程) - 无法保证您为您的消息选择mdc。 Same problem as for receiver, but more deep as you can't capture additional mdc info easy way. 与接收器相同的问题,但更深入,因为你无法轻松捕获额外的mdc信息。

PS Akka could be smarter and acquire mdc information as implicit, so you could capture it as a closure, something like that: PS Akka可能更聪明并且获取mdc信息是隐式的,所以你可以把它捕获为一个闭包,类似的东西:

  implicit val metaMdc = getMetaMdc
  Future {

    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. 这里的问题是Akka必须在每次登录时将这个metaMdc附加到当前线程的mdc(或者你必须以某种方式初始化它),因为SLF4J的Mdc是线程本地的 ,所以每个线程都不同。 So, Akka doesn't know with which exactly physical MDC you're gonna execute your log.warning(...) . 所以,Akka不知道你将使用哪个物理MDC来执行你的log.warning(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM