diff options
4 files changed, 31 insertions, 35 deletions
diff --git a/kamon-play/src/main/resources/reference.conf b/kamon-play/src/main/resources/reference.conf index 7f6e651f..af43edec 100644 --- a/kamon-play/src/main/resources/reference.conf +++ b/kamon-play/src/main/resources/reference.conf @@ -17,7 +17,7 @@ kamon { # in the request headers. automatic-trace-token-propagation = true - # Fully qualified name of the implementation of kamon.play.PlayNameGenerator that will be used for assigning names + # Fully qualified name of the implementation of kamon.play.NameGenerator that will be used for assigning names # to traces and client http segments. name-generator = kamon.play.DefaultNameGenerator diff --git a/kamon-play/src/main/scala/kamon/play/Play.scala b/kamon-play/src/main/scala/kamon/play/Play.scala index 072ea436..d9fdfea8 100644 --- a/kamon-play/src/main/scala/kamon/play/Play.scala +++ b/kamon-play/src/main/scala/kamon/play/Play.scala @@ -20,7 +20,6 @@ import akka.actor.{ ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProv import akka.event.Logging import kamon.Kamon import kamon.http.HttpServerMetrics -import kamon.metric.Entity import play.api.libs.ws.WSRequest import play.api.mvc.RequestHeader @@ -54,6 +53,29 @@ trait NameGenerator { } class DefaultNameGenerator extends NameGenerator { - def generateTraceName(requestHeader: RequestHeader): String = s"${requestHeader.method}: ${requestHeader.uri}" + import scala.collection.concurrent.TrieMap + import play.api.Routes + import java.util.Locale + import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax + + private val cache = TrieMap.empty[String, String] + private val normalizePattern = """\$([^<]+)<[^>]+>""".r + + def generateTraceName(requestHeader: RequestHeader): String = requestHeader.tags.get(Routes.ROUTE_VERB).map { verb ⇒ + val path = requestHeader.tags(Routes.ROUTE_PATTERN) + cache.atomicGetOrElseUpdate(s"$verb$path", { + val traceName = { + // Convert paths of form GET /foo/bar/$paramname<regexp>/blah to foo.bar.paramname.blah.get + val p = normalizePattern.replaceAllIn(path, "$1").replace('/', '.').dropWhile(_ == '.') + val normalisedPath = { + if (p.lastOption.filter(_ != '.').isDefined) s"$p." + else p + } + s"$normalisedPath${verb.toLowerCase(Locale.ENGLISH)}" + } + traceName + }) + } getOrElse s"${requestHeader.method}: ${requestHeader.uri}" + def generateHttpClientSegmentName(request: WSRequest): String = request.url } 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 200fe135..7dcb7913 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -1,5 +1,5 @@ /* ========================================================================================= - * Copyright © 2013-2014 the kamon project <http://kamon.io/> + * Copyright © 2013-2015 the kamon project <http://kamon.io/> * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at @@ -17,12 +17,11 @@ package kamon.play.instrumentation import kamon.Kamon import kamon.play.Play -import kamon.trace.TraceLocal.{ HttpContextKey, HttpContext } +import kamon.trace.TraceLocal.{ HttpContext, HttpContextKey } import kamon.trace._ import kamon.util.SameThreadExecutionContext import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation._ -import play.api.Routes import play.api.mvc.Results._ import play.api.mvc._ @@ -40,13 +39,11 @@ class RequestInstrumentation { val playExtension = Kamon(Play) - val defaultTraceName = playExtension.generateTraceName(requestHeader) val token = if (playExtension.includeTraceToken) { - requestHeader.headers.toSimpleMap.find(_._1 == playExtension.traceTokenHeaderName).map(_._2) + requestHeader.headers.get(playExtension.traceTokenHeaderName) } else None - val newContext = tracer.newContext(defaultTraceName, token) - Tracer.setCurrentContext(newContext) + Tracer.setCurrentContext(tracer.newContext("UnnamedTrace", token)) } @Around("call(* play.api.GlobalSettings.doFilter(*)) && args(next)") @@ -70,7 +67,7 @@ class RequestInstrumentation { storeDiagnosticData(requestHeader) //override the current trace name - normaliseTraceName(requestHeader).map(Tracer.currentContext.rename) + Tracer.currentContext.rename(playExtension.generateTraceName(requestHeader)) // Invoke the action next(requestHeader).map(onResult)(SameThreadExecutionContext) @@ -98,31 +95,7 @@ class RequestInstrumentation { } object RequestInstrumentation { - import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax - 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] - - private val normalizePattern = """\$([^<]+)<[^>]+>""".r - - def normaliseTraceName(requestHeader: RequestHeader): Option[String] = requestHeader.tags.get(Routes.ROUTE_VERB).map({ verb ⇒ - val path = requestHeader.tags(Routes.ROUTE_PATTERN) - cache.atomicGetOrElseUpdate(s"$verb$path", { - val traceName = { - // Convert paths of form GET /foo/bar/$paramname<regexp>/blah to foo.bar.paramname.blah.get - val p = normalizePattern.replaceAllIn(path, "$1").replace('/', '.').dropWhile(_ == '.') - val normalisedPath = { - if (p.lastOption.filter(_ != '.').isDefined) s"$p." - else p - } - s"$normalisedPath${verb.toLowerCase(Locale.ENGLISH)}" - } - traceName - }) - }) } diff --git a/kamon-play/src/test/resources/conf/application.conf b/kamon-play/src/test/resources/conf/application.conf index b214edc0..b927087c 100644 --- a/kamon-play/src/test/resources/conf/application.conf +++ b/kamon-play/src/test/resources/conf/application.conf @@ -2,6 +2,7 @@ kamon { play { include-trace-token-header = true trace-token-header-name = "X-Trace-Token" + name-generator = kamon.play.DefaultNameGenerator } } |