aboutsummaryrefslogtreecommitdiff
path: root/kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala
diff options
context:
space:
mode:
authorIvan Topolnak <ivantopo@gmail.com>2013-11-04 18:11:16 -0300
committerIvan Topolnak <ivantopo@gmail.com>2013-11-04 18:11:16 -0300
commit5127c3bb83cd6fe90e071720d995cfb53d913e6a (patch)
treea29cc7be7f729566f3a5a2fc3bc8044c7f47f3cc /kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala
parentca1e93621ddad4b9f2a9028ea183b1c2f4c25a27 (diff)
downloadKamon-5127c3bb83cd6fe90e071720d995cfb53d913e6a.tar.gz
Kamon-5127c3bb83cd6fe90e071720d995cfb53d913e6a.tar.bz2
Kamon-5127c3bb83cd6fe90e071720d995cfb53d913e6a.zip
wip
Diffstat (limited to 'kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala')
-rw-r--r--kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala55
1 files changed, 55 insertions, 0 deletions
diff --git a/kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala b/kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala
new file mode 100644
index 00000000..236fd4fc
--- /dev/null
+++ b/kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala
@@ -0,0 +1,55 @@
+package kamon.trace.instrumentation
+
+import org.aspectj.lang.annotation._
+import org.aspectj.lang.ProceedingJoinPoint
+import kamon.trace.TraceContext
+
+@Aspect
+class RunnableTracing {
+
+ /**
+ * 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 = new TraceContextAwareRunnable {
+ val traceContext: Option[TraceContext] = Tracer.traceContext.value
+ }
+
+
+ /**
+ * Pointcuts
+ */
+
+ @Pointcut("execution(kamon.instrumentation.TraceContextAwareRunnable+.new(..)) && this(runnable)")
+ def instrumentedRunnableCreation(runnable: TraceContextAwareRunnable): Unit = {}
+
+ @Pointcut("execution(* kamon.instrumentation.TraceContextAwareRunnable+.run()) && this(runnable)")
+ def runnableExecution(runnable: TraceContextAwareRunnable) = {}
+
+
+
+ @After("instrumentedRunnableCreation(runnable)")
+ def beforeCreation(runnable: TraceContextAwareRunnable): Unit = {
+ // Force traceContext initialization.
+ runnable.traceContext
+ }
+
+
+ @Around("runnableExecution(runnable)")
+ def around(pjp: ProceedingJoinPoint, runnable: TraceContextAwareRunnable): Any = {
+ import pjp._
+
+ Tracer.traceContext.withValue(runnable.traceContext) {
+ proceed()
+ }
+ }
+
+}
+
+/**
+ * Marker interface, just to make sure we don't instrument all the Runnables in the classpath.
+ */
+trait TraceContextAwareRunnable {
+ def traceContext: Option[TraceContext]
+} \ No newline at end of file