diff options
author | Diego <diegolparra@gmail.com> | 2014-09-09 00:58:03 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2014-09-09 01:05:42 -0300 |
commit | 6884c0f3f7bf9376e9eaf4f330d7622c142399e3 (patch) | |
tree | 5953082053452a644b0f3323f4216c1808ee3ccb /kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala | |
parent | 23785a41dc3a0e9651ba87bc9dc255932ea64bd6 (diff) | |
download | Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.tar.gz Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.tar.bz2 Kamon-6884c0f3f7bf9376e9eaf4f330d7622c142399e3.zip |
= play:
* remove from publishErrorMessage method
* refactor onError method in RequestInstrumentation
* refactor WSInstrumentation in order to propagate the TraceContext when a WS call is executed outside an Action
* improve tests
* closes #33
Diffstat (limited to 'kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala')
-rw-r--r-- | kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala b/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala index 2862ba19..3e4d6110 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala @@ -16,15 +16,16 @@ package kamon.play.instrumentation -import org.aspectj.lang.annotation.{ Around, Pointcut, Aspect } -import org.aspectj.lang.ProceedingJoinPoint -import kamon.trace.TraceRecorder import kamon.metric.TraceMetrics.HttpClientRequest -import play.api.libs.ws.WSRequest -import scala.concurrent.Future -import play.api.libs.ws.WSResponse -import scala.util.{ Failure, Success } +import kamon.trace.{ TraceContext, TraceRecorder } +import org.aspectj.lang.ProceedingJoinPoint +import org.aspectj.lang.annotation.{ Around, Aspect, Pointcut } +import play.api.libs.ws.ning.NingWSRequest +import play.api.libs.ws.{ WSRequest, WSResponse } +import play.libs.Akka + import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future @Aspect class WSInstrumentation { @@ -34,27 +35,36 @@ class WSInstrumentation { @Around("onExecuteRequest(request)") def aroundExecuteRequest(pjp: ProceedingJoinPoint, request: WSRequest): Any = { - import WSInstrumentation._ - val completionHandle = TraceRecorder.startSegment(HttpClientRequest(request.url), basicRequestAttributes(request)) + import kamon.play.instrumentation.WSInstrumentation._ - val response = pjp.proceed().asInstanceOf[Future[WSResponse]] + withOrNewTraceContext(TraceRecorder.currentContext)(request) { + val response = pjp.proceed().asInstanceOf[Future[WSResponse]] + val segmentHandle = TraceRecorder.startSegment(HttpClientRequest(request.url), basicRequestAttributes(request)) - response.onComplete { - case Failure(t) ⇒ completionHandle.map(_.finish(Map("completed-with-error" -> t.getMessage))) - case Success(_) ⇒ completionHandle.map(_.finish(Map.empty)) + response.map { + r ⇒ + segmentHandle.map(_.finish()) + TraceRecorder.finish() + } + response } - - response } } object WSInstrumentation { + def uri(request: WSRequest): java.net.URI = request.asInstanceOf[NingWSRequest].builder.build().getURI + def basicRequestAttributes(request: WSRequest): Map[String, String] = { Map[String, String]( - "host" -> request.header("host").getOrElse("Unknown"), - "path" -> request.method) + "host" -> uri(request).getHost, + "path" -> uri(request).getPath, + "method" -> request.method) } -} + def withOrNewTraceContext[T](context: Option[TraceContext])(request: WSRequest)(thunk: ⇒ T): T = { + if (context.isDefined) TraceRecorder.withTraceContext(context) { thunk } + else TraceRecorder.withNewTraceContext(request.url, metadata = basicRequestAttributes(request)) { thunk }(Akka.system()) + } +}
\ No newline at end of file |