Skip to content

Instantly share code, notes, and snippets.

@michalkowol
Last active May 26, 2021 23:07
Show Gist options
  • Save michalkowol/fbe6f2d1b118f258c2eb to your computer and use it in GitHub Desktop.
Save michalkowol/fbe6f2d1b118f258c2eb to your computer and use it in GitHub Desktop.
package com.michalkowol.aapi
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}
class MDCPropagatingDispatcherConfigurator(config: Config, prerequisites: DispatcherPrerequisites)
extends MessageDispatcherConfigurator(config, prerequisites) {
override val dispatcher: MessageDispatcher = 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)
)
}
private 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): Unit = self.execute(new Runnable {
def run(): Unit = {
// 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]): Unit = {
if (context == null) { // scalastyle:ignore
MDC.clear()
} else {
MDC.setContextMap(context)
}
}
}
akka.actor.default-dispatcher.type = pl.michalkowol.play.MDCPropagatingDispatcherConfigurator
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{"yyyy-MM-dd'T'HH:mm:ss,SSS'Z'",UTC} %-5level %mdc{X-Request-Id:--} [%logger{0}] - %msg%n</pattern>
</encoder>
</appender>
<logger name="access" level="DEBUG"/>
<logger name="controllers" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
2015-09-14T11:28:25,208Z INFO - [BindListener] - Bound to /0:0:0:0:0:0:0:0:8080
2015-09-14T11:28:31,764Z INFO 1442230111722 [Sumologic] - AuthAPI Request for user xxxx/1442230111722: HttpRequest(GET,
2015-09-14T11:28:31,858Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:31,860Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:31,862Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:31,863Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:31,864Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:31,878Z DEBUG - [ContentActor] - Executing query [GET]: [
2015-09-14T11:28:31,878Z DEBUG - [ContentActor] - Executing query [GET]: [
2015-09-14T11:28:31,879Z DEBUG - [ContentActor] - Executing query [GET]: [
2015-09-14T11:28:31,880Z DEBUG - [ContentActor] - Executing query [GET]: [
2015-09-14T11:28:31,880Z DEBUG - [ContentActor] - Executing query [GET]: [
2015-09-14T11:28:31,889Z DEBUG - [ContentActor] - Querying origin [GET]:
2015-09-14T11:28:31,890Z DEBUG - [ContentActor] - Querying origin [GET]:
2015-09-14T11:28:31,890Z DEBUG - [ContentActor] - Querying origin [GET]:
2015-09-14T11:28:31,891Z DEBUG - [ContentActor] - Querying origin [GET]:
2015-09-14T11:28:31,891Z DEBUG - [ContentActor] - Querying origin [GET]:
2015-09-14T11:28:32,348Z DEBUG - [ContentActor] - SingleContentQuery() successful.
2015-09-14T11:28:32,353Z DEBUG - [ContentActor] - SingleContentQuery() successful.
2015-09-14T11:28:32,353Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:32,355Z DEBUG - [EnvironmentActor] - Executing query [GET]: [
2015-09-14T11:28:32,355Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:32,355Z DEBUG - [NamespaceActor] - Executing query [GET]: [
2015-09-14T11:28:32,356Z DEBUG - [NamespaceActor] - Querying origin [GET]:
2015-09-14T11:28:32,356Z DEBUG - [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:28:32,450Z DEBUG - [ContentActor] - SingleContentQuery(
2015-09-14T11:28:32,587Z DEBUG - [NamespaceActor] - NamespaceQuery(
2015-09-14T11:28:32,588Z DEBUG - [EnvironmentActor] - EnvQuery(
2015-09-14T11:28:32,595Z DEBUG - [ContentActor] - SingleContentQuery(
2015-09-14T11:28:32,596Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:28:32,597Z DEBUG - [SiteActor] - Executing query [GET]: [
2015-09-14T11:28:32,598Z DEBUG - [SiteActor] - Querying origin [GET]:
2015-09-14T11:28:32,709Z DEBUG 1442230111722 [ContentActor] - SingleContentQuery(
2015-09-14T11:28:32,714Z DEBUG 1442230111722 [SiteActor] - SiteQuery(
2015-09-14T11:28:32,715Z INFO 1442230111722 [Sumologic] - method:GET, url:
2015-09-14T11:28:32,716Z DEBUG - [EnvironmentActor] - Executing query [GET]: [
2015-09-14T11:28:32,716Z INFO 1442230111722 [Sumologic] - method:GET, url:
2015-09-14T11:28:32,716Z DEBUG - [EnvironmentActor] - EnvQuery(
2015-09-14T11:28:32,717Z DEBUG - [EnvironmentActor] - Executing query [GET]: [
2015-09-14T11:28:32,717Z DEBUG - [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:28:32,717Z INFO 1442230111722 [Sumologic] - method:GET, url:
2015-09-14T11:28:32,717Z DEBUG - [EnvironmentActor] - Executing query [GET]: [
2015-09-14T11:28:32,717Z DEBUG - [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:28:32,827Z DEBUG 1442230111722 [EnvironmentActor] - EnvQuery(
2015-09-14T11:28:32,828Z DEBUG 1442230111722 [EnvironmentActor] - EnvQuery(
2015-09-14T11:30:00,021Z DEBUG 1442230111722 [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:30:00,022Z DEBUG 1442230111722 [NamespaceActor] - Querying origin [GET]:
2015-09-14T11:30:00,022Z DEBUG 1442230111722 [SiteActor] - Querying origin [GET]:
2015-09-14T11:30:00,023Z DEBUG 1442230111722 [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:30:00,023Z DEBUG 1442230111722 [EnvironmentActor] - Querying origin [GET]:
2015-09-14T11:30:00,025Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:30:00,025Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:30:00,025Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:30:00,025Z INFO - [Sumologic] - method:GET, url:
2015-09-14T11:30:00,025Z INFO - [Sumologic] - method:GET, url:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment