Kamon icon indicating copy to clipboard operation
Kamon copied to clipboard

Bug with Kamon and SPRAY and async/await, Context lost !!!

Open tobiaseriksson opened this issue 11 years ago • 6 comments

The following code results in a lost context/token

post {
      path("car") {
        pathEnd {
          respondWithMediaType("application/vnd.api+json") {
            entity(as[String]) { body =>
              val context = Kamon.tracer.newContext("CreateResource-Context",Some("token-777"))
              Tracer.withContext(context) {
                logger.debug(s"Create resource of type 'car'")
                onComplete(
                  async {
                    Tracer.withContext[JValue](context) {
                      logger.debug("Create Resource " + Tracer.currentContext.name + " token " + Tracer.currentContext.token)
                      val res = justASimpleFunction()
                      val reg = await(aCallToAnAsyncFunction())
                      await(generateSomeJsonAsync(reg))
                    }
                  }
                ) { result => complete {
                  result match {
                    case Success(jsonBody) =>
                      new HttpResponse(201)
                        .withEntity(compact(render(jsonBody)))
                    case Failure(fail) =>
                      HttpResponse(500).withEntity("failed")
                  }
                }
                }
              }
            }
          }
        }
      }
    }

The output looks like this

2015-11-16 16:27:08 *KANMON* [token-777] DEBUG [default-akka.actor.default-dispatcher-2] c.t.p.s.S.HttpServiceActor [SprayAndKamonTest.scala:91] Create resource of type 'car'
2015-11-16 16:27:08 *KANMON* [Tobiass-MacBook-Pro.local-1] DEBUG [ForkJoinPool-3-worker-15] c.t.p.s.S.HttpServiceActor [SprayAndKamonTest.scala:95] Create Resource UnnamedTrace token Tobiass-MacBook-Pro.local-1

As can be seen the second logger output does not have the correct KANMON Context and thus this must be a bug.

In fact commenting out the await code and replacing with a JSON object like this will make it work

onComplete(
                  async {
                    // Tracer.withContext[JValue](context) {
                    Tracer.withContext(context) {
                      logger.debug("Create Resource " + Tracer.currentContext.name + " token " + Tracer.currentContext.token)
                     /* val res = justASimpleFunction()
                      val reg = await(aCallToAnAsyncFunction())
                      await(generateSomeJsonAsync(reg))
                      */
                     JField("car", List(JField("brand", JString("volvo")), JField("model", JString("V70")), JField("REG", JString("ABC-123"))))
                    }
                  }
                )

And as can be seen the output is now correct

2015-11-16 16:41:32 *KANMON* [token-777] DEBUG [default-akka.actor.default-dispatcher-5] c.t.p.s.S.HttpServiceActor [SprayAndKamonTest.scala:91] Create resource of type 'car'
2015-11-16 16:41:32 *KANMON* [token-777] DEBUG [ForkJoinPool-3-worker-15] c.t.p.s.S.HttpServiceActor [SprayAndKamonTest.scala:96] Create Resource CreateResource-Context token token-777

So why? Is this a known problem ? Is it perhaps so that I am making a mistake ? -Tobias

tobiaseriksson avatar Nov 16 '15 15:11 tobiaseriksson

Hello Tobias, this certainly is not a known issue. What I find more interesting is that in the first case you are getting both your custom trace and the automatically generated trace from the spray instrumentation... I think that it shouldn't even be necessary to have the nested Tracer.withContext {...} that you are using there, as the Future instrumentation should already take care of carrying that for you... I will give it a try and update you, thanks for reporting!

ivantopo avatar Nov 25 '15 15:11 ivantopo

Are you using an AsyncAppender in your logback configuration, by chance?

dispalt avatar Feb 05 '16 04:02 dispalt

Hi Guys, This issue is happening to us as well in our Application. The Trace Context is not getting propagated. Is there any work going on this front ? or will it be a long time before this gets fixed ? Do let us know, so that we can plan appropriately. ThnQ.

tojaish avatar Feb 11 '16 15:02 tojaish

Hey @tobiaseriksson @ivantopo any activity on this issue? we are seeing a similar behavior as @tojaish described. are we using it wrong :) ? thanks!

shveik avatar Mar 07 '16 18:03 shveik

Hi No we had to work around it, and since then we have not bothered to pick this issue up Not sure what we did now -Tobias

On 07 Mar 2016, at 19:18, Alex Reznik [email protected] wrote:

Hey @tobiaseriksson https://github.com/tobiaseriksson @ivantopo https://github.com/ivantopo any activity on this issue? we are seeing a similar behavior as @tojaish https://github.com/tojaish described. are we using it wrong :) ? thanks!

— Reply to this email directly or view it on GitHub https://github.com/kamon-io/Kamon/issues/285#issuecomment-193378679.

tobiaseriksson avatar Mar 07 '16 20:03 tobiaseriksson

I'm closing this issue because it is either too old to be relevant or stale. Stop be our Github Discussions section and drop a message if you need any additional help with the topic.

Have a nice day!

ivantopo avatar Sep 10 '20 06:09 ivantopo