diff options
author | Diego <diegolparra@gmail.com> | 2014-11-30 17:24:37 -0300 |
---|---|---|
committer | Diego <diegolparra@gmail.com> | 2014-11-30 17:24:37 -0300 |
commit | 433bd42c21c04a8c9af2400eadabd82e6a524c12 (patch) | |
tree | 58e7bba6507d6935283e68e8eaebc42e296b93a8 /kamon-play/src/main/scala | |
parent | d87efc71d82220a4d6f5dba50a7424a5360ea63b (diff) | |
download | Kamon-433bd42c21c04a8c9af2400eadabd82e6a524c12.tar.gz Kamon-433bd42c21c04a8c9af2400eadabd82e6a524c12.tar.bz2 Kamon-433bd42c21c04a8c9af2400eadabd82e6a524c12.zip |
+ play, spray, newrelic: store in TraceLocal useful data to diagnose errors and closes #6
Diffstat (limited to 'kamon-play/src/main/scala')
-rw-r--r-- | kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala | 31 |
1 files changed, 21 insertions, 10 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 989ef43e..527e0a9e 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -17,7 +17,8 @@ package kamon.play.instrumentation import kamon.Kamon import kamon.play.{ Play, PlayExtension } -import kamon.trace.{ TraceContextAware, TraceRecorder } +import kamon.trace.TraceLocal.{ HttpContextKey, HttpContext } +import kamon.trace.{ TraceLocal, TraceContextAware, TraceRecorder } import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation._ import play.api.Routes @@ -28,7 +29,7 @@ import play.libs.Akka @Aspect class RequestInstrumentation { - import kamon.play.instrumentation.RequestInstrumentation._ + import RequestInstrumentation._ @DeclareMixin("play.api.mvc.RequestHeader+") def mixinContextAwareNewRequest: TraceContextAware = TraceContextAware.default @@ -58,23 +59,22 @@ class RequestInstrumentation { val executor = Kamon(Play)(Akka.system()).defaultDispatcher def onResult(result: Result): Result = { - TraceRecorder.withTraceContextAndSystem { (ctx, system) ⇒ ctx.finish() val playExtension = Kamon(Play)(system) recordHttpServerMetrics(result.header, ctx.name, playExtension) - if (playExtension.includeTraceToken) - result.withHeaders(playExtension.traceTokenHeaderName -> ctx.token) - else - result + if (playExtension.includeTraceToken) result.withHeaders(playExtension.traceTokenHeaderName -> ctx.token) + else result - } getOrElse (result) + } getOrElse result } + //store in TraceLocal useful data to diagnose errors + storeDiagnosticData(requestHeader) //override the current trace name - normaliseTraceName(requestHeader).map(TraceRecorder.rename(_)) + normaliseTraceName(requestHeader).map(TraceRecorder.rename) // Invoke the action next(requestHeader).map(onResult)(executor) @@ -87,8 +87,15 @@ class RequestInstrumentation { recordHttpServerMetrics(InternalServerError.header, ctx.name, Kamon(Play)(system)) } - private def recordHttpServerMetrics(header: ResponseHeader, traceName: String, playExtension: PlayExtension): Unit = + def recordHttpServerMetrics(header: ResponseHeader, traceName: String, playExtension: PlayExtension): Unit = playExtension.httpServerMetrics.recordResponse(traceName, header.status.toString) + + def storeDiagnosticData(request: RequestHeader): Unit = { + val agent = request.headers.get(UserAgent).getOrElse(Unknown) + val forwarded = request.headers.get(XForwardedFor).getOrElse(Unknown) + + TraceLocal.store(HttpContextKey)(HttpContext(agent, request.uri, forwarded)) + } } object RequestInstrumentation { @@ -96,6 +103,10 @@ object RequestInstrumentation { import java.util.Locale import scala.collection.concurrent.TrieMap + val UserAgent = "User-Agent" + val XForwardedFor = "X-Forwarded-For" + val Unknown = "unknown" + private val cache = TrieMap.empty[String, String] def normaliseTraceName(requestHeader: RequestHeader): Option[String] = requestHeader.tags.get(Routes.ROUTE_VERB).map({ verb ⇒ |