You are viewing documentation for an outdated version. Do you wish to see documentation for the latest version?
The kamon-futures module provides bytecode instrumentation for Scala, Twitter and Scalaz Futures that automatically
propagates the current Context across the asynchronous operations that might be scheduled for a given Future.
The following artifacts are published, pick the ones that match the libraries you are using in your services:
kamon-scala-future for Scala 2.10, 2.11 and 2.12.kamon-twitter-future for util-core 6.34 in Scala 2.10 and util-core 6.40 for Scala 2.11 and 2.12.kamon-scalaz-future for scalaz-concurrent 7.2.8 with Scala 2.10, 2.11 and 2.12.
libraryDependencies += "io.kamon" %% "kamon-scala-future" % "1.0.0"
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-scala-future_2.13</artifactId>
<version>1.0.0</version>
</dependency>
implementation 'io.kamon:kamon-scala-future_2.13:1.0.0'
You must start your application with the instrumentation agent for this module to work properly.
In the following piece of code, the body of the future will be executed asynchronously on a thread provided by the
ExecutionContext available in implicit scope, but Kamon will capture the current Context available when the future
was created and make it available while executing the future’s body.
Kamon.withContextKey(userID, Some("1234")) {
// The Context is available here,
Future {
// is available here as well.
"Hello Kamon"
}.map(_.length)
.flatMap(len => Future(len.toString))
.map(s => Kamon.currentContext().get(userID))
.map(println)
// And through all async callbacks, even while
// they are executed at different threads!
}
Also, when you transform a future by using map/flatMap/filter and friends or you directly register a callback on a
future (onComplete/onSuccess/onFailure), Kamon will capture the current Context available when transforming
the future and make it available when executing the given callback. The code snippet above would print the same
userID that was available when creating the future, during its body execution and during the execution of all
the asynchronous operations scheduled on it.