From 959ce3573253ec4ac5b837d8a9c9e70f1f80bd6b Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Thu, 5 Mar 2015 23:39:44 +0100 Subject: ! all: introduced support for metric tags. --- kamon-play/src/main/scala/kamon/play/Play.scala | 8 +---- .../scala/kamon/play/WSInstrumentationSpec.scala | 38 ++++++++++++++-------- 2 files changed, 25 insertions(+), 21 deletions(-) (limited to 'kamon-play/src') diff --git a/kamon-play/src/main/scala/kamon/play/Play.scala b/kamon-play/src/main/scala/kamon/play/Play.scala index 9e160d58..270d244f 100644 --- a/kamon-play/src/main/scala/kamon/play/Play.scala +++ b/kamon-play/src/main/scala/kamon/play/Play.scala @@ -36,13 +36,7 @@ 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 = Kamon.metrics - val factory = metricsExtension.instrumentFactory(HttpServerMetrics.category) - val entity = Entity("play-server", HttpServerMetrics.category) - - metricsExtension.register(entity, new HttpServerMetrics(factory)).recorder - } + val httpServerMetrics = Kamon.metrics.entity(HttpServerMetrics, "play-server") val defaultDispatcher = system.dispatcher val includeTraceToken: Boolean = config.getBoolean("automatic-trace-token-propagation") diff --git a/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala b/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala index d639fd9f..fe16ccbf 100644 --- a/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala +++ b/kamon-play/src/test/scala/kamon/play/WSInstrumentationSpec.scala @@ -17,7 +17,7 @@ package kamon.play import kamon.Kamon -import kamon.metric.{ EntitySnapshot, TraceMetrics } +import kamon.metric.{ Entity, EntitySnapshot, TraceMetrics } import kamon.trace.{ Tracer, TraceContext, SegmentCategory } import org.scalatest.{ Matchers, WordSpecLike } import org.scalatestplus.play.OneServerPerSuite @@ -32,7 +32,7 @@ import scala.concurrent.duration._ class WSInstrumentationSpec extends WordSpecLike with Matchers with OneServerPerSuite { Kamon.start() - import kamon.metric.TraceMetricsSpec.SegmentSyntax + System.setProperty("config.file", "./kamon-play/src/test/resources/conf/application.conf") override lazy val port: Port = 19003 @@ -46,10 +46,16 @@ class WSInstrumentationSpec extends WordSpecLike with Matchers with OneServerPer "propagate the TraceContext inside an Action and complete the WS request" in { Await.result(route(FakeRequest(GET, "/inside")).get, 10 seconds) - val snapshot = takeSnapshotOf("GET: /inside") + val snapshot = takeSnapshotOf("GET: /inside", "trace") snapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) - snapshot.segments.size should be(1) - snapshot.segment(s"http://localhost:$port/async", SegmentCategory.HttpClient, Play.SegmentLibraryName).numberOfMeasurements should be(1) + + val segmentMetricsSnapshot = takeSnapshotOf(s"http://localhost:$port/async", "trace-segment", + tags = Map( + "trace" -> "GET: /inside", + "category" -> SegmentCategory.HttpClient, + "library" -> Play.SegmentLibraryName)) + + segmentMetricsSnapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) } "propagate the TraceContext outside an Action and complete the WS request" in { @@ -58,22 +64,26 @@ class WSInstrumentationSpec extends WordSpecLike with Matchers with OneServerPer Tracer.currentContext.finish() } - val snapshot = takeSnapshotOf("trace-outside-action") + val snapshot = takeSnapshotOf("trace-outside-action", "trace") snapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) - snapshot.segments.size should be(1) - snapshot.segment(s"http://localhost:$port/outside", SegmentCategory.HttpClient, Play.SegmentLibraryName).numberOfMeasurements should be(1) - } + val segmentMetricsSnapshot = takeSnapshotOf(s"http://localhost:$port/outside", "trace-segment", + tags = Map( + "trace" -> "trace-outside-action", + "category" -> SegmentCategory.HttpClient, + "library" -> Play.SegmentLibraryName)) + + segmentMetricsSnapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) + } } + lazy val collectionContext = Kamon.metrics.buildDefaultCollectionContext + def newContext(name: String): TraceContext = Kamon.tracer.newContext(name) - def takeSnapshotOf(traceName: String): EntitySnapshot = { - // Give some time for async segments to finish. - Thread.sleep(300) - val recorder = Kamon.metrics.register(TraceMetrics, traceName).get.recorder - val collectionContext = Kamon.metrics.buildDefaultCollectionContext + def takeSnapshotOf(name: String, category: String, tags: Map[String, String] = Map.empty): EntitySnapshot = { + val recorder = Kamon.metrics.find(Entity(name, category, tags)).get recorder.collect(collectionContext) } -- cgit v1.2.3