From 54f37293b1faac5355c26a298db6b858114bc659 Mon Sep 17 00:00:00 2001 From: Ivan Topolnak Date: Fri, 7 Mar 2014 18:08:01 -0300 Subject: multiple fixes to the custom metrics collection facilities --- .../scala/kamon/metrics/CustomMetricSpec.scala | 29 ++++++-- .../scala/kamon/metrics/MetricSnapshotSpec.scala | 11 ++- .../metrics/TickMetricSnapshotBufferSpec.scala | 81 ++++++++++++++++++++++ 3 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 kamon-core/src/test/scala/kamon/metrics/TickMetricSnapshotBufferSpec.scala (limited to 'kamon-core/src/test') diff --git a/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala b/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala index f5caf6e9..1e072f71 100644 --- a/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala +++ b/kamon-core/src/test/scala/kamon/metrics/CustomMetricSpec.scala @@ -23,6 +23,7 @@ import scala.concurrent.duration._ import com.typesafe.config.ConfigFactory import kamon.Kamon import kamon.metrics.Subscriptions.TickMetricSnapshot +import kamon.metrics.MetricSnapshot.Measurement class CustomMetricSpec extends TestKitBase with WordSpecLike with Matchers { implicit lazy val system: ActorSystem = ActorSystem("actor-metrics-spec", ConfigFactory.parseString( @@ -41,20 +42,36 @@ class CustomMetricSpec extends TestKitBase with WordSpecLike with Matchers { "the Kamon custom metrics support" should { "allow registering a custom metric with the Metrics extension" in { - val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.withConfig(100, 2)) + val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.histogram(100, 2, Scale.Unit)) recorder should be('defined) } "allow subscriptions to custom metrics using the default subscription protocol" in { - val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.withConfig(100, 2)) - recorder.map(_.record(100)) + val recorder = Kamon(Metrics).register(CustomMetric("test/sample-counter"), CustomMetric.histogram(100, 2, Scale.Unit)) + + recorder.map { r ⇒ + r.record(100) + r.record(15) + r.record(0) + r.record(50) + } Kamon(Metrics).subscribe(CustomMetric, "test/sample-counter", testActor) - println(within(5 seconds) { - expectMsgType[TickMetricSnapshot] - }.metrics(CustomMetric("test/sample-counter"))) + val recordedValues = within(5 seconds) { + val snapshot = expectMsgType[TickMetricSnapshot] + snapshot.metrics(CustomMetric("test/sample-counter")).metrics(CustomMetric.RecordedValues) + } + + recordedValues.min should equal(0) + recordedValues.max should equal(100) + recordedValues.numberOfMeasurements should equal(4) + recordedValues.measurements should contain allOf ( + Measurement(0, 1), + Measurement(15, 1), + Measurement(50, 1), + Measurement(100, 1)) } } diff --git a/kamon-core/src/test/scala/kamon/metrics/MetricSnapshotSpec.scala b/kamon-core/src/test/scala/kamon/metrics/MetricSnapshotSpec.scala index 1c5a4b21..c273aff1 100644 --- a/kamon-core/src/test/scala/kamon/metrics/MetricSnapshotSpec.scala +++ b/kamon-core/src/test/scala/kamon/metrics/MetricSnapshotSpec.scala @@ -38,31 +38,30 @@ class MetricSnapshotSpec extends WordSpec with Matchers { merged.min should be(1) merged.max should be(17) merged.numberOfMeasurements should be(200) - merged.measurementLevels.map(_.value) should contain inOrderOnly (1, 2, 4, 5, 7, 10, 17) + merged.measurements.map(_.value) should contain inOrderOnly (1, 2, 4, 5, 7, 10, 17) } "be able to merge with empty snapshots" in new SnapshotFixtures { snapshotA.merge(emptySnapshot) should be(snapshotA) + emptySnapshot.merge(snapshotA).merge(emptySnapshot) should be(snapshotA) } } trait SnapshotFixtures { - val emptySnapshot = DefaultMetricSnapshot(0, Vector.empty) + val emptySnapshot = MetricSnapshot(0, Scale.Unit, Vector.empty) - val snapshotA = DefaultMetricSnapshot(100, Vector( + val snapshotA = MetricSnapshot(100, Scale.Unit, Vector( Measurement(1, 3), Measurement(2, 15), Measurement(5, 68), Measurement(7, 13), Measurement(17, 1))) - val snapshotB = DefaultMetricSnapshot(100, Vector( + val snapshotB = MetricSnapshot(100, Scale.Unit, Vector( Measurement(2, 6), Measurement(4, 48), Measurement(5, 39), Measurement(10, 7))) - } - } diff --git a/kamon-core/src/test/scala/kamon/metrics/TickMetricSnapshotBufferSpec.scala b/kamon-core/src/test/scala/kamon/metrics/TickMetricSnapshotBufferSpec.scala new file mode 100644 index 00000000..33200e2d --- /dev/null +++ b/kamon-core/src/test/scala/kamon/metrics/TickMetricSnapshotBufferSpec.scala @@ -0,0 +1,81 @@ +/* + * ========================================================================================= + * 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.metrics + +import org.scalatest.{ Matchers, WordSpecLike } +import akka.testkit.TestKit +import akka.actor.ActorSystem +import scala.concurrent.duration._ +import kamon.metrics.Subscriptions.TickMetricSnapshot +import kamon.metrics.MetricSnapshot.Measurement + +class TickMetricSnapshotBufferSpec extends TestKit(ActorSystem("tick-metric-snapshot-buffer")) with WordSpecLike with Matchers { + + "the TickMetricSnapshotBuffer" should { + "merge TickMetricSnapshots received until the flush timeout is reached and fix the from/to fields" in new SnapshotFixtures { + val buffer = system.actorOf(TickMetricSnapshotBuffer.props(3 seconds, testActor)) + + buffer ! firstEmpty + buffer ! secondEmpty + buffer ! thirdEmpty + + within(2 seconds)(expectNoMsg()) + val mergedSnapshot = expectMsgType[TickMetricSnapshot] + + mergedSnapshot.from should equal(1000) + mergedSnapshot.to should equal(4000) + mergedSnapshot.metrics should be('empty) + } + + "merge empty and non-empty snapshots" in new SnapshotFixtures { + val buffer = system.actorOf(TickMetricSnapshotBuffer.props(3 seconds, testActor)) + + buffer ! firstNonEmpty + buffer ! secondNonEmpty + buffer ! thirdEmpty + + within(2 seconds)(expectNoMsg()) + val mergedSnapshot = expectMsgType[TickMetricSnapshot] + + mergedSnapshot.from should equal(1000) + mergedSnapshot.to should equal(4000) + mergedSnapshot.metrics should not be ('empty) + + val testMetricSnapshot = mergedSnapshot.metrics(CustomMetric("test-metric")).metrics(CustomMetric.RecordedValues) + testMetricSnapshot.min should equal(1) + testMetricSnapshot.max should equal(10) + testMetricSnapshot.numberOfMeasurements should equal(35) + testMetricSnapshot.measurements should contain allOf (Measurement(1, 10), Measurement(4, 9), Measurement(10, 16)) + + } + } + + trait SnapshotFixtures { + val firstEmpty = TickMetricSnapshot(1000, 2000, Map.empty) + val secondEmpty = TickMetricSnapshot(2000, 3000, Map.empty) + val thirdEmpty = TickMetricSnapshot(3000, 4000, Map.empty) + + val firstNonEmpty = TickMetricSnapshot(1000, 2000, + Map((CustomMetric("test-metric") -> SimpleGroupSnapshot(Map(CustomMetric.RecordedValues -> MetricSnapshot(20, Scale.Unit, Vector(Measurement(1, 10), Measurement(10, 10)))))))) + + val secondNonEmpty = TickMetricSnapshot(1000, 2000, + Map((CustomMetric("test-metric") -> SimpleGroupSnapshot(Map(CustomMetric.RecordedValues -> MetricSnapshot(15, Scale.Unit, Vector(Measurement(4, 9), Measurement(10, 6)))))))) + + } + + case class SimpleGroupSnapshot(metrics: Map[MetricIdentity, MetricSnapshotLike]) extends MetricGroupSnapshot +} -- cgit v1.2.3