跨线程跟踪上下文特定数据

时间:2015-02-03 18:45:21

标签: scala playframework

我知道在Play中!使用Scala,没有可用的Http.context,因为这个想法是利用implicits传递堆栈周围的任何数据。然而,当你需要一整套可用于整个环境的信息时,这似乎是很多锅炉板。

更具体地说,我感兴趣的是跟踪从请求标头传递的UUID,并使其可用于任何记录器,以便每个请求都有自己的唯一标识符。我希望这可以与任何调用记录器(或日志包装器)的人无缝连接

来自.NET背景的http上下文与异步调用一起流动,这也可以通过WCF中的调用上下文来实现。此时,您可以使用记录器注册一个函数,以根据"%requestID%"之类的记录模式返回请求的当前uuid。

构建一个更大的分布式系统,您需要能够跨多个堆栈关联请求。

但是,作为scala和play的新手,我甚至不确定在哪里寻找一种方法来做到这一点?

1 个答案:

答案 0 :(得分:2)

您在Java中寻找的内容称为映射诊断上下文或MDC(至少由SLF4J) - here's an article I found that details how to set this up for Play。为了保留未来访问者的详细信息,这里是用于MDC传播的Akka调度程序的代码:

package monitoring

import java.util.concurrent.TimeUnit

import akka.dispatch._
import com.typesafe.config.Config
import org.slf4j.MDC

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.{Duration, FiniteDuration}

/**
 * Configurator for a MDC propagating dispatcher.
 * Authored by Yann Simon
 * See: http://yanns.github.io/blog/2014/05/04/slf4j-mapped-diagnostic-context-mdc-with-play-framework/
 *
 * To use it, configure play like this:
 * {{{
 * play {
 *   akka {
 *     actor {
 *       default-dispatcher = {
 *         type = "monitoring.MDCPropagatingDispatcherConfigurator"
 *       }
 *     }
 *   }
 * }
 * }}}
 *
 * Credits to James Roper for the [[https://github.com/jroper/thread-local-context-propagation/ initial implementation]]
 */
class MDCPropagatingDispatcherConfigurator(config: Config, prerequisites: DispatcherPrerequisites)
  extends MessageDispatcherConfigurator(config, prerequisites) {

  private val instance = new MDCPropagatingDispatcher(
    this,
    config.getString("id"),
    config.getInt("throughput"),
    FiniteDuration(config.getDuration("throughput-deadline-time", TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS),
    configureExecutor(),
    FiniteDuration(config.getDuration("shutdown-timeout", TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS))

  override def dispatcher(): MessageDispatcher = instance
}

/**
 * A MDC propagating dispatcher.
 *
 * This dispatcher propagates the MDC current request context if it's set when it's executed.
 */
class MDCPropagatingDispatcher(_configurator: MessageDispatcherConfigurator,
                               id: String,
                               throughput: Int,
                               throughputDeadlineTime: Duration,
                               executorServiceFactoryProvider: ExecutorServiceFactoryProvider,
                               shutdownTimeout: FiniteDuration)
  extends Dispatcher(_configurator, id, throughput, throughputDeadlineTime, executorServiceFactoryProvider, shutdownTimeout ) {

  self =>

  override def prepare(): ExecutionContext = new ExecutionContext {
    // capture the MDC
    val mdcContext = MDC.getCopyOfContextMap

    def execute(r: Runnable) = self.execute(new Runnable {
      def run() = {
        // backup the callee MDC context
        val oldMDCContext = MDC.getCopyOfContextMap

        // Run the runnable with the captured context
        setContextMap(mdcContext)
        try {
          r.run()
        } finally {
          // restore the callee MDC context
          setContextMap(oldMDCContext)
        }
      }
    })
    def reportFailure(t: Throwable) = self.reportFailure(t)
  }

  private[this] def setContextMap(context: java.util.Map[String, String]) {
    if (context == null) {
      MDC.clear()
    } else {
      MDC.setContextMap(context)
    }
  }

}

然后,您可以使用MDC.put在MDC中设置值,并使用MDC.remove将其删除(或者,如果您需要在集合中添加和删除某些上下文,请查看putCloseable同步电话):

import org.slf4j.MDC

// Somewhere in a handler
MDC.put("X-UserId", currentUser.id)

// Later, when the user is no longer available
MDC.remove("X-UserId")

并使用%mdc{field-name:default-value}将其添加到日志记录输出中:

<!-- an example from the blog -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %coloredLevel %logger{35} %mdc{X-UserId:--} - %msg%n%rootException</pattern>
    </encoder>
</appender>

有关于调整Play用于正确传播MDC上下文的ExecutionContext的详细信息in the linked blog post。(作为替代方法)。