Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cats Effect 3.x initial implementation #1128

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ lazy val `kamon-cats-io` = (project in file("instrumentation/kamon-cats-io"))
kanelaAgent % "provided",
{
if(scalaBinaryVersion.value == "2.11")
"org.typelevel" %% "cats-effect" % "2.0.0" % "provided"
"org.typelevel" %% "cats-effect" % "3.3.5" % "provided"
Copy link
Contributor

@hughsimpson hughsimpson Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be saner to split this into kamon-cats-effect2/kamon-cats-effect-3 ? I'm picturing people on cats effect 2 bumping kamon and suddenly their instrumentation breaks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the best way to go indeed.. Just gotta make it work first 😉

else
"org.typelevel" %% "cats-effect" % "2.1.2" % "provided"
"org.typelevel" %% "cats-effect" % "3.3.5" % "provided"
},
scalatest % "test",
logbackClassic % "test"
Expand Down
15 changes: 12 additions & 3 deletions instrumentation/kamon-cats-io/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#############################################
# Kamon Cats IO Reference Configuration #
#############################################

kanela.modules {
executor-service {
within += "cats.effect.internals.IOShift\\$Tick"
within += "cats.effect.internals.IOTimer\\$ShiftTick"
within += "cats.effect.*"
}
cats-fibers {
name = "Cats-IO Instrumentation"
description = "Provides instrumentation for Cats IO Fibers"

instrumentations = [
"kamon.instrumentation.cats.IOFiberInstrumentation"
]
within = [
"^cats.effect.*"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package kamon.instrumentation.cats

import cats.effect.{IO, IOLocal}
import kamon.Kamon
import kamon.context.Context
import kamon.instrumentation.context.HasContext
import kanela.agent.api.instrumentation.InstrumentationBuilder
import kanela.agent.libs.net.bytebuddy.asm.Advice


class IOFiberInstrumentation extends InstrumentationBuilder {

/**Approach: RunLoop Instrumentation**/
//onTypes("cats.effect.IOFiber")
// .advise(anyMethods(
// "runLoop",
// ), InstrumentRunLoop)
/**Approach: RunLoop Instrumentation**/



/**Approach: Instrumenting run() and "forks" **/
onTypes("cats.effect.IOFiber")
.advise(method("run"), RestoreContextFromFiber)
.advise(method("run"), SaveCurrentContextOnExit)

onTypes("cats.effect.IOFiber")
.advise(anyMethods(
"rescheduleFiber",
"scheduleFiber",
"scheduleOnForeignEC",
), SetContextOnNewFiber)
onTypes("cats.effect.unsafe.WorkStealingThreadPool")
.advise(anyMethods("scheduleFiber", "rescheduleFiber", "scheduleExternal"),SetContextOnNewFiberForWSTP)
/**Approach: More efficient solution**/



/**Debug: begin**/
onTypes("cats.effect.IOFiber")
.advise(anyMethods(
"runLoop",
), Debug)
/**Debug: end**/

}
object Helper {
def padTo(obj: Any, len: Int): String =
obj.toString.take(len).padTo(len, " ").mkString("")

def setIfNotEmpty(f: HasContext)(ctx: Context): Unit =
if(ctx.nonEmpty()){
f.setContext(ctx)
}

def setCurrentCtxIfNotEmpty(ctx: Context): Unit =
if(ctx.nonEmpty()){
Kamon.storeContext(ctx)
}
}
import Helper._


object RestoreContextFromFiber {
@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.This fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"run(enter) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val ctxFiber = fiber.asInstanceOf[HasContext].context
setCurrentCtxIfNotEmpty(ctxFiber)
}
}

object SaveCurrentContextOnExit {
@Advice.OnMethodExit(suppress = classOf[Throwable])
def exit(@Advice.This fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"run(exit) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val currentCtx = Kamon.currentContext()
setIfNotEmpty(fiber.asInstanceOf[HasContext])(currentCtx)
}
}


object SetContextOnNewFiber {
@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.This currFiber: Any, @Advice.Argument(1) fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"ScheduleNew | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(currFiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo(fiber.hashCode(), 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val currentCtx = Kamon.currentContext()
setIfNotEmpty(fiber.asInstanceOf[HasContext])(currentCtx)
}
}

object SetContextOnNewFiberForWSTP {
@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.Argument(0) fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"ScheduleNew | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo("unknown", 10)} | ToBeScheduledFiberId: ${padTo(fiber.hashCode(), 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val currentCtx = Kamon.currentContext()
setIfNotEmpty(fiber.asInstanceOf[HasContext])(currentCtx)
}
}

object InstrumentRunLoop {
@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.This fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"run(enter) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val ctxFiber = fiber.asInstanceOf[HasContext].context
Kamon.storeContext(ctxFiber)
}

@Advice.OnMethodExit(suppress = classOf[Throwable])
def exit(@Advice.This fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"run(exit) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: {${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(fiber.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
val currentCtx = Kamon.currentContext()
fiber.asInstanceOf[HasContext].setContext(currentCtx)
}
}

object Debug {

@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.This fiber: Any, @Advice.Argument(0) io :Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"runLoop(Enter) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(io.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
}

@Advice.OnMethodExit(suppress = classOf[Throwable])
def exit(@Advice.This fiber: Any, @Advice.Argument(0) io :Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"runLoop(Exit) | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo(fiber.hashCode(), 10)} | ToBeScheduledFiberId: ${padTo("NA", 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(io.getClass.getCanonicalName, 25)} | Thread ${Thread.currentThread().getName}")
}
}

object DebugWT {

@Advice.OnMethodEnter(suppress = classOf[Throwable])
def enter(@Advice.Argument(0) fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"WorkerThread | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo("undefined", 10)} | ToBeScheduledFiberId: ${padTo(fiber.hashCode(), 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(-1, 25)} | Thread ${Thread.currentThread().getName}")
}

@Advice.OnMethodExit(suppress = classOf[Throwable])
def exit(@Advice.Argument(0) fiber: Any): Unit = {
val field = fiber.getClass.getDeclaredField("resumeTag")
field.setAccessible(true)
//println(s"WorkerThread | Resume Tag: ${field.get(fiber)} | CurrFiberId: ${padTo("undefined", 10)} | ToBeScheduledFiberId: ${padTo(fiber.hashCode(), 10)} | Fiber: ${padTo(fiber.asInstanceOf[HasContext].context.tags, 15)} | Thread ${padTo(Kamon.currentContext().tags, 15)} | IO: ${padTo(-1, 25)} | Thread ${Thread.currentThread().getName}")
}
}

This file was deleted.

Loading