aboutsummaryrefslogtreecommitdiff
path: root/kamon-datadog/src/test/scala/kamon/datadog/DatadogMetricSenderSpec.scala
blob: 91b503e2208a2460b1a2841952ca5f519a7c56b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * =========================================================================================
 * Copyright © 2013-2014 the kamon project <http://kamon.io/>
 *
 * 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.datadog

import akka.testkit.{ TestKitBase, TestProbe }
import akka.actor.{ Props, ActorRef, ActorSystem }
import kamon.Kamon
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 kamon.metric.Subscriptions.TickMetricSnapshot
import java.lang.management.ManagementFactory
import java.net.InetSocketAddress
import com.typesafe.config.ConfigFactory

class DatadogMetricSenderSpec extends TestKitBase with WordSpecLike with Matchers {
  implicit lazy val system = ActorSystem("datadog-metric-sender-spec",
    ConfigFactory.parseString("kamon.datadog.max-packet-size = 256 bytes"))

  val collectionContext = Kamon(Metrics).buildDefaultCollectionContext

  "the DataDogMetricSender" should {
    "send latency measurements" in new UdpListenerFixture {
      val testMetricName = "processing-time"
      val testRecorder = Histogram(1000L, Precision.Normal, Scale.Unit)
      testRecorder.record(10L)

      val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext)))
      val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send]

      data.utf8String should be(s"kamon.actor.processing-time:10|ms|#actor:user/kamon")
    }

    "include the sampling rate in case of multiple measurements of the same value" in new UdpListenerFixture {
      val testMetricName = "processing-time"
      val testRecorder = Histogram(1000L, Precision.Normal, Scale.Unit)
      testRecorder.record(10L)
      testRecorder.record(10L)

      val udp = setup(Map(testMetricName -> testRecorder.collect(collectionContext)))
      val Udp.Send(data, _, _) = udp.expectMsgType[Udp.Send]

      data.utf8String should be(s"kamon.actor.processing-time:10|ms|@0.5|#actor:user/kamon")
    }

    "flush the packet when the max-packet-size is reached" in new UdpListenerFixture {
      val testMetricName = "processing-time"
      val testRecorder = Histogram(10000L, Precision.Normal, Scale.Unit)

      var bytes = 0
      var level = 0

      while (bytes <= testMaxPacketSize) {
        level += 1
        testRecorder.record(level)
        bytes += s"kamon.actor.$testMetricName:$level|ms|#actor:user/kamon".length
      }

      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]
      data.utf8String should be(s"kamon.actor.$testMetricName:$level|ms|#actor:user/kamon")
    }

    "render multiple keys in the same packet using newline as separator" in new UdpListenerFixture {
      val firstTestMetricName = "processing-time-1"
      val secondTestMetricName = "processing-time-2"
      val thirdTestMetricName = "counter"

      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.increment(4L)

      val udp = setup(Map(
        firstTestMetricName -> firstTestRecorder.collect(collectionContext),
        secondTestMetricName -> secondTestRecorder.collect(collectionContext),
        thirdTestMetricName -> thirdTestRecorder.collect(collectionContext)))
      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")
    }
  }

  trait UdpListenerFixture {
    val localhostName = ManagementFactory.getRuntimeMXBean.getName.split('@')(1)
    val testMaxPacketSize = system.settings.config.getBytes("kamon.datadog.max-packet-size")

    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
      }))

      // Setup the SimpleSender
      udp.expectMsgType[Udp.SimpleSender]
      udp.reply(Udp.SimpleSenderReady)

      // These names are not intented to match the real actor metrics, it's just about seeing more familiar data in tests.
      val testGroupIdentity = new MetricGroupIdentity {
        val name: String = "user/kamon"
        val category: MetricGroupCategory = new MetricGroupCategory {
          val name: String = "actor"
        }
      }

      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 {
        type GroupSnapshotType = Histogram.Snapshot
        def merge(that: GroupSnapshotType, context: CollectionContext): GroupSnapshotType = ???

        val metrics: Map[MetricIdentity, MetricSnapshot] = testMetrics.toMap
      }))
      udp
    }
  }

}