From 09af20cca6243400e0b922113d896e1ddc46382c Mon Sep 17 00:00:00 2001 From: mladens Date: Wed, 12 Jul 2017 12:05:56 +0200 Subject: Separate span metrics and counts for spans finished with an error --- .../src/test/scala/kamon/trace/SpanMetrics.scala | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala (limited to 'kamon-core/src/test/scala') diff --git a/kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala b/kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala new file mode 100644 index 00000000..6f32e000 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala @@ -0,0 +1,70 @@ +package kamon.trace + +import kamon.Kamon +import kamon.Kamon.buildSpan +import kamon.metric._ +import org.scalatest.{Matchers, WordSpecLike} + +class SpanMetrics extends WordSpecLike with Matchers { + import SpanMetricsTestHelper._ + + val errorTag = "error" -> Span.BooleanTagTrueValue + val histogramMetric: HistogramMetric = Kamon.histogram("span.elapsed-time") + + "Span Metrics" should { + "be recorded for successeful execution" in { + val operation = "span-success" + val operationTag = "operation" -> operation + + val span = buildSpan(operation).startManual() + span.finish() + + + val histogram = histogramMetric.refine(operationTag) + histogram.distribution().count === 1 + + val errorHistogram = histogramMetric.refine(operationTag, errorTag).distribution() + errorHistogram.count === 0 + + val errorCount = histogramMetric.refine(operationTag, errorTag).distribution() + errorCount === 0 + } + + "record correctly error latency and count" in { + val operation = "span-failure" + val operationTag = "operation" -> operation + + val span = buildSpan(operation).startManual() + span.setTag("error", Span.BooleanTagTrueValue) + span.finish() + + val histogram = histogramMetric.refine(operationTag) + histogram.distribution().count === 0 + + val errorHistogram = histogramMetric.refine(operationTag, errorTag).distribution() + errorHistogram.count === 1 + + val errorCount = histogramMetric.refine(operationTag, errorTag).distribution() + errorCount === 1 + } + } + +} + +object SpanMetricsTestHelper { + + 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 + } + } + + + +} + + + -- cgit v1.2.3