diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-08-24 11:23:21 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-08-24 11:23:21 +0200 |
commit | cd54e4ed73734dbabebbf22e4fa288c9b047992e (patch) | |
tree | 73a94488fb9097c3e3fe1ad0271f9caadd53f6cf /kamon-testkit/src/main/scala/kamon/testkit | |
parent | 5291089bcc6bb048dcecad2c931f3408bd539574 (diff) | |
download | Kamon-cd54e4ed73734dbabebbf22e4fa288c9b047992e.tar.gz Kamon-cd54e4ed73734dbabebbf22e4fa288c9b047992e.tar.bz2 Kamon-cd54e4ed73734dbabebbf22e4fa288c9b047992e.zip |
introduce the SpanCustomizer API
Diffstat (limited to 'kamon-testkit/src/main/scala/kamon/testkit')
-rw-r--r-- | kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala | 64 | ||||
-rw-r--r-- | kamon-testkit/src/main/scala/kamon/testkit/SpanInspector.scala | 61 |
2 files changed, 64 insertions, 61 deletions
diff --git a/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala b/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala new file mode 100644 index 00000000..c4a83a73 --- /dev/null +++ b/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala @@ -0,0 +1,64 @@ +package kamon.testkit + +import kamon.trace.{Span, SpanContext} +import kamon.trace.Span.FinishedSpan +import kamon.util.Clock + +import scala.reflect.ClassTag +import scala.util.Try + +trait SpanInspection { + + def inspect(span: Span): SpanInspection.Inspector = + new SpanInspection.Inspector(span) +} + +object SpanInspection { + + class Inspector(span: Span) { + private val (realSpan, spanData) = Try { + val realSpan = span match { + case _: Span.Local => span + } + + val spanData = invoke[Span.Local, FinishedSpan](realSpan, "toFinishedSpan", classOf[Long] -> Long.box(Clock.microTimestamp())) + (realSpan, spanData) + }.getOrElse((null, null)) + + def isEmpty: Boolean = + realSpan == null + + def spanTag(key: String): Option[Span.TagValue] = + spanData.tags.get(key) + + def spanTags(): Map[String, Span.TagValue] = + spanData.tags + + def metricTags(): Map[String, String] = + getField[Span.Local, Map[String, String]](realSpan, "customMetricTags") + + def startTimestamp(): Long = + getField[Span.Local, Long](realSpan, "startTimestampMicros") + + def context(): SpanContext = + spanData.context + + def operationName(): String = + spanData.operationName + + + private def getField[T, R](target: Any, fieldName: String)(implicit classTag: ClassTag[T]): R = { + val toFinishedSpanMethod = classTag.runtimeClass.getDeclaredFields.find(_.getName.contains(fieldName)).get + toFinishedSpanMethod.setAccessible(true) + toFinishedSpanMethod.get(target).asInstanceOf[R] + } + + private def invoke[T, R](target: Any, fieldName: String, parameters: (Class[_], AnyRef)*)(implicit classTag: ClassTag[T]): R = { + val parameterClasses = parameters.map(_._1) + val parameterInstances = parameters.map(_._2) + val toFinishedSpanMethod = classTag.runtimeClass.getDeclaredMethod(fieldName, parameterClasses: _*) + toFinishedSpanMethod.setAccessible(true) + toFinishedSpanMethod.invoke(target, parameterInstances: _*).asInstanceOf[R] + } + } +}
\ No newline at end of file diff --git a/kamon-testkit/src/main/scala/kamon/testkit/SpanInspector.scala b/kamon-testkit/src/main/scala/kamon/testkit/SpanInspector.scala deleted file mode 100644 index 25f7ee06..00000000 --- a/kamon-testkit/src/main/scala/kamon/testkit/SpanInspector.scala +++ /dev/null @@ -1,61 +0,0 @@ -package kamon.testkit - -import kamon.trace.{Span, SpanContext} -import kamon.trace.Span.FinishedSpan -import kamon.util.Clock - -import scala.reflect.ClassTag -import scala.util.Try - -class SpanInspector(span: Span) { - private val (realSpan, spanData) = Try { - val realSpan = span match { - case _: Span.Local => span - } - - val spanData = invoke[Span.Local, FinishedSpan](realSpan, "toFinishedSpan", classOf[Long] -> Long.box(Clock.microTimestamp())) - (realSpan, spanData) - }.getOrElse((null, null)) - - def isEmpty: Boolean = - realSpan == null - - def spanTag(key: String): Option[Span.TagValue] = - spanData.tags.get(key) - - def spanTags(): Map[String, Span.TagValue] = - spanData.tags - - def metricTags(): Map[String, String] = - getField[Span.Local, Map[String, String]](realSpan, "customMetricTags") - - def startTimestamp(): Long = - getField[Span.Local, Long](realSpan, "startTimestampMicros") - - def context(): SpanContext = - spanData.context - - def operationName(): String = - spanData.operationName - - - - - private def getField[T, R](target: Any, fieldName: String)(implicit classTag: ClassTag[T]): R = { - val toFinishedSpanMethod = classTag.runtimeClass.getDeclaredFields.find(_.getName.contains(fieldName)).get - toFinishedSpanMethod.setAccessible(true) - toFinishedSpanMethod.get(target).asInstanceOf[R] - } - - private def invoke[T, R](target: Any, fieldName: String, parameters: (Class[_], AnyRef)*)(implicit classTag: ClassTag[T]): R = { - val parameterClasses = parameters.map(_._1) - val parameterInstances = parameters.map(_._2) - val toFinishedSpanMethod = classTag.runtimeClass.getDeclaredMethod(fieldName, parameterClasses: _*) - toFinishedSpanMethod.setAccessible(true) - toFinishedSpanMethod.invoke(target, parameterInstances: _*).asInstanceOf[R] - } -} - -object SpanInspector { - def apply(span: Span): SpanInspector = new SpanInspector(span) -} |