Documentation

Automatic TraceContext Propagation with Futures

The kamon-scala module provides bytecode instrumentation for both Scala and Scalaz Futures that automatically propagates the TraceContext across the asynchronous operations that might be schedulede for a given Future.

The kamon-scala module require you to start your application using the AspectJ Weaver Agent. Kamon will warn you at startup if you failed to do so.

Future’s Body and Callbacks

In the following piece of code, the body of the future will be executed asynchronously on some other thread provided by the ExecutionContext available in implicit scope, but Kamon will capture the TraceContext available when the future was created and make it available while executing the future’s body.

Tracer.withNewContext("sample-trace") {
    // The same TraceContext available here,

    Future {
      // is available here as well.
      "Hello Kamon"

    }.map(_.length)
      .flatMap(len => Future(len.toString))
      .map(s => Tracer.currentContext)
      .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 onComplete/onSuccess/onFailure callback on a future, Kamon will capture the TraceContext available when transforming the future and make it available when executing the given callback. The code snippet above would print the same TraceContext that was available when creating the future, during it’s body execution and during the execution of all the asynchronous operations scheduled on it.