blob: 918b4aacd3925549e41288d94301c52e8be76b13 (
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
|
package akka.instrumentation
import org.aspectj.lang.annotation.{Around, Before, Pointcut, Aspect}
import kamon.TraceContext
import scala.util.Try
import scala.concurrent.ExecutionContext
import org.aspectj.lang.ProceedingJoinPoint
@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 = {
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) = {
val wrappedFunction = traceContext match {
case Some(ctx) => (tryResult: Try[Any]) => {
TraceContext.set(ctx)
val result = func(tryResult)
TraceContext.clear
result
}
case None => func
}
pjp.proceed(pjp.getArgs.updated(0, wrappedFunction))
}
}
|