Is there com.twitter.util.Local for Scala Future and ExecutionContext?

651 Views Asked by At

ThreadLocal-like class that keeps its value through Future.map/Future.flatMap is extremely useful to trace request, for example, for logging.

Is there existing abstraction in Scala library to serve as such Local?

Is there a way to attach such Local to ExecutionContext.global?

1

There are 1 best solutions below

0
On

Here is a blog entry where someone describes using scala.util.DynamicVariable and a custom scala.concurrent.ExecutionContext to capture and manage it: http://stevenskelton.ca/threadlocal-variables-scala-futures/

And here's is another blog entry describing how Hootsuite does something similar: http://code.hootsuite.com/logging-contextual-info-in-an-asynchronous-scala-application/