diff options
Diffstat (limited to 'src/main/scala/xyz/driver/core/database/MdcExecutionContext.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/database/MdcExecutionContext.scala | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/database/MdcExecutionContext.scala b/src/main/scala/xyz/driver/core/database/MdcExecutionContext.scala new file mode 100644 index 0000000..f08f16c --- /dev/null +++ b/src/main/scala/xyz/driver/core/database/MdcExecutionContext.scala @@ -0,0 +1,28 @@ +package xyz.driver.core.database + +import org.slf4j.MDC +import scala.concurrent.ExecutionContext + +/** + * Execution context proxy for propagating SLF4J diagnostic context from caller thread to execution thread. + */ +class MdcExecutionContext(executionContext: ExecutionContext) extends ExecutionContext { + override def execute(runnable: Runnable): Unit = { + val callerMdc = MDC.getCopyOfContextMap + executionContext.execute(new Runnable { + def run(): Unit = { + // copy caller thread diagnostic context to execution thread + // scalastyle:off + if (callerMdc != null) MDC.setContextMap(callerMdc) + try { + runnable.run() + } finally { + // the thread might be reused, so we clean up for the next use + MDC.clear() + } + } + }) + } + + override def reportFailure(cause: Throwable): Unit = executionContext.reportFailure(cause) +} |