Last active
January 15, 2019 14:06
-
-
Save kubukoz/3e301c7a8a463ff8bdb3f89f40cef717 to your computer and use it in GitHub Desktop.
Getting Slick 3.x + Kamon 1.x to stop forgetting the context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on https://github.com/kamon-io/Kamon/issues/311 | |
class AsyncExecutorContextAware(underlying: AsyncExecutor) extends AsyncExecutor { | |
override def executionContext: ExecutionContext = new ExecutionContextAware(underlying.executionContext) | |
override def close(): Unit = underlying.close() | |
} | |
class ExecutionContextAware(underlying: ExecutionContext) extends ExecutionContext { | |
override def execute(runnable: Runnable): Unit = underlying.execute(new RunnableContextAware(runnable)) | |
override def reportFailure(cause: Throwable): Unit = underlying.reportFailure(cause) | |
} | |
class RunnableContextAware(underlying: Runnable) extends Runnable { | |
private val traceContext = Kamon.currentContext | |
override def run(): Unit = { | |
Kamon.withContext(traceContext) { | |
underlying.run() | |
} | |
} | |
} | |
//then use it to create your Database instance, e.g. | |
Database.forUrl(???, new AsyncExecutorContextAware(AsyncExecutor.default())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment