diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2015-01-12 01:45:27 +0100 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2015-01-24 23:19:01 +0100 |
commit | 01a34f67ff75419c440f2e69c0a0db888a670a34 (patch) | |
tree | 9c4dee4e9c13c26937356950f9e4927c3f9dfb7d /kamon-play/src/main/scala | |
parent | 4a47e92d23af371f1d50b40af6cbe00a5ffc0105 (diff) | |
download | Kamon-01a34f67ff75419c440f2e69c0a0db888a670a34.tar.gz Kamon-01a34f67ff75419c440f2e69c0a0db888a670a34.tar.bz2 Kamon-01a34f67ff75419c440f2e69c0a0db888a670a34.zip |
! all: improve the metric recorders infrastructure
Diffstat (limited to 'kamon-play/src/main/scala')
4 files changed, 26 insertions, 20 deletions
diff --git a/kamon-play/src/main/scala/kamon/play/Play.scala b/kamon-play/src/main/scala/kamon/play/Play.scala index 6c6cbf4c..7ca81028 100644 --- a/kamon-play/src/main/scala/kamon/play/Play.scala +++ b/kamon-play/src/main/scala/kamon/play/Play.scala @@ -20,7 +20,7 @@ import akka.actor.{ ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProv import akka.event.Logging import kamon.Kamon import kamon.http.HttpServerMetrics -import kamon.metric.Metrics +import kamon.metric.{ Entity, Metrics } import play.api.libs.ws.WSRequest import play.api.mvc.RequestHeader @@ -36,8 +36,14 @@ class PlayExtension(private val system: ExtendedActorSystem) extends Kamon.Exten log.info(s"Starting the Kamon(Play) extension") private val config = system.settings.config.getConfig("kamon.play") + val httpServerMetrics = { + val metricsExtension = Metrics.get(system) + val factory = metricsExtension.instrumentFactory(HttpServerMetrics.category) + val entity = Entity("play-server", HttpServerMetrics.category) + + Metrics.get(system).register(entity, new HttpServerMetrics(factory)).recorder + } - val httpServerMetrics = Kamon(Metrics)(system).register(HttpServerMetrics, HttpServerMetrics.Factory).get val defaultDispatcher = system.dispatchers.lookup(config.getString("dispatcher")) val includeTraceToken: Boolean = config.getBoolean("automatic-trace-token-propagation") val traceTokenHeaderName: String = config.getString("trace-token-header-name") diff --git a/kamon-play/src/main/scala/kamon/play/action/KamonTraceActions.scala b/kamon-play/src/main/scala/kamon/play/action/KamonTraceActions.scala index 36eabf8e..715cac5e 100644 --- a/kamon-play/src/main/scala/kamon/play/action/KamonTraceActions.scala +++ b/kamon-play/src/main/scala/kamon/play/action/KamonTraceActions.scala @@ -16,13 +16,13 @@ package kamon.play.action -import kamon.trace.TraceRecorder +import kamon.trace.TraceContext import play.api.mvc._ import scala.concurrent.Future case class TraceName[A](name: String)(action: Action[A]) extends Action[A] { def apply(request: Request[A]): Future[Result] = { - TraceRecorder.rename(name) + TraceContext.currentContext.rename(name) action(request) } lazy val parser = action.parser 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 1bafa8ff..38f499b4 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/RequestInstrumentation.scala @@ -18,7 +18,7 @@ package kamon.play.instrumentation import kamon.Kamon import kamon.play.{ Play, PlayExtension } import kamon.trace.TraceLocal.{ HttpContextKey, HttpContext } -import kamon.trace.{ TraceLocal, TraceContextAware, TraceRecorder } +import kamon.trace._ import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation._ import play.api.Routes @@ -41,15 +41,17 @@ class RequestInstrumentation { @Before("call(* play.api.GlobalSettings.onRouteRequest(..)) && args(requestHeader)") def beforeRouteRequest(requestHeader: RequestHeader): Unit = { - val system = Akka.system() - val playExtension = Kamon(Play)(system) - val defaultTraceName = playExtension.generateTraceName(requestHeader) + implicit val system = Akka.system() + val playExtension = Kamon(Play) + val tracer = Kamon(Tracer) + val defaultTraceName = playExtension.generateTraceName(requestHeader) val token = if (playExtension.includeTraceToken) { requestHeader.headers.toSimpleMap.find(_._1 == playExtension.traceTokenHeaderName).map(_._2) } else None - TraceRecorder.start(defaultTraceName, token)(system) + val newContext = token.map(t ⇒ tracer.newContext(defaultTraceName, t)).getOrElse(tracer.newContext(defaultTraceName)) + TraceContext.setCurrentContext(newContext) } @Around("call(* play.api.GlobalSettings.doFilter(*)) && args(next)") @@ -59,11 +61,10 @@ class RequestInstrumentation { val executor = Kamon(Play)(Akka.system()).defaultDispatcher def onResult(result: Result): Result = { - TraceRecorder.withTraceContextAndSystem { (ctx, system) ⇒ + TraceContext.map { ctx ⇒ ctx.finish() - val playExtension = Kamon(Play)(system) - + val playExtension = ctx.lookupExtension(Play) recordHttpServerMetrics(result.header, ctx.name, playExtension) if (playExtension.includeTraceToken) result.withHeaders(playExtension.traceTokenHeaderName -> ctx.token) @@ -75,7 +76,7 @@ class RequestInstrumentation { storeDiagnosticData(requestHeader) //override the current trace name - normaliseTraceName(requestHeader).map(TraceRecorder.rename) + normaliseTraceName(requestHeader).map(TraceContext.currentContext.rename) // Invoke the action next(requestHeader).map(onResult)(executor) @@ -85,8 +86,8 @@ class RequestInstrumentation { @Before("call(* play.api.GlobalSettings.onError(..)) && args(request, ex)") def beforeOnError(request: TraceContextAware, ex: Throwable): Unit = { - TraceRecorder.withTraceContextAndSystem { (ctx, system) ⇒ - recordHttpServerMetrics(InternalServerError.header, ctx.name, Kamon(Play)(system)) + TraceContext.map { ctx ⇒ + recordHttpServerMetrics(InternalServerError.header, ctx.name, ctx.lookupExtension(Play)) } } @@ -102,7 +103,7 @@ class RequestInstrumentation { } object RequestInstrumentation { - import kamon.metric.Metrics.AtomicGetOrElseUpdateForTriemap + import kamon.util.TriemapAtomicGetOrElseUpdate.Syntax import java.util.Locale import scala.collection.concurrent.TrieMap 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 fca13c4c..fc58f9da 100644 --- a/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala +++ b/kamon-play/src/main/scala/kamon/play/instrumentation/WSInstrumentation.scala @@ -16,9 +16,8 @@ package kamon.play.instrumentation -import kamon.Kamon import kamon.play.Play -import kamon.trace.{ SegmentCategory, TraceRecorder } +import kamon.trace.{ TraceContext, SegmentCategory } import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation.{ Around, Aspect, Pointcut } import play.api.libs.ws.{ WSRequest, WSResponse } @@ -33,8 +32,8 @@ class WSInstrumentation { @Around("onExecuteRequest(request)") def aroundExecuteRequest(pjp: ProceedingJoinPoint, request: WSRequest): Any = { - TraceRecorder.withTraceContextAndSystem { (ctx, system) ⇒ - val playExtension = Kamon(Play)(system) + TraceContext.map { ctx ⇒ + val playExtension = ctx.lookupExtension(Play) val executor = playExtension.defaultDispatcher val segmentName = playExtension.generateHttpClientSegmentName(request) val segment = ctx.startSegment(segmentName, SegmentCategory.HttpClient, Play.SegmentLibraryName) |