From 01a34f67ff75419c440f2e69c0a0db888a670a34 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Mon, 12 Jan 2015 01:45:27 +0100 Subject: ! all: improve the metric recorders infrastructure --- kamon-core/src/test/resources/logback.xml | 24 +- .../scala/FutureInstrumentationSpec.scala | 63 ----- .../scalaz/FutureInstrumentationSpec.scala | 63 ----- .../kamon/metric/SubscriptionsProtocolSpec.scala | 112 ++++---- .../metric/TickMetricSnapshotBufferSpec.scala | 65 +++-- .../test/scala/kamon/metric/TraceMetricsSpec.scala | 107 ++++---- .../test/scala/kamon/metric/UserMetricsSpec.scala | 296 ++++----------------- .../kamon/metric/instrument/CounterSpec.scala | 1 - .../scala/kamon/metric/instrument/GaugeSpec.scala | 66 ++--- .../kamon/metric/instrument/HistogramSpec.scala | 34 +-- .../metric/instrument/MinMaxCounterSpec.scala | 22 +- .../test/scala/kamon/testkit/BaseKamonSpec.scala | 34 +++ .../test/scala/kamon/trace/SimpleTraceSpec.scala | 76 ++---- .../kamon/trace/TraceContextManipulationSpec.scala | 94 +++---- .../test/scala/kamon/trace/TraceLocalSpec.scala | 23 +- .../test/scala/kamon/util/GlobPathFilterSpec.scala | 9 +- 16 files changed, 357 insertions(+), 732 deletions(-) delete mode 100644 kamon-core/src/test/scala/kamon/instrumentation/scala/FutureInstrumentationSpec.scala delete mode 100644 kamon-core/src/test/scala/kamon/instrumentation/scalaz/FutureInstrumentationSpec.scala create mode 100644 kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala (limited to 'kamon-core/src/test') diff --git a/kamon-core/src/test/resources/logback.xml b/kamon-core/src/test/resources/logback.xml index eb578346..dd623d61 100644 --- a/kamon-core/src/test/resources/logback.xml +++ b/kamon-core/src/test/resources/logback.xml @@ -1,17 +1,17 @@ + + true + - - true - + - - - %date{HH:mm:ss.SSS} %-5level [%X{uow}][%X{requestId}] [%thread] %logger{55} - %msg%n - - - - - - + + + %date{HH:mm:ss.SSS} %-5level [%traceToken][%thread] %logger{55} - %msg%n + + + + + diff --git a/kamon-core/src/test/scala/kamon/instrumentation/scala/FutureInstrumentationSpec.scala b/kamon-core/src/test/scala/kamon/instrumentation/scala/FutureInstrumentationSpec.scala deleted file mode 100644 index 31afd3ff..00000000 --- a/kamon-core/src/test/scala/kamon/instrumentation/scala/FutureInstrumentationSpec.scala +++ /dev/null @@ -1,63 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================================================== */ -package kamon.instrumentation.scala - -import akka.actor.ActorSystem -import akka.testkit.TestKit -import kamon.trace.TraceRecorder -import org.scalatest.concurrent.{ PatienceConfiguration, ScalaFutures } -import org.scalatest.{ Matchers, OptionValues, WordSpecLike } - -import scala.concurrent.Future - -class FutureInstrumentationSpec extends TestKit(ActorSystem("future-instrumentation-spec")) with WordSpecLike with Matchers - with ScalaFutures with PatienceConfiguration with OptionValues { - - implicit val execContext = system.dispatcher - - "a Future created with FutureTracing" should { - "capture the TraceContext available when created" which { - "must be available when executing the future's body" in { - - val (future, testTraceContext) = TraceRecorder.withNewTraceContext("future-body") { - val future = Future(TraceRecorder.currentContext) - - (future, TraceRecorder.currentContext) - } - - whenReady(future)(ctxInFuture ⇒ - ctxInFuture should equal(testTraceContext)) - } - - "must be available when executing callbacks on the future" in { - - val (future, testTraceContext) = TraceRecorder.withNewTraceContext("future-body") { - val future = Future("Hello Kamon!") - // The TraceContext is expected to be available during all intermediate processing. - .map(_.length) - .flatMap(len ⇒ Future(len.toString)) - .map(s ⇒ TraceRecorder.currentContext) - - (future, TraceRecorder.currentContext) - } - - whenReady(future)(ctxInFuture ⇒ - ctxInFuture should equal(testTraceContext)) - } - } - } -} - diff --git a/kamon-core/src/test/scala/kamon/instrumentation/scalaz/FutureInstrumentationSpec.scala b/kamon-core/src/test/scala/kamon/instrumentation/scalaz/FutureInstrumentationSpec.scala deleted file mode 100644 index 29bf96f8..00000000 --- a/kamon-core/src/test/scala/kamon/instrumentation/scalaz/FutureInstrumentationSpec.scala +++ /dev/null @@ -1,63 +0,0 @@ -/* =================================================== - * Copyright © 2013 the kamon project - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================================================== */ -package kamon.instrumentation.scalaz - -import akka.actor.ActorSystem -import akka.testkit.TestKit -import kamon.trace.TraceRecorder -import org.scalatest.concurrent.{ PatienceConfiguration, ScalaFutures } -import org.scalatest.{ Matchers, OptionValues, WordSpecLike } -import scalaz.concurrent.Future -import java.util.concurrent.Executors - -class FutureInstrumentationSpec extends TestKit(ActorSystem("future-instrumentation-spec")) with WordSpecLike with Matchers - with ScalaFutures with PatienceConfiguration with OptionValues { - - implicit val execContext = Executors.newCachedThreadPool() - - "a Future created with FutureTracing" should { - "capture the TraceContext available when created" which { - "must be available when executing the future's body" in { - - val (future, testTraceContext) = TraceRecorder.withNewTraceContext("future-body") { - val future = Future(TraceRecorder.currentContext).start - - (future, TraceRecorder.currentContext) - } - - val ctxInFuture = future.run - ctxInFuture should equal(testTraceContext) - } - - "must be available when executing callbacks on the future" in { - - val (future, testTraceContext) = TraceRecorder.withNewTraceContext("future-body") { - val future = Future("Hello Kamon!") - // The TraceContext is expected to be available during all intermediate processing. - .map(_.length) - .flatMap(len ⇒ Future(len.toString)) - .map(s ⇒ TraceRecorder.currentContext) - - (future.start, TraceRecorder.currentContext) - } - - val ctxInFuture = future.run - ctxInFuture should equal(testTraceContext) - } - } - } -} - diff --git a/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala b/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala index 9144725e..40200685 100644 --- a/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala @@ -1,128 +1,110 @@ package kamon.metric import akka.actor._ -import akka.testkit.{ TestProbe, ImplicitSender, TestKitBase } +import akka.testkit.{ TestProbe, ImplicitSender } import com.typesafe.config.ConfigFactory -import kamon.Kamon -import kamon.metric.Subscriptions.TickMetricSnapshot -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot +import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ -class SubscriptionsProtocolSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("subscriptions-protocol-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - |} - """.stripMargin)) +class SubscriptionsProtocolSpec extends BaseKamonSpec("subscriptions-protocol-spec") with ImplicitSender { + override lazy val config = + ConfigFactory.parseString( + """ + |kamon.metric { + | tick-interval = 1 hour + |} + """.stripMargin) - val metricsExtension = Kamon(Metrics)(system) - import metricsExtension.{ register, subscribe, unsubscribe } + val metricsModule = kamon.metrics + import metricsModule.{ register, subscribe, unsubscribe } "the Subscriptions messaging protocol" should { "allow subscribing for a single tick" in { val subscriber = TestProbe() - register(TraceMetrics("one-shot"), TraceMetrics.Factory) - subscribe(TraceMetrics, "one-shot", subscriber.ref, permanently = false) + register(TraceMetrics, "one-shot") + subscribe("trace", "one-shot", subscriber.ref, permanently = false) - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] tickSnapshot.metrics.size should be(1) - tickSnapshot.metrics.keys should contain(TraceMetrics("one-shot")) + tickSnapshot.metrics.keys should contain(Entity("one-shot", "trace")) - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() subscriber.expectNoMsg(1 second) } "allow subscribing permanently to a metric" in { val subscriber = TestProbe() - register(TraceMetrics("permanent"), TraceMetrics.Factory) - subscribe(TraceMetrics, "permanent", subscriber.ref, permanently = true) + register(TraceMetrics, "permanent") + subscribe("trace", "permanent", subscriber.ref, permanently = true) for (repetition ← 1 to 5) { - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] tickSnapshot.metrics.size should be(1) - tickSnapshot.metrics.keys should contain(TraceMetrics("permanent")) - subscriber.expectNoMsg(1 second) + tickSnapshot.metrics.keys should contain(Entity("permanent", "trace")) } } "allow subscribing to metrics matching a glob pattern" in { val subscriber = TestProbe() - register(TraceMetrics("include-one"), TraceMetrics.Factory) - register(TraceMetrics("exclude-two"), TraceMetrics.Factory) - register(TraceMetrics("include-three"), TraceMetrics.Factory) - subscribe(TraceMetrics, "include-*", subscriber.ref, permanently = true) + register(TraceMetrics, "include-one") + register(TraceMetrics, "exclude-two") + register(TraceMetrics, "include-three") + subscribe("trace", "include-*", subscriber.ref, permanently = true) for (repetition ← 1 to 5) { - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] tickSnapshot.metrics.size should be(2) - tickSnapshot.metrics.keys should contain(TraceMetrics("include-one")) - tickSnapshot.metrics.keys should contain(TraceMetrics("include-three")) - subscriber.expectNoMsg(1 second) + tickSnapshot.metrics.keys should contain(Entity("include-one", "trace")) + tickSnapshot.metrics.keys should contain(Entity("include-three", "trace")) } } "send a single TickMetricSnapshot to each subscriber, even if subscribed multiple times" in { val subscriber = TestProbe() - register(TraceMetrics("include-one"), TraceMetrics.Factory) - register(TraceMetrics("exclude-two"), TraceMetrics.Factory) - register(TraceMetrics("include-three"), TraceMetrics.Factory) - subscribe(TraceMetrics, "include-one", subscriber.ref, permanently = true) - subscribe(TraceMetrics, "include-three", subscriber.ref, permanently = true) + register(TraceMetrics, "include-one") + register(TraceMetrics, "exclude-two") + register(TraceMetrics, "include-three") + subscribe("trace", "include-one", subscriber.ref, permanently = true) + subscribe("trace", "include-three", subscriber.ref, permanently = true) for (repetition ← 1 to 5) { - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] tickSnapshot.metrics.size should be(2) - tickSnapshot.metrics.keys should contain(TraceMetrics("include-one")) - tickSnapshot.metrics.keys should contain(TraceMetrics("include-three")) + tickSnapshot.metrics.keys should contain(Entity("include-one", "trace")) + tickSnapshot.metrics.keys should contain(Entity("include-three", "trace")) } } "allow un-subscribing a subscriber" in { val subscriber = TestProbe() - register(TraceMetrics("one-shot"), TraceMetrics.Factory) - subscribe(TraceMetrics, "one-shot", subscriber.ref, permanently = true) + register(TraceMetrics, "one-shot") + subscribe("trace", "one-shot", subscriber.ref, permanently = true) - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] tickSnapshot.metrics.size should be(1) - tickSnapshot.metrics.keys should contain(TraceMetrics("one-shot")) + tickSnapshot.metrics.keys should contain(Entity("one-shot", "trace")) unsubscribe(subscriber.ref) - metricsExtension.subscriptions ! Subscriptions.FlushMetrics + flushSubscriptions() subscriber.expectNoMsg(1 second) } + } - "watch all subscribers and un-subscribe them if they die" in { - val subscriber = TestProbe() - val forwarderSubscriber = system.actorOf(Props(new ForwarderSubscriber(subscriber.ref))) - watch(forwarderSubscriber) - register(TraceMetrics("one-shot"), TraceMetrics.Factory) - subscribe(TraceMetrics, "one-shot", forwarderSubscriber, permanently = true) - - metricsExtension.subscriptions ! Subscriptions.FlushMetrics - val tickSnapshot = subscriber.expectMsgType[TickMetricSnapshot] - tickSnapshot.metrics.size should be(1) - tickSnapshot.metrics.keys should contain(TraceMetrics("one-shot")) - - forwarderSubscriber ! PoisonPill - expectTerminated(forwarderSubscriber) - - metricsExtension.subscriptions ! Subscriptions.FlushMetrics - metricsExtension.subscriptions ! Subscriptions.FlushMetrics - metricsExtension.subscriptions ! Subscriptions.FlushMetrics - metricsExtension.subscriptions ! Subscriptions.FlushMetrics - subscriber.expectNoMsg(2 seconds) - } + def subscriptionsActor: ActorRef = { + val listener = TestProbe() + system.actorSelection("/user/kamon/kamon-metrics").tell(Identify(1), listener.ref) + listener.expectMsgType[ActorIdentity].ref.get } } diff --git a/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala b/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala index a9197ab5..2e1f246d 100644 --- a/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala @@ -17,32 +17,29 @@ package kamon.metric import com.typesafe.config.ConfigFactory -import kamon.{ MilliTimestamp, Kamon } -import kamon.metric.instrument.Histogram import kamon.metric.instrument.Histogram.MutableRecord -import org.scalatest.{ Matchers, WordSpecLike } -import akka.testkit.{ ImplicitSender, TestKitBase } -import akka.actor.ActorSystem +import kamon.testkit.BaseKamonSpec +import kamon.util.MilliTimestamp +import akka.testkit.ImplicitSender import scala.concurrent.duration._ -import kamon.metric.Subscriptions.TickMetricSnapshot - -class TickMetricSnapshotBufferSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("trace-metrics-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - | default-collection-context-buffer-size = 10 - | - | filters = [ - | { - | trace { - | includes = [ "*" ] - | excludes = [ "non-tracked-trace"] - | } - | } - | ] - |} - """.stripMargin)) +import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot + +class TickMetricSnapshotBufferSpec extends BaseKamonSpec("trace-metrics-spec") with ImplicitSender { + override lazy val config = + ConfigFactory.parseString( + """ + |kamon.metric { + | tick-interval = 1 hour + | default-collection-context-buffer-size = 10 + | + | filters { + | trace { + | includes = [ "*" ] + | excludes = [ "non-tracked-trace" ] + | } + | } + |} + """.stripMargin) "the TickMetricSnapshotBuffer" should { "merge TickMetricSnapshots received until the flush timeout is reached and fix the from/to fields" in new SnapshotFixtures { @@ -74,7 +71,7 @@ class TickMetricSnapshotBufferSpec extends TestKitBase with WordSpecLike with Ma mergedSnapshot.to.millis should equal(4000) mergedSnapshot.metrics should not be ('empty) - val testMetricSnapshot = mergedSnapshot.metrics(testTraceIdentity).metrics(TraceMetrics.ElapsedTime).asInstanceOf[Histogram.Snapshot] + val testMetricSnapshot = mergedSnapshot.metrics(testTraceIdentity).histogram("elapsed-time").get testMetricSnapshot.min should equal(10) testMetricSnapshot.max should equal(300) testMetricSnapshot.numberOfMeasurements should equal(6) @@ -88,23 +85,23 @@ class TickMetricSnapshotBufferSpec extends TestKitBase with WordSpecLike with Ma } trait SnapshotFixtures { - val collectionContext = Kamon(Metrics).buildDefaultCollectionContext - val testTraceIdentity = TraceMetrics("buffer-spec-test-trace") - val traceRecorder = Kamon(Metrics).register(testTraceIdentity, TraceMetrics.Factory).get + val collectionContext = kamon.metrics.buildDefaultCollectionContext + val testTraceIdentity = Entity("buffer-spec-test-trace", "trace") + val traceRecorder = kamon.metrics.register(TraceMetrics, "buffer-spec-test-trace").get.recorder val firstEmpty = TickMetricSnapshot(new MilliTimestamp(1000), new MilliTimestamp(2000), Map.empty) val secondEmpty = TickMetricSnapshot(new MilliTimestamp(2000), new MilliTimestamp(3000), Map.empty) val thirdEmpty = TickMetricSnapshot(new MilliTimestamp(3000), new MilliTimestamp(4000), Map.empty) - traceRecorder.elapsedTime.record(10L) - traceRecorder.elapsedTime.record(20L) - traceRecorder.elapsedTime.record(30L) + traceRecorder.ElapsedTime.record(10L) + traceRecorder.ElapsedTime.record(20L) + traceRecorder.ElapsedTime.record(30L) val firstNonEmpty = TickMetricSnapshot(new MilliTimestamp(1000), new MilliTimestamp(2000), Map( (testTraceIdentity -> traceRecorder.collect(collectionContext)))) - traceRecorder.elapsedTime.record(10L) - traceRecorder.elapsedTime.record(10L) - traceRecorder.elapsedTime.record(300L) + traceRecorder.ElapsedTime.record(10L) + traceRecorder.ElapsedTime.record(10L) + traceRecorder.ElapsedTime.record(300L) val secondNonEmpty = TickMetricSnapshot(new MilliTimestamp(1000), new MilliTimestamp(2000), Map( (testTraceIdentity -> traceRecorder.collect(collectionContext)))) } diff --git a/kamon-core/src/test/scala/kamon/metric/TraceMetricsSpec.scala b/kamon-core/src/test/scala/kamon/metric/TraceMetricsSpec.scala index cd10f2d3..793c0112 100644 --- a/kamon-core/src/test/scala/kamon/metric/TraceMetricsSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/TraceMetricsSpec.scala @@ -1,92 +1,83 @@ package kamon.metric -import akka.actor.ActorSystem -import akka.testkit.{ ImplicitSender, TestKitBase } +import akka.testkit.ImplicitSender import com.typesafe.config.ConfigFactory -import kamon.Kamon -import kamon.metric.TraceMetrics.TraceMetricsSnapshot -import kamon.trace.{ SegmentMetricIdentity, TraceRecorder } -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.testkit.BaseKamonSpec +import kamon.trace.TraceContext +import kamon.metric.instrument.Histogram -class TraceMetricsSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("trace-metrics-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - | default-collection-context-buffer-size = 10 - | - | filters = [ - | { - | trace { - | includes = [ "*" ] - | excludes = [ "non-tracked-trace"] - | } - | } - | ] - | precision { - | default-histogram-precision { - | highest-trackable-value = 3600000000000 - | significant-value-digits = 2 - | } - | - | default-min-max-counter-precision { - | refresh-interval = 1 second - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - """.stripMargin)) +class TraceMetricsSpec extends BaseKamonSpec("trace-metrics-spec") with ImplicitSender { + import TraceMetricsSpec.SegmentSyntax + + override lazy val config = + ConfigFactory.parseString( + """ + |kamon.metric { + | tick-interval = 1 hour + | default-collection-context-buffer-size = 10 + | + | filters { + | trace { + | includes = [ "*" ] + | excludes = [ "non-tracked-trace"] + | } + | } + |} + """.stripMargin) "the TraceMetrics" should { "record the elapsed time between a trace creation and finish" in { for (repetitions ← 1 to 10) { - TraceRecorder.withNewTraceContext("record-elapsed-time") { - TraceRecorder.finish() + TraceContext.withContext(newContext("record-elapsed-time")) { + TraceContext.currentContext.finish() } } - val snapshot = takeSnapshotOf("record-elapsed-time") - snapshot.elapsedTime.numberOfMeasurements should be(10) - snapshot.segments shouldBe empty + val snapshot = takeSnapshotOf("record-elapsed-time", "trace") + snapshot.histogram("elapsed-time").get.numberOfMeasurements should be(10) } "record the elapsed time for segments that occur inside a given trace" in { - TraceRecorder.withNewTraceContext("trace-with-segments") { - val segment = TraceRecorder.currentContext.startSegment("test-segment", "test-category", "test-library") + TraceContext.withContext(newContext("trace-with-segments")) { + val segment = TraceContext.currentContext.startSegment("test-segment", "test-category", "test-library") segment.finish() - TraceRecorder.finish() + TraceContext.currentContext.finish() } - val snapshot = takeSnapshotOf("trace-with-segments") - snapshot.elapsedTime.numberOfMeasurements should be(1) + val snapshot = takeSnapshotOf("trace-with-segments", "trace") + snapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) snapshot.segments.size should be(1) - snapshot.segments(SegmentMetricIdentity("test-segment", "test-category", "test-library")).numberOfMeasurements should be(1) + snapshot.segment("test-segment", "test-category", "test-library").numberOfMeasurements should be(1) } "record the elapsed time for segments that finish after their correspondent trace has finished" in { - val segment = TraceRecorder.withNewTraceContext("closing-segment-after-trace") { - val s = TraceRecorder.currentContext.startSegment("test-segment", "test-category", "test-library") - TraceRecorder.finish() + val segment = TraceContext.withContext(newContext("closing-segment-after-trace")) { + val s = TraceContext.currentContext.startSegment("test-segment", "test-category", "test-library") + TraceContext.currentContext.finish() s } - val beforeFinishSegmentSnapshot = takeSnapshotOf("closing-segment-after-trace") - beforeFinishSegmentSnapshot.elapsedTime.numberOfMeasurements should be(1) + val beforeFinishSegmentSnapshot = takeSnapshotOf("closing-segment-after-trace", "trace") + beforeFinishSegmentSnapshot.histogram("elapsed-time").get.numberOfMeasurements should be(1) beforeFinishSegmentSnapshot.segments.size should be(0) segment.finish() - val afterFinishSegmentSnapshot = takeSnapshotOf("closing-segment-after-trace") - afterFinishSegmentSnapshot.elapsedTime.numberOfMeasurements should be(0) + val afterFinishSegmentSnapshot = takeSnapshotOf("closing-segment-after-trace", "trace") + afterFinishSegmentSnapshot.histogram("elapsed-time").get.numberOfMeasurements should be(0) afterFinishSegmentSnapshot.segments.size should be(1) - afterFinishSegmentSnapshot.segments(SegmentMetricIdentity("test-segment", "test-category", "test-library")).numberOfMeasurements should be(1) + afterFinishSegmentSnapshot.segment("test-segment", "test-category", "test-library").numberOfMeasurements should be(1) } } +} + +object TraceMetricsSpec { + implicit class SegmentSyntax(val entitySnapshot: EntitySnapshot) extends AnyVal { + def segments: Map[HistogramKey, Histogram.Snapshot] = { + entitySnapshot.histograms.filterKeys(_.metadata.contains("category")) + } - def takeSnapshotOf(traceName: String): TraceMetricsSnapshot = { - val recorder = Kamon(Metrics).register(TraceMetrics(traceName), TraceMetrics.Factory) - val collectionContext = Kamon(Metrics).buildDefaultCollectionContext - recorder.get.collect(collectionContext) + def segment(name: String, category: String, library: String): Histogram.Snapshot = + segments(TraceMetrics.segmentKey(name, category, library)) } } diff --git a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala index 6c4fe3fb..a345c6a9 100644 --- a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala @@ -1,310 +1,110 @@ package kamon.metric -import akka.actor.{ Props, ActorSystem } -import akka.testkit.{ ImplicitSender, TestKitBase } import com.typesafe.config.ConfigFactory -import kamon.Kamon -import kamon.metric.Subscriptions.TickMetricSnapshot -import kamon.metric.UserMetrics._ -import kamon.metric.instrument.{ Histogram, Counter, MinMaxCounter, Gauge } -import kamon.metric.instrument.Histogram.MutableRecord -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.metric.instrument.Histogram.DynamicRange +import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ -class UserMetricsSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("actor-metrics-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - | default-collection-context-buffer-size = 10 - | - | precision { - | default-histogram-precision { - | highest-trackable-value = 10000 - | significant-value-digits = 2 - | } - | - | default-min-max-counter-precision { - | refresh-interval = 1 hour - | highest-trackable-value = 1000 - | significant-value-digits = 2 - | } - | - | default-gauge-precision { - | refresh-interval = 1 hour - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - """.stripMargin)) +class UserMetricsSpec extends BaseKamonSpec("user-metrics-spec") { + override lazy val config = + ConfigFactory.parseString( + """ + |kamon.metric { + | tick-interval = 1 hour + | default-collection-context-buffer-size = 10 + |} + """.stripMargin) "the UserMetrics extension" should { + "allow registering a fully configured Histogram and get the same Histogram if registering again" in { - val histogramA = Kamon(UserMetrics).registerHistogram("histogram-with-settings", Histogram.Precision.Normal, 10000L) - val histogramB = Kamon(UserMetrics).registerHistogram("histogram-with-settings", Histogram.Precision.Normal, 10000L) + val histogramA = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) + val histogramB = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) histogramA shouldBe theSameInstanceAs(histogramB) } "return the original Histogram when registering a fully configured Histogram for second time but with different settings" in { - val histogramA = Kamon(UserMetrics).registerHistogram("histogram-with-settings", Histogram.Precision.Normal, 10000L) - val histogramB = Kamon(UserMetrics).registerHistogram("histogram-with-settings", Histogram.Precision.Fine, 50000L) + val histogramA = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) + val histogramB = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 50000, 2)) histogramA shouldBe theSameInstanceAs(histogramB) } "allow registering a Histogram that takes the default configuration from the kamon.metrics.precision settings" in { - Kamon(UserMetrics).registerHistogram("histogram-with-default-configuration") + kamon.userMetrics.histogram("histogram-with-default-configuration") } "allow registering a Counter and get the same Counter if registering again" in { - val counterA = Kamon(UserMetrics).registerCounter("counter") - val counterB = Kamon(UserMetrics).registerCounter("counter") + val counterA = kamon.userMetrics.counter("counter") + val counterB = kamon.userMetrics.counter("counter") counterA shouldBe theSameInstanceAs(counterB) } "allow registering a fully configured MinMaxCounter and get the same MinMaxCounter if registering again" in { - val minMaxCounterA = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-settings", Histogram.Precision.Normal, 1000L, 1 second) - val minMaxCounterB = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-settings", Histogram.Precision.Normal, 1000L, 1 second) + val minMaxCounterA = kamon.userMetrics.minMaxCounter("min-max-counter-with-settings", DynamicRange(1, 10000, 2), 1 second) + val minMaxCounterB = kamon.userMetrics.minMaxCounter("min-max-counter-with-settings", DynamicRange(1, 10000, 2), 1 second) minMaxCounterA shouldBe theSameInstanceAs(minMaxCounterB) } "return the original MinMaxCounter when registering a fully configured MinMaxCounter for second time but with different settings" in { - val minMaxCounterA = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-settings", Histogram.Precision.Normal, 1000L, 1 second) - val minMaxCounterB = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-settings", Histogram.Precision.Fine, 5000L, 1 second) + val minMaxCounterA = kamon.userMetrics.minMaxCounter("min-max-counter-with-settings", DynamicRange(1, 10000, 2), 1 second) + val minMaxCounterB = kamon.userMetrics.minMaxCounter("min-max-counter-with-settings", DynamicRange(1, 50000, 2), 1 second) minMaxCounterA shouldBe theSameInstanceAs(minMaxCounterB) } "allow registering a MinMaxCounter that takes the default configuration from the kamon.metrics.precision settings" in { - Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-default-configuration") + kamon.userMetrics.minMaxCounter("min-max-counter-with-default-configuration") } "allow registering a fully configured Gauge and get the same Gauge if registering again" in { - val gaugeA = Kamon(UserMetrics).registerGauge("gauge-with-settings", Histogram.Precision.Normal, 1000L, 1 second) { + val gaugeA = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L - } + }) - val gaugeB = Kamon(UserMetrics).registerGauge("gauge-with-settings", Histogram.Precision.Normal, 1000L, 1 second) { + val gaugeB = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L - } + }) gaugeA shouldBe theSameInstanceAs(gaugeB) } "return the original Gauge when registering a fully configured Gauge for second time but with different settings" in { - val gaugeA = Kamon(UserMetrics).registerGauge("gauge-with-settings", Histogram.Precision.Normal, 1000L, 1 second) { + val gaugeA = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L - } + }) - val gaugeB = Kamon(UserMetrics).registerGauge("gauge-with-settings", Histogram.Precision.Fine, 5000L, 1 second) { + val gaugeB = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L - } + }) gaugeA shouldBe theSameInstanceAs(gaugeB) } "allow registering a Gauge that takes the default configuration from the kamon.metrics.precision settings" in { - Kamon(UserMetrics).registerGauge("gauge-with-default-configuration") { + kamon.userMetrics.gauge("gauge-with-default-configuration", { () ⇒ 2L - } + }) } "allow un-registering user metrics" in { - val metricsExtension = Kamon(Metrics) - Kamon(UserMetrics).registerCounter("counter-for-remove") - Kamon(UserMetrics).registerHistogram("histogram-for-remove") - Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-for-remove") - Kamon(UserMetrics).registerGauge("gauge-for-remove") { () ⇒ 2L } - - metricsExtension.storage.keys should contain(UserCounter("counter-for-remove")) - metricsExtension.storage.keys should contain(UserHistogram("histogram-for-remove")) - metricsExtension.storage.keys should contain(UserMinMaxCounter("min-max-counter-for-remove")) - metricsExtension.storage.keys should contain(UserGauge("gauge-for-remove")) - - Kamon(UserMetrics).removeCounter("counter-for-remove") - Kamon(UserMetrics).removeHistogram("histogram-for-remove") - Kamon(UserMetrics).removeMinMaxCounter("min-max-counter-for-remove") - Kamon(UserMetrics).removeGauge("gauge-for-remove") - - metricsExtension.storage.keys should not contain (UserCounter("counter-for-remove")) - metricsExtension.storage.keys should not contain (UserHistogram("histogram-for-remove")) - metricsExtension.storage.keys should not contain (UserMinMaxCounter("min-max-counter-for-remove")) - metricsExtension.storage.keys should not contain (UserGauge("gauge-for-remove")) - } - - "include all the registered metrics in the a tick snapshot and reset all recorders" in { - Kamon(Metrics).subscribe(UserHistograms, "*", testActor, permanently = true) - Kamon(Metrics).subscribe(UserCounters, "*", testActor, permanently = true) - Kamon(Metrics).subscribe(UserMinMaxCounters, "*", testActor, permanently = true) - Kamon(Metrics).subscribe(UserGauges, "*", testActor, permanently = true) - - val histogramWithSettings = Kamon(UserMetrics).registerHistogram("histogram-with-settings", Histogram.Precision.Normal, 10000L) - val histogramWithDefaultConfiguration = Kamon(UserMetrics).registerHistogram("histogram-with-default-configuration") - val counter = Kamon(UserMetrics).registerCounter("counter") - val minMaxCounterWithSettings = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-with-settings", Histogram.Precision.Normal, 1000L, 1 second) - val gauge = Kamon(UserMetrics).registerGauge("gauge-with-default-configuration") { () ⇒ 2L } - - // lets put some values on those metrics - histogramWithSettings.record(10) - histogramWithSettings.record(20, 100) - histogramWithDefaultConfiguration.record(40) - - counter.increment() - counter.increment(16) - - minMaxCounterWithSettings.increment(43) - minMaxCounterWithSettings.decrement() - - gauge.record(15) - - Kamon(Metrics).subscriptions ! Subscriptions.FlushMetrics - val firstSnapshot = expectMsgType[TickMetricSnapshot].metrics - - firstSnapshot.keys should contain allOf ( - UserHistogram("histogram-with-settings"), - UserHistogram("histogram-with-default-configuration")) - - firstSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (10) - firstSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (20) - firstSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(101) - firstSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain allOf ( - MutableRecord(10, 1), - MutableRecord(20, 100)) - - firstSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (40) - firstSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (40) - firstSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(1) - firstSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain only ( - MutableRecord(40, 1)) - - firstSnapshot(UserCounter("counter")).metrics(Count).asInstanceOf[Counter.Snapshot].count should be(17) - - firstSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (43) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(3) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain allOf ( - MutableRecord(0, 1), // min - MutableRecord(42, 1), // current - MutableRecord(43, 1)) // max - - firstSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (0) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(3) - firstSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain only ( - MutableRecord(0, 3)) // min, max and current - - firstSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (15) - firstSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (15) - firstSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(1) - firstSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain only ( - MutableRecord(15, 1)) // only the manually recorded value - - Kamon(Metrics).subscriptions ! Subscriptions.FlushMetrics - val secondSnapshot = expectMsgType[TickMetricSnapshot].metrics - - secondSnapshot.keys should contain allOf ( - UserHistogram("histogram-with-settings"), - UserHistogram("histogram-with-default-configuration")) - - secondSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - secondSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (0) - secondSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(0) - secondSnapshot(UserHistogram("histogram-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream shouldBe empty - - secondSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - secondSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (0) - secondSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(0) - secondSnapshot(UserHistogram("histogram-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream shouldBe empty - - secondSnapshot(UserCounter("counter")).metrics(Count).asInstanceOf[Counter.Snapshot].count should be(0) - - secondSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (42) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (42) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(3) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-settings")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain only ( - MutableRecord(42, 3)) // max - - secondSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (0) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(3) - secondSnapshot(UserMinMaxCounter("min-max-counter-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain only ( - MutableRecord(0, 3)) // min, max and current - - secondSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - secondSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (0) - secondSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(0) - secondSnapshot(UserGauge("gauge-with-default-configuration")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream shouldBe empty - - Kamon(Metrics).unsubscribe(testActor) - } - - "generate a snapshot that can be merged with another" in { - val buffer = system.actorOf(TickMetricSnapshotBuffer.props(1 hours, testActor)) - Kamon(Metrics).subscribe(UserHistograms, "*", buffer, permanently = true) - Kamon(Metrics).subscribe(UserCounters, "*", buffer, permanently = true) - Kamon(Metrics).subscribe(UserMinMaxCounters, "*", buffer, permanently = true) - Kamon(Metrics).subscribe(UserGauges, "*", buffer, permanently = true) - - val histogram = Kamon(UserMetrics).registerHistogram("histogram-for-merge") - val counter = Kamon(UserMetrics).registerCounter("counter-for-merge") - val minMaxCounter = Kamon(UserMetrics).registerMinMaxCounter("min-max-counter-for-merge") - val gauge = Kamon(UserMetrics).registerGauge("gauge-for-merge") { () ⇒ 10L } - - histogram.record(100) - counter.increment(10) - minMaxCounter.increment(50) - minMaxCounter.decrement(10) - gauge.record(50) - - Kamon(Metrics).subscriptions ! Subscriptions.FlushMetrics - Thread.sleep(2000) // Make sure that the snapshots are taken before proceeding - - val extraCounter = Kamon(UserMetrics).registerCounter("extra-counter") - histogram.record(200) - extraCounter.increment(20) - minMaxCounter.increment(40) - minMaxCounter.decrement(50) - gauge.record(70) - - Kamon(Metrics).subscriptions ! Subscriptions.FlushMetrics - Thread.sleep(2000) // Make sure that the metrics are buffered. - buffer ! TickMetricSnapshotBuffer.FlushBuffer - val snapshot = expectMsgType[TickMetricSnapshot].metrics - - snapshot.keys should contain(UserHistogram("histogram-for-merge")) - - snapshot(UserHistogram("histogram-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (100) - snapshot(UserHistogram("histogram-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (200) - snapshot(UserHistogram("histogram-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(2) - snapshot(UserHistogram("histogram-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain allOf ( - MutableRecord(100, 1), - MutableRecord(200, 1)) - - snapshot(UserCounter("counter-for-merge")).metrics(Count).asInstanceOf[Counter.Snapshot].count should be(10) - snapshot(UserCounter("extra-counter")).metrics(Count).asInstanceOf[Counter.Snapshot].count should be(20) - - snapshot(UserMinMaxCounter("min-max-counter-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (0) - snapshot(UserMinMaxCounter("min-max-counter-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (80) - snapshot(UserMinMaxCounter("min-max-counter-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(6) - snapshot(UserMinMaxCounter("min-max-counter-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain allOf ( - MutableRecord(0, 1), // min in first snapshot - MutableRecord(30, 2), // min and current in second snapshot - MutableRecord(40, 1), // current in first snapshot - MutableRecord(50, 1), // max in first snapshot - MutableRecord(80, 1)) // max in second snapshot - - snapshot(UserGauge("gauge-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].min shouldBe (50) - snapshot(UserGauge("gauge-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].max shouldBe (70) - snapshot(UserGauge("gauge-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].numberOfMeasurements should be(2) - snapshot(UserGauge("gauge-for-merge")).metrics(RecordedValues).asInstanceOf[Histogram.Snapshot].recordsIterator.toStream should contain allOf ( - MutableRecord(50, 1), - MutableRecord(70, 1)) - - Kamon(Metrics).unsubscribe(testActor) + val counter = kamon.userMetrics.counter("counter-for-remove") + val histogram = kamon.userMetrics.histogram("histogram-for-remove") + val minMaxCounter = kamon.userMetrics.minMaxCounter("min-max-counter-for-remove") + val gauge = kamon.userMetrics.gauge("gauge-for-remove", { () ⇒ 2L }) + + kamon.userMetrics.removeCounter("counter-for-remove") + kamon.userMetrics.removeHistogram("histogram-for-remove") + kamon.userMetrics.removeMinMaxCounter("min-max-counter-for-remove") + kamon.userMetrics.removeGauge("gauge-for-remove") + + counter should not be (theSameInstanceAs(kamon.userMetrics.counter("counter-for-remove"))) + histogram should not be (theSameInstanceAs(kamon.userMetrics.histogram("histogram-for-remove"))) + minMaxCounter should not be (theSameInstanceAs(kamon.userMetrics.minMaxCounter("min-max-counter-for-remove"))) + gauge should not be (theSameInstanceAs(kamon.userMetrics.gauge("gauge-for-remove", { () ⇒ 2L }))) } } } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala index 1a93e1f6..500a69c5 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/CounterSpec.scala @@ -2,7 +2,6 @@ package kamon.metric.instrument import java.nio.LongBuffer -import kamon.metric.CollectionContext import org.scalatest.{ Matchers, WordSpec } class CounterSpec extends WordSpec with Matchers { diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala index 9192d999..bd39652c 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala @@ -1,72 +1,62 @@ package kamon.metric.instrument import java.util.concurrent.atomic.AtomicLong - -import akka.actor.ActorSystem -import com.typesafe.config.ConfigFactory -import kamon.Kamon -import kamon.metric.{ Metrics, Scale, CollectionContext } -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.metric.instrument.Histogram.DynamicRange +import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ -class GaugeSpec extends WordSpecLike with Matchers { - implicit val system = ActorSystem("gauge-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | flush-interval = 1 hour - | default-collection-context-buffer-size = 10 - | precision { - | default-gauge-precision { - | refresh-interval = 100 milliseconds - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - """.stripMargin)) +class GaugeSpec extends BaseKamonSpec("gauge-spec") { "a Gauge" should { - "automatically record the current value using the configured refresh-interval" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } - + "automatically record the current value using the configured refresh-interval" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) + numberOfValuesRecorded.get() should be(10L +- 1L) gauge.cleanup } - "stop automatically recording after a call to cleanup" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } - + "stop automatically recording after a call to cleanup" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) + gauge.cleanup numberOfValuesRecorded.get() should be(10L +- 1L) Thread.sleep(1.second.toMillis) + numberOfValuesRecorded.get() should be(10L +- 1L) } - "produce a Histogram snapshot including all the recorded values" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge.fromDefaultConfig(system) { () ⇒ numberOfValuesRecorded.addAndGet(1) } + "produce a Histogram snapshot including all the recorded values" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge() Thread.sleep(1.second.toMillis) gauge.cleanup - val snapshot = gauge.collect(Kamon(Metrics).buildDefaultCollectionContext) + val snapshot = gauge.collect(kamon.metrics.buildDefaultCollectionContext) snapshot.numberOfMeasurements should be(10L +- 1L) snapshot.min should be(1) snapshot.max should be(10L +- 1L) } - "not record the current value when doing a collection" in { - val numberOfValuesRecorded = new AtomicLong(0) - val gauge = Gauge(Histogram.Precision.Normal, 10000L, Scale.Unit, 1 hour, system)(() ⇒ numberOfValuesRecorded.addAndGet(1)) - - val snapshot = gauge.collect(Kamon(Metrics).buildDefaultCollectionContext) + "not record the current value when doing a collection" in new GaugeFixture { + val (numberOfValuesRecorded, gauge) = createGauge(10 seconds) + val snapshot = gauge.collect(kamon.metrics.buildDefaultCollectionContext) snapshot.numberOfMeasurements should be(0) numberOfValuesRecorded.get() should be(0) } } + + trait GaugeFixture { + def createGauge(refreshInterval: FiniteDuration = 100 millis): (AtomicLong, Gauge) = { + val recordedValuesCounter = new AtomicLong(0) + val gauge = Gauge(DynamicRange(1, 100, 2), refreshInterval, kamon.metrics.settings.refreshScheduler, { + () ⇒ recordedValuesCounter.addAndGet(1) + }) + + (recordedValuesCounter, gauge) + } + + } } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala index c3060d4a..9a50e149 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/HistogramSpec.scala @@ -18,22 +18,13 @@ package kamon.metric.instrument import java.nio.LongBuffer -import com.typesafe.config.ConfigFactory -import kamon.metric.CollectionContext +import kamon.metric.instrument.Histogram.DynamicRange import org.scalatest.{ Matchers, WordSpec } import scala.util.Random class HistogramSpec extends WordSpec with Matchers { - val histogramConfig = ConfigFactory.parseString( - """ - | - |highest-trackable-value = 100000 - |significant-value-digits = 2 - | - """.stripMargin) - "a Histogram" should { "allow record values within the configured range" in new HistogramFixture { histogram.record(1000) @@ -109,7 +100,7 @@ class HistogramSpec extends WordSpec with Matchers { val buffer: LongBuffer = LongBuffer.allocate(10000) } - val histogram = Histogram.fromConfig(histogramConfig) + val histogram = Histogram(DynamicRange(1, 100000, 2)) def takeSnapshot(): Histogram.Snapshot = histogram.collect(collectionContext) } @@ -119,17 +110,20 @@ class HistogramSpec extends WordSpec with Matchers { val buffer: LongBuffer = LongBuffer.allocate(10000) } - val controlHistogram = Histogram.fromConfig(histogramConfig) - val histogramA = Histogram.fromConfig(histogramConfig) - val histogramB = Histogram.fromConfig(histogramConfig) + val controlHistogram = Histogram(DynamicRange(1, 100000, 2)) + val histogramA = Histogram(DynamicRange(1, 100000, 2)) + val histogramB = Histogram(DynamicRange(1, 100000, 2)) + + def takeSnapshotFrom(histogram: Histogram): InstrumentSnapshot = histogram.collect(collectionContext) - def takeSnapshotFrom(histogram: Histogram): Histogram.Snapshot = histogram.collect(collectionContext) + def assertEquals(left: InstrumentSnapshot, right: InstrumentSnapshot): Unit = { + val leftSnapshot = left.asInstanceOf[Histogram.Snapshot] + val rightSnapshot = right.asInstanceOf[Histogram.Snapshot] - def assertEquals(left: Histogram.Snapshot, right: Histogram.Snapshot): Unit = { - left.numberOfMeasurements should equal(right.numberOfMeasurements) - left.min should equal(right.min) - left.max should equal(right.max) - left.recordsIterator.toStream should contain theSameElementsAs (right.recordsIterator.toStream) + leftSnapshot.numberOfMeasurements should equal(rightSnapshot.numberOfMeasurements) + leftSnapshot.min should equal(rightSnapshot.min) + leftSnapshot.max should equal(rightSnapshot.max) + leftSnapshot.recordsIterator.toStream should contain theSameElementsAs (rightSnapshot.recordsIterator.toStream) } } } diff --git a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala index 2c11adc3..7a3d7aa3 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala @@ -19,19 +19,11 @@ import java.nio.LongBuffer import akka.actor._ import akka.testkit.TestProbe -import com.typesafe.config.ConfigFactory -import kamon.metric.CollectionContext -import kamon.metric.instrument.Histogram.MutableRecord -import org.scalatest.{ Matchers, WordSpecLike } - -class MinMaxCounterSpec extends WordSpecLike with Matchers { - implicit val system = ActorSystem("min-max-counter-spec") - val minMaxCounterConfig = ConfigFactory.parseString( - """ - |refresh-interval = 1 hour - |highest-trackable-value = 1000 - |significant-value-digits = 2 - """.stripMargin) +import kamon.metric.instrument.Histogram.{ DynamicRange, MutableRecord } +import kamon.testkit.BaseKamonSpec +import scala.concurrent.duration._ + +class MinMaxCounterSpec extends BaseKamonSpec("min-max-counter-spec") { "the MinMaxCounter" should { "track ascending tendencies" in new MinMaxCounterFixture { @@ -104,7 +96,7 @@ class MinMaxCounterSpec extends WordSpecLike with Matchers { workers foreach (_ ! "increment") for (refresh ← 1 to 1000) { collectCounterSnapshot() - Thread.sleep(10) + Thread.sleep(1) } monitor.expectNoMsg() @@ -117,7 +109,7 @@ class MinMaxCounterSpec extends WordSpecLike with Matchers { val buffer: LongBuffer = LongBuffer.allocate(64) } - val mmCounter = MinMaxCounter.fromConfig(minMaxCounterConfig, system).asInstanceOf[PaddedMinMaxCounter] + val mmCounter = MinMaxCounter(DynamicRange(1, 1000, 2), 1 hour, kamon.metrics.settings.refreshScheduler) mmCounter.cleanup // cancel the refresh schedule def collectCounterSnapshot(): Histogram.Snapshot = mmCounter.collect(collectionContext) diff --git a/kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala b/kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala new file mode 100644 index 00000000..20fc3ed5 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala @@ -0,0 +1,34 @@ +package kamon.testkit + +import akka.testkit.{ ImplicitSender, TestKitBase } +import akka.actor.ActorSystem +import com.typesafe.config.{ Config, ConfigFactory } +import kamon.Kamon +import kamon.metric.{ SubscriptionsDispatcher, EntitySnapshot, MetricsExtensionImpl } +import kamon.trace.TraceContext +import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } + +abstract class BaseKamonSpec(actorSystemName: String) extends TestKitBase with WordSpecLike with Matchers with ImplicitSender with BeforeAndAfterAll { + lazy val kamon = Kamon(actorSystemName, config) + lazy val collectionContext = kamon.metrics.buildDefaultCollectionContext + implicit lazy val system: ActorSystem = kamon.actorSystem + + def config: Config = + ConfigFactory.load() + + def newContext(name: String): TraceContext = + kamon.tracer.newContext(name) + + def newContext(name: String, token: String): TraceContext = + kamon.tracer.newContext(name, token) + + def takeSnapshotOf(name: String, category: String): EntitySnapshot = { + val recorder = kamon.metrics.find(name, category).get + recorder.collect(collectionContext) + } + + def flushSubscriptions(): Unit = + system.actorSelection("/user/kamon/subscriptions-dispatcher") ! SubscriptionsDispatcher.Tick + + override protected def afterAll(): Unit = system.shutdown() +} diff --git a/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala b/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala index cda9cad7..0cb4ce34 100644 --- a/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala @@ -16,58 +16,40 @@ package kamon.trace -import akka.actor.ActorSystem -import akka.testkit.{ ImplicitSender, TestKitBase } import com.typesafe.config.ConfigFactory import kamon.Kamon -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ -class SimpleTraceSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("simple-trace-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - | filters = [ - | { - | trace { - | includes = [ "*" ] - | excludes = [ "non-tracked-trace"] - | } - | } - | ] - | precision { - | default-histogram-precision { - | highest-trackable-value = 3600000000000 - | significant-value-digits = 2 - | } - | - | default-min-max-counter-precision { - | refresh-interval = 1 second - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - | - |kamon.trace { - | level = simple-trace - | sampling = all - |} - """.stripMargin)) +class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") { + + override lazy val config = + ConfigFactory.parseString( + """ + |kamon { + | metric { + | tick-interval = 1 hour + | } + | + | trace { + | level-of-detail = simple-trace + | sampling = all + | } + |} + """.stripMargin) "the simple tracing" should { "send a TraceInfo when the trace has finished and all segments are finished" in { - Kamon(Trace)(system).subscribe(testActor) + Kamon(Tracer)(system).subscribe(testActor) - TraceRecorder.withNewTraceContext("simple-trace-without-segments") { - TraceRecorder.currentContext.startSegment("segment-one", "test-segment", "test").finish() - TraceRecorder.currentContext.startSegment("segment-two", "test-segment", "test").finish() - TraceRecorder.finish() + TraceContext.withContext(newContext("simple-trace-without-segments")) { + TraceContext.currentContext.startSegment("segment-one", "test-segment", "test").finish() + TraceContext.currentContext.startSegment("segment-two", "test-segment", "test").finish() + TraceContext.currentContext.finish() } val traceInfo = expectMsgType[TraceInfo] - Kamon(Trace)(system).unsubscribe(testActor) + Kamon(Tracer)(system).unsubscribe(testActor) traceInfo.name should be("simple-trace-without-segments") traceInfo.segments.size should be(2) @@ -76,12 +58,12 @@ class SimpleTraceSpec extends TestKitBase with WordSpecLike with Matchers with I } "incubate the tracing context if there are open segments after finishing" in { - Kamon(Trace)(system).subscribe(testActor) + Kamon(Tracer)(system).subscribe(testActor) - val secondSegment = TraceRecorder.withNewTraceContext("simple-trace-without-segments") { - TraceRecorder.currentContext.startSegment("segment-one", "test-segment", "test").finish() - val segment = TraceRecorder.currentContext.startSegment("segment-two", "test-segment", "test") - TraceRecorder.finish() + val secondSegment = TraceContext.withContext(newContext("simple-trace-without-segments")) { + TraceContext.currentContext.startSegment("segment-one", "test-segment", "test").finish() + val segment = TraceContext.currentContext.startSegment("segment-two", "test-segment", "test") + TraceContext.currentContext.finish() segment } @@ -90,7 +72,7 @@ class SimpleTraceSpec extends TestKitBase with WordSpecLike with Matchers with I within(10 seconds) { val traceInfo = expectMsgType[TraceInfo] - Kamon(Trace)(system).unsubscribe(testActor) + Kamon(Tracer)(system).unsubscribe(testActor) traceInfo.name should be("simple-trace-without-segments") traceInfo.segments.size should be(2) diff --git a/kamon-core/src/test/scala/kamon/trace/TraceContextManipulationSpec.scala b/kamon-core/src/test/scala/kamon/trace/TraceContextManipulationSpec.scala index 0875deff..9d7725b7 100644 --- a/kamon-core/src/test/scala/kamon/trace/TraceContextManipulationSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/TraceContextManipulationSpec.scala @@ -1,94 +1,80 @@ package kamon.trace -import akka.actor.ActorSystem -import akka.testkit.{ ImplicitSender, TestKitBase } import com.typesafe.config.ConfigFactory -import org.scalatest.{ Matchers, WordSpecLike } +import kamon.testkit.BaseKamonSpec -class TraceContextManipulationSpec extends TestKitBase with WordSpecLike with Matchers with ImplicitSender { - implicit lazy val system: ActorSystem = ActorSystem("trace-metrics-spec", ConfigFactory.parseString( - """ - |kamon.metrics { - | tick-interval = 1 hour - | filters = [ - | { - | trace { - | includes = [ "*" ] - | excludes = [ "non-tracked-trace"] - | } - | } - | ] - | precision { - | default-histogram-precision { - | highest-trackable-value = 3600000000000 - | significant-value-digits = 2 - | } - | - | default-min-max-counter-precision { - | refresh-interval = 1 second - | highest-trackable-value = 999999999 - | significant-value-digits = 2 - | } - | } - |} - """.stripMargin)) +class TraceContextManipulationSpec extends BaseKamonSpec("trace-metrics-spec") { + override lazy val config = + ConfigFactory.parseString( + """ + |kamon.metric { + | tick-interval = 1 hour + | + | filters { + | trace { + | includes = [ "*" ] + | excludes = [ "non-tracked-trace"] + | } + | } + |} + """.stripMargin) - "the TraceRecorder api" should { + "the TraceContext api" should { "allow starting a trace within a specified block of code, and only within that block of code" in { - val createdContext = TraceRecorder.withNewTraceContext("start-context") { - TraceRecorder.currentContext should not be empty - TraceRecorder.currentContext + val createdContext = TraceContext.withContext(newContext("start-context")) { + TraceContext.currentContext should not be empty + TraceContext.currentContext } - TraceRecorder.currentContext shouldBe empty + TraceContext.currentContext shouldBe empty createdContext.name shouldBe ("start-context") } "allow starting a trace within a specified block of code, providing a trace-token and only within that block of code" in { - val createdContext = TraceRecorder.withNewTraceContext("start-context-with-token", Some("token-1")) { - TraceRecorder.currentContext should not be empty - TraceRecorder.currentContext + val createdContext = TraceContext.withContext(newContext("start-context-with-token", "token-1")) { + TraceContext.currentContext should not be empty + TraceContext.currentContext } - TraceRecorder.currentContext shouldBe empty + TraceContext.currentContext shouldBe empty createdContext.name shouldBe ("start-context-with-token") createdContext.token should be("token-1") } "allow providing a TraceContext and make it available within a block of code" in { - val createdContext = TraceRecorder.withNewTraceContext("manually-provided-trace-context") { TraceRecorder.currentContext } + val createdContext = newContext("manually-provided-trace-context") - TraceRecorder.currentContext shouldBe empty - TraceRecorder.withTraceContext(createdContext) { - TraceRecorder.currentContext should be(createdContext) + TraceContext.currentContext shouldBe empty + TraceContext.withContext(createdContext) { + TraceContext.currentContext should be(createdContext) } - TraceRecorder.currentContext shouldBe empty + TraceContext.currentContext shouldBe empty } "allow renaming a trace" in { - val createdContext = TraceRecorder.withNewTraceContext("trace-before-rename") { - TraceRecorder.rename("renamed-trace") - TraceRecorder.currentContext + val createdContext = TraceContext.withContext(newContext("trace-before-rename")) { + TraceContext.currentContext.rename("renamed-trace") + TraceContext.currentContext } - TraceRecorder.currentContext shouldBe empty + TraceContext.currentContext shouldBe empty createdContext.name shouldBe ("renamed-trace") } "allow creating a segment within a trace" in { - val createdContext = TraceRecorder.withNewTraceContext("trace-with-segments") { - val segment = TraceRecorder.currentContext.startSegment("segment-1", "segment-1-category", "segment-library") - TraceRecorder.currentContext + val createdContext = TraceContext.withContext(newContext("trace-with-segments")) { + val segment = TraceContext.currentContext.startSegment("segment-1", "segment-1-category", "segment-library") + TraceContext.currentContext } - TraceRecorder.currentContext shouldBe empty + TraceContext.currentContext shouldBe empty createdContext.name shouldBe ("trace-with-segments") } "allow renaming a segment" in { - TraceRecorder.withNewTraceContext("trace-with-renamed-segment") { - val segment = TraceRecorder.currentContext.startSegment("original-segment-name", "segment-label", "segment-library") + TraceContext.withContext(newContext("trace-with-renamed-segment")) { + val segment = TraceContext.currentContext.startSegment("original-segment-name", "segment-label", "segment-library") segment.name should be("original-segment-name") segment.rename("new-segment-name") diff --git a/kamon-core/src/test/scala/kamon/trace/TraceLocalSpec.scala b/kamon-core/src/test/scala/kamon/trace/TraceLocalSpec.scala index f2b25820..8bacca83 100644 --- a/kamon-core/src/test/scala/kamon/trace/TraceLocalSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/TraceLocalSpec.scala @@ -16,24 +16,21 @@ package kamon.trace -import akka.actor.ActorSystem -import akka.testkit.TestKit +import kamon.testkit.BaseKamonSpec import kamon.trace.TraceLocal.AvailableToMdc import kamon.trace.logging.MdcKeysSupport import org.scalatest.concurrent.PatienceConfiguration -import org.scalatest.{ Matchers, OptionValues, WordSpecLike } +import org.scalatest.OptionValues import org.slf4j.MDC -class TraceLocalSpec extends TestKit(ActorSystem("trace-local-spec")) with WordSpecLike with Matchers - with PatienceConfiguration with OptionValues with MdcKeysSupport { - +class TraceLocalSpec extends BaseKamonSpec("trace-local-spec") with PatienceConfiguration with OptionValues with MdcKeysSupport { val SampleTraceLocalKeyAvailableToMDC = AvailableToMdc("someKey") object SampleTraceLocalKey extends TraceLocal.TraceLocalKey { type ValueType = String } "the TraceLocal storage" should { "allow storing and retrieving values" in { - TraceRecorder.withNewTraceContext("store-and-retrieve-trace-local") { + TraceContext.withContext(newContext("store-and-retrieve-trace-local")) { val testString = "Hello World" TraceLocal.store(SampleTraceLocalKey)(testString) @@ -42,7 +39,7 @@ class TraceLocalSpec extends TestKit(ActorSystem("trace-local-spec")) with WordS } "return None when retrieving a non existent key" in { - TraceRecorder.withNewTraceContext("non-existent-key") { + TraceContext.withContext(newContext("non-existent-key")) { TraceLocal.retrieve(SampleTraceLocalKey) should equal(None) } } @@ -53,22 +50,22 @@ class TraceLocalSpec extends TestKit(ActorSystem("trace-local-spec")) with WordS "be attached to the TraceContext when it is propagated" in { val testString = "Hello World" - val testContext = TraceRecorder.withNewTraceContext("manually-propagated-trace-local") { + val testContext = TraceContext.withContext(newContext("manually-propagated-trace-local")) { TraceLocal.store(SampleTraceLocalKey)(testString) TraceLocal.retrieve(SampleTraceLocalKey).value should equal(testString) - TraceRecorder.currentContext + TraceContext.currentContext } /** No TraceLocal should be available here */ TraceLocal.retrieve(SampleTraceLocalKey) should equal(None) - TraceRecorder.withTraceContext(testContext) { + TraceContext.withContext(testContext) { TraceLocal.retrieve(SampleTraceLocalKey).value should equal(testString) } } "allow retrieve a value from the MDC when was created a key with AvailableToMdc(cool-key)" in { - TraceRecorder.withNewTraceContext("store-and-retrieve-trace-local-and-copy-to-mdc") { + TraceContext.withContext(newContext("store-and-retrieve-trace-local-and-copy-to-mdc")) { val testString = "Hello MDC" TraceLocal.store(SampleTraceLocalKeyAvailableToMDC)(testString) @@ -81,7 +78,7 @@ class TraceLocalSpec extends TestKit(ActorSystem("trace-local-spec")) with WordS } "allow retrieve a value from the MDC when was created a key with AvailableToMdc.storeForMdc(String, String)" in { - TraceRecorder.withNewTraceContext("store-and-retrieve-trace-local-and-copy-to-mdc") { + TraceContext.withContext(newContext("store-and-retrieve-trace-local-and-copy-to-mdc")) { val testString = "Hello MDC" TraceLocal.storeForMdc("someKey", testString) diff --git a/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala b/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala index 83992e61..ab98d0ac 100644 --- a/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala +++ b/kamon-core/src/test/scala/kamon/util/GlobPathFilterSpec.scala @@ -40,6 +40,13 @@ class GlobPathFilterSpec extends WordSpecLike with Matchers { filter.accept("/user/something/otherActor") shouldBe false } + "match all expressions in the same levelss" in { + val filter = new GlobPathFilter("**") + + filter.accept("GET: /ping") shouldBe true + filter.accept("GET: /ping/pong") shouldBe true + } + "match all expressions and crosses the path boundaries" in { val filter = new GlobPathFilter("/user/actor-**") @@ -51,7 +58,7 @@ class GlobPathFilterSpec extends WordSpecLike with Matchers { filter.accept("/user/something/otherActor") shouldBe false } - "match exactly one characterr" in { + "match exactly one character" in { val filter = new GlobPathFilter("/user/actor-?") filter.accept("/user/actor-1") shouldBe true -- cgit v1.2.3