-
Notifications
You must be signed in to change notification settings - Fork 328
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
cmcmteixeira
wants to merge
7
commits into
kamon-io:master
Choose a base branch
from
cmcmteixeira:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f72077a
Added failing test for cats-io 3.x context propagation.
cmcmteixeira 4841908
First draft.
cmcmteixeira 8d4163f
Couple more changes.
cmcmteixeira c9b0686
Fixed tests.
cmcmteixeira 596dc70
Small improvement?
cmcmteixeira b02bff1
A few more changes.
cmcmteixeira f9ddb40
Fixed misconfigured test.
cmcmteixeira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 12 additions & 3 deletions
15
instrumentation/kamon-cats-io/src/main/resources/reference.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*" | ||
] | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
...tion/kamon-cats-io/src/main/scala/kamon/instrumentation/cats/IOFiberInstrumentation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |
60 changes: 0 additions & 60 deletions
60
...cats-io/src/test/scala/kamon/instrumentation/futures/cats/CatsIOInstrumentationSpec.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😉