aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala
blob: a4ce98822f4cd5650e75ffc2f66373789b5b6c7a (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
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

    }

    "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

    }
  }

}

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



}