Need guidance with production monitoring?
Book FREE office hours and we'll help you out
Not sure how to approach production monitoring? Book FREE office hours and we'll help you out

You are viewing documentation for an outdated version. Do you wish to see documentation for the latest version?

Instrumentation/Available Instrumentation

Context Propagation with Futures #

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.

Dependency Installation #

libraryDependencies += "io.kamon" %% "kamon-scala-future" % "1.0.0"


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.

Future’s Body and Callbacks #

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"

      .flatMap(len => Future(len.toString))
      .map(s => Kamon.currentContext().get(userID))
      // 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.

On this article
Kamon APM Logo
Monitor and fix issues in production without being an expert
Learn about APM
Try Kamon APM I know how Kamon APM can help with monitoring. Don't show this again.