aboutsummaryrefslogtreecommitdiff
path: root/kamon-testkit/src/main/scala/kamon/testkit/MetricInspection.scala
blob: d0681fb58375dd3509e0c6bacfbb01bc39d28fcf (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
package kamon.testkit

import kamon.metric._
import _root_.scala.collection.concurrent.TrieMap


trait MetricInspection {

  implicit class MetricSyntax(metric: Metric[_]) {
    def valuesForTag(tag: String): Seq[String] = {
      val instrumentsField = classOf[BaseMetric[_, _]].getDeclaredField("instruments")
      instrumentsField.setAccessible(true)

      val instruments = instrumentsField.get(metric).asInstanceOf[TrieMap[Map[String, String], _]]
      val instrumentsWithTheTag = instruments.keys.filter(_.keys.find(_ == tag).nonEmpty)
      instrumentsWithTheTag.map(t => t(tag)).toSeq
    }
  }

  implicit class HistogramMetricSyntax(histogram: Histogram) {
    def distribution(resetState: Boolean = true): Distribution =
      histogram match {
        case hm: HistogramMetric    => hm.refine(Map.empty[String, String]).distribution(resetState)
        case h: AtomicHdrHistogram  => h.snapshot(resetState).distribution
        case h: HdrHistogram        => h.snapshot(resetState).distribution
      }
  }

  implicit class MinMaxCounterMetricSyntax(mmCounter: MinMaxCounter) {
    def distribution(resetState: Boolean = true): Distribution =
      mmCounter match {
        case mmcm: MinMaxCounterMetric  => mmcm.refine(Map.empty[String, String]).distribution(resetState)
        case mmc: SimpleMinMaxCounter   => mmc.snapshot(resetState).distribution
      }
  }

  implicit class CounterMetricSyntax(counter: Counter) {
    def value(resetState: Boolean = true): Long =
      counter match {
        case cm: CounterMetric    => cm.refine(Map.empty[String, String]).value(resetState)
        case c: LongAdderCounter  => c.snapshot(resetState).value
      }
  }
}