diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-20 14:58:48 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-20 14:58:48 +0200 |
commit | 5c8a8d169858b83a059c89e48cb43a41040788b8 (patch) | |
tree | 929a625053d48be75da1d25632d156fe968ea41b /kamon-core/src/test/scala | |
parent | 73246cb9c4b796a7590aa4b8d62aedd55d81d6f6 (diff) | |
download | Kamon-5c8a8d169858b83a059c89e48cb43a41040788b8.tar.gz Kamon-5c8a8d169858b83a059c89e48cb43a41040788b8.tar.bz2 Kamon-5c8a8d169858b83a059c89e48cb43a41040788b8.zip |
add metric inspection facility for tests
Diffstat (limited to 'kamon-core/src/test/scala')
-rw-r--r-- | kamon-core/src/test/scala/kamon/testkit/MetricInspection.scala | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/kamon-core/src/test/scala/kamon/testkit/MetricInspection.scala b/kamon-core/src/test/scala/kamon/testkit/MetricInspection.scala new file mode 100644 index 00000000..d0681fb5 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/testkit/MetricInspection.scala @@ -0,0 +1,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 + } + } +} + |