blob: 087e43bf088d32ffe4f494235021a16244f55e21 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package kamon.instrumentation
import org.aspectj.lang.annotation._
import kamon.TraceContext
import scala.util.Try
import scala.concurrent.ExecutionContext
import org.aspectj.lang.ProceedingJoinPoint
import scala.Some
@Aspect("perthis(promiseCreation())")
class PromiseInstrumentation {
private var traceContext: Option[TraceContext] = None
@Pointcut("execution(scala.concurrent.impl.Promise.DefaultPromise.new(..))")
def promiseCreation(): Unit = {}
@Before("promiseCreation()")
def catchTheTraceContext = {
println(s"During promise creation the context is: ${TraceContext.current}")
TraceContext.current match {
case Some(ctx) => traceContext = Some(ctx.fork)
case None => traceContext = None
}
}
@Pointcut("execution(* scala.concurrent.Future.onComplete(..)) && args(func, executor)")
def registeringOnCompleteCallback(func: Try[Any] => Any, executor: ExecutionContext) = {}
@Around("registeringOnCompleteCallback(func, executor)")
def around(pjp: ProceedingJoinPoint, func: Try[Any] => Any, executor: ExecutionContext) = {
import pjp._
val wrappedFunction = traceContext match {
case Some(ctx) => (tryResult: Try[Any]) => {
TraceContext.set(ctx)
val result = func(tryResult)
TraceContext.clear
result
}
case None => func
}
proceed(getArgs.updated(0, wrappedFunction))
}
@Pointcut("execution(* scala.concurrent.impl.Future$.apply(..)) && args(body, executor)")
def registeringApplyOnFuture(body: () => Any, executor: ExecutionContext) = {}
@Around("registeringApplyOnFuture(body, executor)")
def aroundApplyOnFuture(pjp:ProceedingJoinPoint, body: () => Any, executor: ExecutionContext) = {
import pjp._
val wrappedBody = wrapFutureBody(traceContext)(body)
proceed(getArgs.updated(0, wrappedBody))
}
def wrapFutureBody[A](ctx:Option[TraceContext])(block: => A) : A = {
ctx match {
case Some(ctx) => {
println("Wrapping body")
TraceContext.set(ctx)
val result = block
TraceContext.clear
result
}
case None => block
}
}
}
|