diff options
author | Ivan Topolnak <ivantopo@gmail.com> | 2013-05-30 13:04:41 -0300 |
---|---|---|
committer | Ivan Topolnak <ivantopo@gmail.com> | 2013-05-30 13:04:41 -0300 |
commit | 38316a2b0b3a58b81cf7458b0a719980136bbb97 (patch) | |
tree | 156ac7cde159d432d70f4f5b82aa1ed67e4aee28 /src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala | |
parent | 577a03219827889e31fedbe915f677060503cc2c (diff) | |
download | Kamon-38316a2b0b3a58b81cf7458b0a719980136bbb97.tar.gz Kamon-38316a2b0b3a58b81cf7458b0a719980136bbb97.tar.bz2 Kamon-38316a2b0b3a58b81cf7458b0a719980136bbb97.zip |
Settled with a @Aspectj style for instrumentation on runnables
Diffstat (limited to 'src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala')
-rw-r--r-- | src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala b/src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala new file mode 100644 index 00000000..ce19a7e6 --- /dev/null +++ b/src/main/scala/kamon/instrumentation/PromiseCompletingRunnableInstrumentation.scala @@ -0,0 +1,61 @@ +package kamon.instrumentation + +import org.aspectj.lang.annotation._ +import kamon.TraceContext +import org.aspectj.lang.ProceedingJoinPoint +import scala.Some + +/** + * Marker interface, just to make sure we don't instrument all the Runnables in the classpath. + */ +trait TraceContextAwareRunnable extends Runnable {} + + +@Aspect("perthis(instrumentedRunnableCreation())") +class PromiseCompletingRunnableInstrumentation { + + /** + * These are the Runnables that need to be instrumented and make the TraceContext available + * while their run method is executed. + */ + @DeclareMixin("scala.concurrent.impl.CallbackRunnable || scala.concurrent.impl.Future.PromiseCompletingRunnable") + def onCompleteCallbacksRunnable: TraceContextAwareRunnable = null + + + /** + * Pointcuts + */ + + @Pointcut("execution(kamon.instrumentation.TraceContextAwareRunnable+.new(..))") + def instrumentedRunnableCreation(): Unit = {} + + @Pointcut("execution(* kamon.instrumentation.TraceContextAwareRunnable.run())") + def runnableExecution() = {} + + + /** + * Aspect members + */ + + private val traceContext = TraceContext.current + + + /** + * Advices + */ + + @Around("runnableExecution()") + def around(pjp: ProceedingJoinPoint) = { + import pjp._ + + traceContext match { + case Some(ctx) => { + TraceContext.set(ctx) + proceed() + TraceContext.clear + } + case None => proceed() + } + } + +} |