diff options
Diffstat (limited to 'kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala')
-rw-r--r-- | kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala | 113 |
1 files changed, 73 insertions, 40 deletions
diff --git a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala index 9dfd05f7..3bc1364c 100644 --- a/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala +++ b/kamon-statsd/src/test/scala/kamon/statsd/StatsDMetricSenderSpec.scala @@ -18,63 +18,83 @@ package kamon.statsd import akka.testkit.{ TestKitBase, TestProbe } import akka.actor.{ ActorRef, Props, ActorSystem } +import kamon.Kamon +import kamon.metric.instrument.Histogram.Precision +import kamon.metric.instrument.Histogram import org.scalatest.{ Matchers, WordSpecLike } -import kamon.metrics._ +import kamon.metric._ import akka.io.Udp -import org.HdrHistogram.HdrRecorder -import kamon.metrics.Subscriptions.TickMetricSnapshot +import kamon.metric.Subscriptions.TickMetricSnapshot import java.lang.management.ManagementFactory import java.net.InetSocketAddress import com.typesafe.config.ConfigFactory class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers { - implicit lazy val system = ActorSystem("statsd-metric-sender-spec", - ConfigFactory.parseString("kamon.statsd.max-packet-size = 256 bytes")) + implicit lazy val system: ActorSystem = ActorSystem("statsd-metric-sender-spec", ConfigFactory.parseString( + """ + |kamon { + | metrics { + | disable-aspectj-weaver-missing-error = true + | } + | + | statsd { + | max-packet-size = 256 bytes + | } + |} + | + """.stripMargin)) + + val collectionContext = Kamon(Metrics).buildDefaultCollectionContext "the StatsDMetricSender" should { + "normalize the group entity name to remove spaces, colons and replace '/' with '_'" in new UdpListenerFixture { + val testMetricKey = buildMetricKey("trace", "POST: /kamon/example", "elapsed-time") + testMetricKey should be(s"kamon.localhost_local.trace.POST-_kamon_example.elapsed-time") + } + "flush the metrics data after processing the tick, even if the max-packet-size is not reached" in new UdpListenerFixture { - val testMetricName = "test-metric" - val testMetricKey = buildMetricKey(testMetricName) - val testRecorder = HdrRecorder(1000L, 2, Scale.Unit) + val testMetricName = "processing-time" + val testMetricKey = buildMetricKey("actor", "/user/kamon", testMetricName) + val testRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) testRecorder.record(10L) - val udp = setup(Map(testMetricName -> testRecorder.collect())) + val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"$testMetricKey:10|ms") } "render several measurements of the same key under a single (key + multiple measurements) packet" in new UdpListenerFixture { - val testMetricName = "test-metric" - val testMetricKey = buildMetricKey(testMetricName) - val testRecorder = HdrRecorder(1000L, 2, Scale.Unit) + val testMetricName = "processing-time" + val testMetricKey = buildMetricKey("actor", "/user/kamon", testMetricName) + val testRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) testRecorder.record(10L) testRecorder.record(11L) testRecorder.record(12L) - val udp = setup(Map(testMetricName -> testRecorder.collect())) + val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"$testMetricKey:10|ms:11|ms:12|ms") } "include the correspondent sampling rate when rendering multiple occurrences of the same value" in new UdpListenerFixture { - val testMetricName = "test-metric" - val testMetricKey = buildMetricKey(testMetricName) - val testRecorder = HdrRecorder(1000L, 2, Scale.Unit) + val testMetricName = "processing-time" + val testMetricKey = buildMetricKey("actor", "/user/kamon", testMetricName) + val testRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) testRecorder.record(10L) testRecorder.record(10L) - val udp = setup(Map(testMetricName -> testRecorder.collect())) + val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"$testMetricKey:10|ms|@0.5") } "flush the packet when the max-packet-size is reached" in new UdpListenerFixture { - val testMetricName = "test-metric" - val testMetricKey = buildMetricKey(testMetricName) - val testRecorder = HdrRecorder(testMaxPacketSize, 3, Scale.Unit) + val testMetricName = "processing-time" + val testMetricKey = buildMetricKey("actor", "/user/kamon", testMetricName) + val testRecorder = Histogram(10000L, Precision.Normal, Scale.Unit) var bytes = testMetricKey.length var level = 0 @@ -84,7 +104,7 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers bytes += s":$level|ms".length } - val udp = setup(Map(testMetricName -> testRecorder.collect())) + val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext))) udp.expectMsgType[Udp.Send] // let the first flush pass val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] @@ -93,12 +113,12 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers "render multiple keys in the same packet using newline as separator" in new UdpListenerFixture { val firstTestMetricName = "first-test-metric" - val firstTestMetricKey = buildMetricKey(firstTestMetricName) + val firstTestMetricKey = buildMetricKey("actor", "/user/kamon", firstTestMetricName) val secondTestMetricName = "second-test-metric" - val secondTestMetricKey = buildMetricKey(secondTestMetricName) + val secondTestMetricKey = buildMetricKey("actor", "/user/kamon", secondTestMetricName) - val firstTestRecorder = HdrRecorder(1000L, 2, Scale.Unit) - val secondTestRecorder = HdrRecorder(1000L, 2, Scale.Unit) + val firstTestRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) + val secondTestRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) firstTestRecorder.record(10L) firstTestRecorder.record(10L) @@ -108,8 +128,8 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers secondTestRecorder.record(21L) val udp = setup(Map( - firstTestMetricName -> firstTestRecorder.collect(), - secondTestMetricName -> secondTestRecorder.collect())) + firstTestMetricName -> firstTestRecorder.collect(collectionContext), + secondTestMetricName -> secondTestRecorder.collect(collectionContext))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"$firstTestMetricKey:10|ms|@0.5:11|ms\n$secondTestMetricKey:20|ms:21|ms") @@ -117,14 +137,32 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers } trait UdpListenerFixture { - val localhostName = ManagementFactory.getRuntimeMXBean.getName.split('@')(1) val testMaxPacketSize = system.settings.config.getBytes("kamon.statsd.max-packet-size") + val metricKeyGenerator = new SimpleMetricKeyGenerator(system.settings.config) { + override def normalizedLocalhostName: String = "localhost_local" + } - def buildMetricKey(metricName: String): String = s"kamon.$localhostName.test-metric-category.test-group.$metricName" + val testGroupIdentity = new MetricGroupIdentity { + val name: String = "/user/kamon" + val category: MetricGroupCategory = new MetricGroupCategory { + val name: String = "actor" + } + } - def setup(metrics: Map[String, MetricSnapshotLike]): TestProbe = { + def buildMetricKey(categoryName: String, entityName: String, metricName: String): String = { + val metricIdentity = new MetricIdentity { val name: String = metricName } + val groupIdentity = new MetricGroupIdentity { + val name: String = entityName + val category: MetricGroupCategory = new MetricGroupCategory { + val name: String = categoryName + } + } + metricKeyGenerator.generateKey(groupIdentity, metricIdentity) + } + + def setup(metrics: Map[String, MetricSnapshot]): TestProbe = { val udp = TestProbe() - val metricsSender = system.actorOf(Props(new StatsDMetricsSender(new InetSocketAddress(localhostName, 0), testMaxPacketSize) { + val metricsSender = system.actorOf(Props(new StatsDMetricsSender(new InetSocketAddress("127.0.0.1", 0), testMaxPacketSize, metricKeyGenerator) { override def udpExtension(implicit system: ActorSystem): ActorRef = udp.ref })) @@ -132,24 +170,19 @@ class StatsDMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers udp.expectMsgType[Udp.SimpleSender] udp.reply(Udp.SimpleSenderReady) - val testGroupIdentity = new MetricGroupIdentity { - val name: String = "test-group" - val category: MetricGroupCategory = new MetricGroupCategory { - val name: String = "test-metric-category" - } - } - val testMetrics = for ((metricName, snapshot) ← metrics) yield { val testMetricIdentity = new MetricIdentity { val name: String = metricName - val tag: String = "" } (testMetricIdentity, snapshot) } metricsSender ! TickMetricSnapshot(0, 0, Map(testGroupIdentity -> new MetricGroupSnapshot { - val metrics: Map[MetricIdentity, MetricSnapshotLike] = testMetrics.toMap + type GroupSnapshotType = Histogram.Snapshot + def merge(that: GroupSnapshotType, context: CollectionContext): GroupSnapshotType = ??? + + val metrics: Map[MetricIdentity, MetricSnapshot] = testMetrics.toMap })) udp |