aboutsummaryrefslogtreecommitdiff
path: root/kamon-trace/src/main/scala/kamon/trace/instrumentation/ActorLoggingInstrumentation.scala
blob: 77993cdd27a66200df8d9fbcec9a820980698f6e (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
package kamon.trace.instrumentation

import org.aspectj.lang.annotation.{Around, Pointcut, DeclareMixin, Aspect}
import org.aspectj.lang.ProceedingJoinPoint
import org.slf4j.MDC
import kamon.trace.{TraceContext, ContextAware, Trace}

@Aspect
class ActorLoggingInstrumentation {


  @DeclareMixin("akka.event.Logging.LogEvent+")
  def traceContextMixin: ContextAware = new ContextAware {
    def traceContext: Option[TraceContext] = Trace.context()
  }

  @Pointcut("execution(* akka.event.slf4j.Slf4jLogger.withMdc(..)) && args(logSource, logEvent, logStatement)")
  def withMdcInvocation(logSource: String, logEvent: ContextAware, logStatement: () => _): Unit = {}

  @Around("withMdcInvocation(logSource, logEvent, logStatement)")
  def putTraceContextInMDC(pjp: ProceedingJoinPoint, logSource: String, logEvent: ContextAware, logStatement: () => _): Unit = {
    logEvent.traceContext match {
      case Some(ctx) =>
        MDC.put("uow", ctx.uow)
        pjp.proceed()
        MDC.remove("uow")

      case None => pjp.proceed()
    }
  }
}