aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/test/scala/kamon/metric/MetricScaleDecoratorSpec.scala
blob: 902102cd2d8d9205fc01a5d00eb453a457a86174 (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
/*
 * =========================================================================================
 * Copyright © 2013-2015 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.metric

import kamon.Kamon
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot
import kamon.metric.instrument.{ InstrumentFactory, Memory, Time, UnitOfMeasurement }
import kamon.testkit.BaseKamonSpec
import kamon.util.MilliTimestamp
import org.scalatest.OptionValues._

class MetricScaleDecoratorSpec extends BaseKamonSpec("metrics-scale-decorator-spec") with SnapshotFixtures {
  "the MetricScaleDecorator" when {
    "receives a snapshot" which {

      val scaleDecorator = system.actorOf(MetricScaleDecorator.props(
        Some(Time.Milliseconds), Some(Memory.KiloBytes), testActor))
      "is empty" should {
        "do nothing for empty snapshots" in {
          scaleDecorator ! emptySnapshot
          expectMsg(emptySnapshot)
        }
      }
      "is non empty" should {
        scaleDecorator ! nonEmptySnapshot
        val scaled = expectMsgType[TickMetricSnapshot]
        val snapshot = scaled.metrics(testEntity)

        "scale time metrics" in {
          snapshot.histogram("nano-time").value.max should be(10L +- 1L)
          snapshot.counter("micro-time").value.count should be(1000L)
        }
        "scale memory metrics" in {
          snapshot.histogram("byte-memory").value.max should be(1)
          snapshot.counter("kbyte-memory").value.count should be(100L)
        }
        "do nothing with unknown metrics" in {
          snapshot.histogram("unknown-histogram").value.max should be(1000L)
          snapshot.counter("unknown-counter").value.count should be(10L)
        }
        "not change from and to" in {
          scaled.from.millis should be(1000)
          scaled.to.millis should be(2000)
        }
      }
    }
  }
}

trait SnapshotFixtures {
  self: BaseKamonSpec 

  class ScaleDecoratorTestMetrics(instrumentFactory: InstrumentFactory)
      extends GenericEntityRecorder(instrumentFactory) {
    val nanoTime = histogram("nano-time", Time.Nanoseconds)
    val microTime = counter("micro-time", Time.Microseconds)
    val byteMemory = histogram("byte-memory", Memory.Bytes)
    val kbyteMemory = counter("kbyte-memory", Memory.KiloBytes)
    val unknownHistogram = histogram("unknown-histogram", UnitOfMeasurement.Unknown)
    val unknownCounter = counter("unknown-counter", UnitOfMeasurement.Unknown)
  }

  object ScaleDecoratorTestMetrics extends EntityRecorderFactory[ScaleDecoratorTestMetrics] {
    override def category: String = "decorator-spec"

    override def createRecorder(instrumentFactory: InstrumentFactory): ScaleDecoratorTestMetrics =
      new ScaleDecoratorTestMetrics(instrumentFactory)
  }

  val testEntity = Entity("metrics-scale-decorator-spec", "decorator-spec")
  val recorder = Kamon.metrics.entity(ScaleDecoratorTestMetrics, "metrics-scale-decorator-spec")

  val emptySnapshot = TickMetricSnapshot(new MilliTimestamp(1000), new MilliTimestamp(2000), Map.empty)

  recorder.unknownCounter.increment(10)
  recorder.unknownHistogram.record(1000L)
  recorder.nanoTime.record(10000000L)
  recorder.microTime.increment(1000000L)
  recorder.byteMemory.record(1024L)
  recorder.kbyteMemory.increment(100L)

  val nonEmptySnapshot = TickMetricSnapshot(new MilliTimestamp(1000), new MilliTimestamp(2000), Map(
    (testEntity -> recorder.collect(collectionContext))))

}