From 29068fc70a3e5a17a630c2c7fff951572bb5fa21 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Thu, 3 Jul 2014 14:36:42 -0300 Subject: ! all: refactor the core metric recording instruments and accomodate UserMetrics This PR is including several changes to the kamon-core, most notably: - Formalize the interface for Histograms, Counters and MinMaxCounters. Making sure that the interfaces are as clean as possible. - Move away from the all Vector[Measurement] based Histogram snapshot to a new approach in which we use a single long to store both the index in the counts array and the frequency on that bucket. The leftmost 2 bytes of each long are used for storing the counts array index and the remaining 6 bytes are used for the actual count, and everything is put into a simple long array. This way only the buckets that actually have values will be included in the snapshot with the smallest possible memory footprint. - Introduce Gauges. - Reorganize the instrumentation for Akka and Scala and rewrite most of the tests of this components to avoid going through the subscription protocol to test. - Introduce trace tests and fixes on various tests. - Necessary changes on new relic, datadog and statsd modules to compile with the new codebase. Pending: - Finish the upgrade of the new relic to the current model. - Introduce proper limit checks for histograms to ensure that we never pass the 2/6 bytes limits. - More testing, more testing, more testing. - Create the KamonStandalone module. --- .../src/main/scala/kamon/datadog/Datadog.scala | 2 +- .../scala/kamon/datadog/DatadogMetricsSender.scala | 47 ++++++++++--------- .../kamon/datadog/DatadogMetricSenderSpec.scala | 54 +++++++++++----------- 3 files changed, 53 insertions(+), 50 deletions(-) (limited to 'kamon-datadog/src') diff --git a/kamon-datadog/src/main/scala/kamon/datadog/Datadog.scala b/kamon-datadog/src/main/scala/kamon/datadog/Datadog.scala index 15d5d3fe..b4358ce7 100644 --- a/kamon-datadog/src/main/scala/kamon/datadog/Datadog.scala +++ b/kamon-datadog/src/main/scala/kamon/datadog/Datadog.scala @@ -18,7 +18,7 @@ package kamon.datadog import akka.actor._ import kamon.Kamon -import kamon.metrics._ +import kamon.metric._ import scala.concurrent.duration._ import scala.collection.JavaConverters._ import com.typesafe.config.Config diff --git a/kamon-datadog/src/main/scala/kamon/datadog/DatadogMetricsSender.scala b/kamon-datadog/src/main/scala/kamon/datadog/DatadogMetricsSender.scala index 028e9608..17e19d0b 100644 --- a/kamon-datadog/src/main/scala/kamon/datadog/DatadogMetricsSender.scala +++ b/kamon-datadog/src/main/scala/kamon/datadog/DatadogMetricsSender.scala @@ -20,11 +20,10 @@ import akka.actor.{ ActorSystem, Props, ActorRef, Actor } import akka.io.{ Udp, IO } import java.net.InetSocketAddress import akka.util.ByteString -import kamon.metrics.Subscriptions.TickMetricSnapshot -import kamon.metrics.MetricSnapshot.Measurement -import kamon.metrics.InstrumentTypes.{ Counter, Gauge, Histogram, InstrumentType } +import kamon.metric.Subscriptions.TickMetricSnapshot import java.text.{ DecimalFormatSymbols, DecimalFormat } -import kamon.metrics.{ MetricIdentity, MetricGroupIdentity } +import kamon.metric.instrument.{ Counter, Histogram } +import kamon.metric.{ MetricIdentity, MetricGroupIdentity } import java.util.Locale class DatadogMetricsSender(remote: InetSocketAddress, maxPacketSizeInBytes: Long) extends Actor with UdpExtensionProvider { @@ -50,7 +49,7 @@ class DatadogMetricsSender(remote: InetSocketAddress, maxPacketSizeInBytes: Long } def writeMetricsToRemote(tick: TickMetricSnapshot, udpSender: ActorRef): Unit = { - val dataBuilder = new MetricDataPacketBuilder(maxPacketSizeInBytes, udpSender, remote) + val packetBuilder = new MetricDataPacketBuilder(maxPacketSizeInBytes, udpSender, remote) for { (groupIdentity, groupSnapshot) ← tick.metrics @@ -59,33 +58,35 @@ class DatadogMetricsSender(remote: InetSocketAddress, maxPacketSizeInBytes: Long val key = buildMetricName(groupIdentity, metricIdentity) - for (measurement ← metricSnapshot.measurements) { - val measurementData = formatMeasurement(groupIdentity, metricIdentity, measurement, metricSnapshot.instrumentType) - dataBuilder.appendMeasurement(key, measurementData) + metricSnapshot match { + case hs: Histogram.Snapshot ⇒ + hs.recordsIterator.foreach { record ⇒ + val measurementData = formatMeasurement(groupIdentity, metricIdentity, encodeStatsDTimer(record.level, record.count)) + packetBuilder.appendMeasurement(key, measurementData) + + } + + case cs: Counter.Snapshot ⇒ + val measurementData = formatMeasurement(groupIdentity, metricIdentity, encodeStatsDCounter(cs.count)) + packetBuilder.appendMeasurement(key, measurementData) } } - dataBuilder.flush() + packetBuilder.flush() } - def formatMeasurement(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity, measurement: Measurement, - instrumentType: InstrumentType): String = { - - StringBuilder.newBuilder.append(buildMeasurementData(measurement, instrumentType)) + def formatMeasurement(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity, measurementData: String): String = + StringBuilder.newBuilder + .append(measurementData) .append(buildIdentificationTag(groupIdentity, metricIdentity)) .result() - } - def buildMeasurementData(measurement: Measurement, instrumentType: InstrumentType): String = { - def dataDogDMetricFormat(value: String, metricType: String, samplingRate: Double = 1D): String = - s"$value|$metricType${(if (samplingRate != 1D) "|@" + samplingRateFormat.format(samplingRate) else "")}" - - instrumentType match { - case Histogram ⇒ dataDogDMetricFormat(measurement.value.toString, "ms", (1D / measurement.count)) - case Gauge ⇒ dataDogDMetricFormat(measurement.value.toString, "g") - case Counter ⇒ dataDogDMetricFormat(measurement.count.toString, "c") - } + def encodeStatsDTimer(level: Long, count: Long): String = { + val samplingRate: Double = 1D / count + level.toString + "|ms" + (if (samplingRate != 1D) "|@" + samplingRateFormat.format(samplingRate) else "") } + def encodeStatsDCounter(count: Long): String = count.toString + "|c" + def buildMetricName(groupIdentity: MetricGroupIdentity, metricIdentity: MetricIdentity): String = s"$appName.${groupIdentity.category.name}.${metricIdentity.name}" diff --git a/kamon-datadog/src/test/scala/kamon/datadog/DatadogMetricSenderSpec.scala b/kamon-datadog/src/test/scala/kamon/datadog/DatadogMetricSenderSpec.scala index 6a7191a1..cb82c362 100644 --- a/kamon-datadog/src/test/scala/kamon/datadog/DatadogMetricSenderSpec.scala +++ b/kamon-datadog/src/test/scala/kamon/datadog/DatadogMetricSenderSpec.scala @@ -16,14 +16,14 @@ package kamon.datadog -import akka.testkit.{TestKitBase, TestProbe} -import akka.actor.{Props, ActorRef, ActorSystem} -import kamon.metrics.instruments.CounterRecorder -import org.scalatest.{Matchers, WordSpecLike} -import kamon.metrics._ +import akka.testkit.{ TestKitBase, TestProbe } +import akka.actor.{ Props, ActorRef, ActorSystem } +import kamon.metric.instrument.Histogram.Precision +import kamon.metric.instrument.{ Counter, Histogram, HdrHistogram, LongAdderCounter } +import org.scalatest.{ Matchers, WordSpecLike } +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 @@ -32,13 +32,15 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher implicit lazy val system = ActorSystem("datadog-metric-sender-spec", ConfigFactory.parseString("kamon.datadog.max-packet-size = 256 bytes")) + val context = CollectionContext.default + "the DataDogMetricSender" should { "send latency measurements" in new UdpListenerFixture { val testMetricName = "processing-time" - val testRecorder = HdrRecorder(1000L, 2, Scale.Unit) + 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(context))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"kamon.actor.processing-time:10|ms|#actor:user/kamon") @@ -46,11 +48,11 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher "include the sampling rate in case of multiple measurements of the same value" in new UdpListenerFixture { val testMetricName = "processing-time" - val testRecorder = HdrRecorder(1000L, 2, Scale.Unit) + 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(context))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"kamon.actor.processing-time:10|ms|@0.5|#actor:user/kamon") @@ -58,7 +60,7 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher "flush the packet when the max-packet-size is reached" in new UdpListenerFixture { val testMetricName = "processing-time" - val testRecorder = HdrRecorder(testMaxPacketSize, 3, Scale.Unit) + val testRecorder = Histogram(10000L, Precision.Normal, Scale.Unit) var bytes = 0 var level = 0 @@ -69,8 +71,8 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher bytes += s"kamon.actor.$testMetricName:$level|ms|#actor:user/kamon".length } - val udp = setup(Map(testMetricName -> testRecorder.collect())) - udp.expectMsgType[Udp.Send]// let the first flush pass + val udp = setup(Map(testMetricName -> testRecorder.collect(context))) + udp.expectMsgType[Udp.Send] // let the first flush pass val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be(s"kamon.actor.$testMetricName:$level|ms|#actor:user/kamon") @@ -81,24 +83,21 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher val secondTestMetricName = "processing-time-2" val thirdTestMetricName = "counter" - val firstTestRecorder = HdrRecorder(1000L, 2, Scale.Unit) - val secondTestRecorder = HdrRecorder(1000L, 2, Scale.Unit) - val thirdTestRecorder = CounterRecorder() + val firstTestRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) + val secondTestRecorder = Histogram(1000L, Precision.Normal, Scale.Unit) + val thirdTestRecorder = Counter() firstTestRecorder.record(10L) firstTestRecorder.record(10L) secondTestRecorder.record(21L) - thirdTestRecorder.record(1L) - thirdTestRecorder.record(1L) - thirdTestRecorder.record(1L) - thirdTestRecorder.record(1L) + thirdTestRecorder.increment(4L) val udp = setup(Map( - firstTestMetricName -> firstTestRecorder.collect(), - secondTestMetricName -> secondTestRecorder.collect(), - thirdTestMetricName -> thirdTestRecorder.collect())) + firstTestMetricName -> firstTestRecorder.collect(context), + secondTestMetricName -> secondTestRecorder.collect(context), + thirdTestMetricName -> thirdTestRecorder.collect(context))) val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send] data.utf8String should be("kamon.actor.processing-time-1:10|ms|@0.5|#actor:user/kamon\nkamon.actor.processing-time-2:21|ms|#actor:user/kamon\nkamon.actor.counter:4|c|#actor:user/kamon") @@ -109,7 +108,7 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher val localhostName = ManagementFactory.getRuntimeMXBean.getName.split('@')(1) val testMaxPacketSize = system.settings.config.getBytes("kamon.datadog.max-packet-size") - def setup(metrics: Map[String, MetricSnapshotLike]): TestProbe = { + def setup(metrics: Map[String, MetricSnapshot]): TestProbe = { val udp = TestProbe() val metricsSender = system.actorOf(Props(new DatadogMetricsSender(new InetSocketAddress(localhostName, 0), testMaxPacketSize) { override def udpExtension(implicit system: ActorSystem): ActorRef = udp.ref @@ -137,7 +136,10 @@ class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matcher } 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 } -- cgit v1.2.3