Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Last active January 15, 2019 14:06
Show Gist options
  • Save kubukoz/3e301c7a8a463ff8bdb3f89f40cef717 to your computer and use it in GitHub Desktop.
Save kubukoz/3e301c7a8a463ff8bdb3f89f40cef717 to your computer and use it in GitHub Desktop.
Getting Slick 3.x + Kamon 1.x to stop forgetting the context
//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