From 07fc83bb01c5873b47aff50d6d3abbb9f11842bd Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Thu, 12 Feb 2015 11:30:06 +0100 Subject: ! all: Kamon now works as a single instance in a companion object. --- .../kamon/metric/SubscriptionsProtocolSpec.scala | 3 +- .../metric/TickMetricSnapshotBufferSpec.scala | 5 +- .../test/scala/kamon/metric/UserMetricsSpec.scala | 63 +++++++++++----------- .../scala/kamon/metric/instrument/GaugeSpec.scala | 7 +-- .../metric/instrument/MinMaxCounterSpec.scala | 3 +- .../test/scala/kamon/testkit/BaseKamonSpec.scala | 28 ++++++---- .../test/scala/kamon/trace/SimpleTraceSpec.scala | 8 +-- 7 files changed, 66 insertions(+), 51 deletions(-) (limited to 'kamon-core/src/test') diff --git a/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala b/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala index 7dbcafd7..53ae5273 100644 --- a/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/SubscriptionsProtocolSpec.scala @@ -19,6 +19,7 @@ package kamon.metric import akka.actor._ import akka.testkit.{ TestProbe, ImplicitSender } import com.typesafe.config.ConfigFactory +import kamon.Kamon import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ @@ -32,7 +33,7 @@ class SubscriptionsProtocolSpec extends BaseKamonSpec("subscriptions-protocol-sp |} """.stripMargin) - val metricsModule = kamon.metrics + lazy val metricsModule = Kamon.metrics import metricsModule.{ register, subscribe, unsubscribe } "the Subscriptions messaging protocol" should { diff --git a/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala b/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala index 2e1f246d..0c9ced32 100644 --- a/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/TickMetricSnapshotBufferSpec.scala @@ -17,6 +17,7 @@ package kamon.metric import com.typesafe.config.ConfigFactory +import kamon.Kamon import kamon.metric.instrument.Histogram.MutableRecord import kamon.testkit.BaseKamonSpec import kamon.util.MilliTimestamp @@ -85,9 +86,9 @@ class TickMetricSnapshotBufferSpec extends BaseKamonSpec("trace-metrics-spec") w } trait SnapshotFixtures { - val collectionContext = kamon.metrics.buildDefaultCollectionContext + 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 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) diff --git a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala index 7b6cbebc..455518f8 100644 --- a/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/UserMetricsSpec.scala @@ -17,6 +17,7 @@ package kamon.metric import com.typesafe.config.ConfigFactory +import kamon.Kamon import kamon.metric.instrument.Histogram.DynamicRange import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ @@ -34,54 +35,54 @@ class UserMetricsSpec extends BaseKamonSpec("user-metrics-spec") { "the UserMetrics extension" should { "allow registering a fully configured Histogram and get the same Histogram if registering again" in { - val histogramA = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) - val histogramB = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) + 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.histogram("histogram-with-settings", DynamicRange(1, 10000, 2)) - val histogramB = kamon.userMetrics.histogram("histogram-with-settings", DynamicRange(1, 50000, 2)) + 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.histogram("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.counter("counter") - val counterB = kamon.userMetrics.counter("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.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) + 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.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) + 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.minMaxCounter("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.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { + val gaugeA = Kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L }) - val gaugeB = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { + val gaugeB = Kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L }) @@ -89,11 +90,11 @@ class UserMetricsSpec extends BaseKamonSpec("user-metrics-spec") { } "return the original Gauge when registering a fully configured Gauge for second time but with different settings" in { - val gaugeA = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { + val gaugeA = Kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L }) - val gaugeB = kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { + val gaugeB = Kamon.userMetrics.gauge("gauge-with-settings", DynamicRange(1, 10000, 2), 1 second, { () ⇒ 1L }) @@ -101,26 +102,26 @@ class UserMetricsSpec extends BaseKamonSpec("user-metrics-spec") { } "allow registering a Gauge that takes the default configuration from the kamon.metrics.precision settings" in { - kamon.userMetrics.gauge("gauge-with-default-configuration", { + Kamon.userMetrics.gauge("gauge-with-default-configuration", { () ⇒ 2L }) } "allow un-registering user metrics" in { - 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 }))) + 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/GaugeSpec.scala b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala index bb494d0b..ec07d66c 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/GaugeSpec.scala @@ -17,6 +17,7 @@ package kamon.metric.instrument import java.util.concurrent.atomic.AtomicLong +import kamon.Kamon import kamon.metric.instrument.Histogram.DynamicRange import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ @@ -48,7 +49,7 @@ class GaugeSpec extends BaseKamonSpec("gauge-spec") { 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) @@ -58,7 +59,7 @@ class GaugeSpec extends BaseKamonSpec("gauge-spec") { "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) + val snapshot = gauge.collect(Kamon.metrics.buildDefaultCollectionContext) snapshot.numberOfMeasurements should be(0) numberOfValuesRecorded.get() should be(0) } @@ -67,7 +68,7 @@ class GaugeSpec extends BaseKamonSpec("gauge-spec") { 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, { + val gauge = Gauge(DynamicRange(1, 100, 2), refreshInterval, Kamon.metrics.settings.refreshScheduler, { () ⇒ recordedValuesCounter.addAndGet(1) }) 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 7a3d7aa3..7acfc229 100644 --- a/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala +++ b/kamon-core/src/test/scala/kamon/metric/instrument/MinMaxCounterSpec.scala @@ -19,6 +19,7 @@ import java.nio.LongBuffer import akka.actor._ import akka.testkit.TestProbe +import kamon.Kamon import kamon.metric.instrument.Histogram.{ DynamicRange, MutableRecord } import kamon.testkit.BaseKamonSpec import scala.concurrent.duration._ @@ -109,7 +110,7 @@ class MinMaxCounterSpec extends BaseKamonSpec("min-max-counter-spec") { val buffer: LongBuffer = LongBuffer.allocate(64) } - val mmCounter = MinMaxCounter(DynamicRange(1, 1000, 2), 1 hour, kamon.metrics.settings.refreshScheduler) + 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 index 9142ff16..eab6b754 100644 --- a/kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala +++ b/kamon-core/src/test/scala/kamon/testkit/BaseKamonSpec.scala @@ -22,29 +22,39 @@ import com.typesafe.config.{ Config, ConfigFactory } import kamon.Kamon import kamon.metric.{ SubscriptionsDispatcher, EntitySnapshot, MetricsExtensionImpl } import kamon.trace.TraceContext +import kamon.util.LazyActorRef import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } +import scala.reflect.ClassTag + 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 + lazy val collectionContext = Kamon.metrics.buildDefaultCollectionContext + implicit lazy val system: ActorSystem = { + Kamon.start(config.withFallback(ConfigFactory.load())) + ActorSystem(actorSystemName, config) + } def config: Config = - ConfigFactory.load() + ConfigFactory.empty() def newContext(name: String): TraceContext = - kamon.tracer.newContext(name) + Kamon.tracer.newContext(name) def newContext(name: String, token: String): TraceContext = - kamon.tracer.newContext(name, token) + Kamon.tracer.newContext(name, token) def takeSnapshotOf(name: String, category: String): EntitySnapshot = { - val recorder = kamon.metrics.find(name, category).get + val recorder = Kamon.metrics.find(name, category).get recorder.collect(collectionContext) } - def flushSubscriptions(): Unit = - system.actorSelection("/user/kamon/subscriptions-dispatcher") ! SubscriptionsDispatcher.Tick + def flushSubscriptions(): Unit = { + val subscriptionsField = Kamon.metrics.getClass.getDeclaredField("_subscriptions") + subscriptionsField.setAccessible(true) + val subscriptions = subscriptionsField.get(Kamon.metrics).asInstanceOf[LazyActorRef] + + subscriptions.tell(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 0cb4ce34..1d270106 100644 --- a/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/SimpleTraceSpec.scala @@ -40,7 +40,7 @@ class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") { "the simple tracing" should { "send a TraceInfo when the trace has finished and all segments are finished" in { - Kamon(Tracer)(system).subscribe(testActor) + Kamon.tracer.subscribe(testActor) TraceContext.withContext(newContext("simple-trace-without-segments")) { TraceContext.currentContext.startSegment("segment-one", "test-segment", "test").finish() @@ -49,7 +49,7 @@ class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") { } val traceInfo = expectMsgType[TraceInfo] - Kamon(Tracer)(system).unsubscribe(testActor) + Kamon.tracer.unsubscribe(testActor) traceInfo.name should be("simple-trace-without-segments") traceInfo.segments.size should be(2) @@ -58,7 +58,7 @@ class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") { } "incubate the tracing context if there are open segments after finishing" in { - Kamon(Tracer)(system).subscribe(testActor) + Kamon.tracer.subscribe(testActor) val secondSegment = TraceContext.withContext(newContext("simple-trace-without-segments")) { TraceContext.currentContext.startSegment("segment-one", "test-segment", "test").finish() @@ -72,7 +72,7 @@ class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") { within(10 seconds) { val traceInfo = expectMsgType[TraceInfo] - Kamon(Tracer)(system).unsubscribe(testActor) + Kamon.tracer.unsubscribe(testActor) traceInfo.name should be("simple-trace-without-segments") traceInfo.segments.size should be(2) -- cgit v1.2.3