aboutsummaryrefslogblamecommitdiff
path: root/kamon-trace/src/main/scala/kamon/trace/instrumentation/RunnableTracing.scala
blob: 3e5a7cceb140cc1bc0ff9d8b501c94cf454cb31e (plain) (tree)
1
2
3
4
5
6
7
8
                                   

                                    
                                           
                                        
 
       
                       





                                                                                                                   
                                                                                              
                                                                     
   





               

                                                                                                    
 

                                                                                                    
 

 
                                                  
                                                                   

                                         


   
                                        
                                                                                    
                
 
                                                         

               


   
 





                                                                                               
package kamon.trace.instrumentation

import org.aspectj.lang.annotation._
import org.aspectj.lang.ProceedingJoinPoint
import kamon.trace.{TraceContext, Trace}

@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] = Trace.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._

    Trace.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]
}