aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kamon-core/src/main/scala/kamon/trace/Span.scala19
-rw-r--r--kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala64
2 files changed, 75 insertions, 8 deletions
diff --git a/kamon-core/src/main/scala/kamon/trace/Span.scala b/kamon-core/src/main/scala/kamon/trace/Span.scala
index 161042d5..a4424a45 100644
--- a/kamon-core/src/main/scala/kamon/trace/Span.scala
+++ b/kamon-core/src/main/scala/kamon/trace/Span.scala
@@ -59,7 +59,7 @@ trait Span {
def annotate(timestampMicroseconds: Long, name: String, fields: Map[String, String]): Span =
annotate(Span.Annotation(timestampMicroseconds, name, fields))
-
+
}
object Span {
@@ -168,14 +168,17 @@ object Span {
val elapsedTime = endTimestampMicros - startTimestampMicros
val metricTags = Map("operation" -> operationName) ++ customMetricTags
- val latencyHistogram = Span.Metrics.SpanProcessingTimeMetric.refine(metricTags)
- latencyHistogram.record(elapsedTime)
-
- spanTags.get("error").foreach { errorTag =>
- if(errorTag != null && errorTag.equals(TagValue.True)) {
- Span.Metrics.SpanErrorCount.refine(metricTags).increment()
- }
+ val isError = spanTags.get("error").exists {
+ errorTag => errorTag != null && errorTag.equals(Span.TagValue.True)
}
+
+ val refinedMetricTags = if(isError)
+ metricTags + ("error" -> "true")
+ else
+ metricTags
+
+ val latencyHistogram = Span.Metrics.SpanProcessingTimeMetric.refine(refinedMetricTags)
+ latencyHistogram.record(elapsedTime)
}
}
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..9ecffb24
--- /dev/null
+++ b/kamon-core/src/test/scala/kamon/trace/SpanMetrics.scala
@@ -0,0 +1,64 @@
+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" -> "true"
+ 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
+
+ buildSpan(operation)
+ .start()
+ .finish()
+
+ val histogram = histogramMetric.refine(operationTag)
+ histogram.distribution().count === 1
+
+ val errorHistogram = histogramMetric.refine(Map(operationTag, errorTag)).distribution()
+ errorHistogram.count === 0
+
+ }
+
+ "record correctly error latency and count" in {
+ val operation = "span-failure"
+ val operationTag = "operation" -> operation
+
+ buildSpan(operation)
+ .start()
+ .addSpanTag("error", true)
+ .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
+ }
+ }
+}
+
+
+