diff options
author | Diego <diegolparra@gmail.com> | 2014-06-16 15:52:14 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2014-06-16 15:52:14 -0300 |
commit | 4abab8df49d1bc5d9a051a8b54852e0712be7b74 (patch) | |
tree | 3e8be92dd0b458c388cfae54da1f6318a9d68f0e /kamon-play/src/main/scala/kamon/play | |
parent | e3d16846beb4ce0a629a79430d8742f369a73a32 (diff) | |
download | Kamon-4abab8df49d1bc5d9a051a8b54852e0712be7b74.tar.gz Kamon-4abab8df49d1bc5d9a051a8b54852e0712be7b74.tar.bz2 Kamon-4abab8df49d1bc5d9a051a8b54852e0712be7b74.zip |
+ play: refactor in RequestInstrumentation in order to propagate the TraceContext through the filters and all actions in the incoming request
Diffstat (limited to 'kamon-play/src/main/scala/kamon/play')
-rw-r--r-- | kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala | 91 |
1 files changed, 38 insertions, 53 deletions
diff --git a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala index af11a07a..00170b1b 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -16,42 +16,62 @@ package kamon.play.instrumentation -import kamon.trace.{ TraceRecorder, TraceContextAware } import kamon.Kamon import kamon.play.Play -import play.libs.Akka -import play.api.mvc._ -import akka.actor.ActorSystem +import kamon.trace.{ TraceContextAware, TraceRecorder } import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation._ -import scala.Some +import play.api.mvc._ +import play.libs.Akka @Aspect class RequestInstrumentation { - @DeclareMixin("play.api.mvc.RequestHeader$$anon$4") + @DeclareMixin("play.api.mvc.RequestHeader+") def mixinContextAwareNewRequest: TraceContextAware = TraceContextAware.default - @Pointcut("execution(* play.api.GlobalSettings+.onStart(*)) && args(application)") - def onStart(application: play.api.Application): Unit = {} - - @After("onStart(application)") + @After("execution(* play.api.GlobalSettings+.onStart(*)) && args(application)") def afterApplicationStart(application: play.api.Application): Unit = { Kamon(Play)(Akka.system()) } - @Pointcut("execution(* play.api.GlobalSettings+.doFilter(*)) && args(next)") - def doFilter(next: EssentialAction): Unit = {} + @Before("execution(* play.api.GlobalSettings+.onRouteRequest(..)) && args(requestHeader)") + def onRouteRequest(requestHeader: RequestHeader): Unit = { + val system = Akka.system() + val playExtension = Kamon(Play)(system) + val defaultTraceName: String = s"${requestHeader.method}: ${requestHeader.uri}" - @Around("doFilter(next)") - def afterDoFilter(pjp: ProceedingJoinPoint, next: EssentialAction): Any = { - Filters(pjp.proceed(Array(next)).asInstanceOf[EssentialAction], kamonRequestFilter) + val token = if (playExtension.includeTraceToken) { + requestHeader.headers.toSimpleMap.find(_._1 == playExtension.traceTokenHeaderName).map(_._2) + } else None + + TraceRecorder.start(defaultTraceName, token)(system) } - @Pointcut("execution(* play.api.GlobalSettings+.onError(..)) && args(request, ex)") - def onError(request: TraceContextAware, ex: Throwable): Unit = {} + @Around("execution(* play.api.GlobalSettings+.doFilter(*)) && args(next)") + def afterDoFilter(pjp: ProceedingJoinPoint, next: EssentialAction): Any = { + val essentialAction = (requestHeader: RequestHeader) ⇒ { + + val incomingContext = TraceRecorder.currentContext + val executor = Kamon(Play)(Akka.system()).defaultDispatcher + + next(requestHeader).map { + result ⇒ + TraceRecorder.finish() + incomingContext match { + case None ⇒ result + case Some(traceContext) ⇒ + val playExtension = Kamon(Play)(traceContext.system) + if (playExtension.includeTraceToken) { + result.withHeaders(playExtension.traceTokenHeaderName -> traceContext.token) + } else result + } + }(executor) + } + pjp.proceed(Array(EssentialAction(essentialAction))) + } - @Around("onError(request, ex)") + @Around("execution(* play.api.GlobalSettings+.onError(..)) && args(request, ex)") def aroundOnError(pjp: ProceedingJoinPoint, request: TraceContextAware, ex: Throwable): Any = request.traceContext match { case None ⇒ pjp.proceed() case Some(ctx) ⇒ { @@ -60,39 +80,4 @@ class RequestInstrumentation { pjp.proceed() } } - - private[this] val kamonRequestFilter = Filter { (nextFilter, requestHeader) ⇒ - - processRequest(requestHeader) - - val incomingContext = TraceRecorder.currentContext - val executor = Kamon(Play)(Akka.system()).defaultDispatcher - - nextFilter(requestHeader).map { result ⇒ - - TraceRecorder.finish() - - val simpleResult = incomingContext match { - case None ⇒ result - case Some(traceContext) ⇒ - val playExtension = Kamon(Play)(traceContext.system) - if (playExtension.includeTraceToken) { - result.withHeaders(playExtension.traceTokenHeaderName -> traceContext.token) - } else result - } - simpleResult - }(executor) - } - - private[this] def processRequest(requestHeader: RequestHeader): Unit = { - val system: ActorSystem = Akka.system() - val playExtension = Kamon(Play)(system) - val defaultTraceName: String = s"${requestHeader.method}: ${requestHeader.uri}" - - val token = if (playExtension.includeTraceToken) { - requestHeader.headers.toSimpleMap.find(_._1 == playExtension.traceTokenHeaderName).map(_._2) - } else None - - TraceRecorder.start(defaultTraceName, token)(system) - } } |