diff options
Diffstat (limited to 'kamon-testkit/src')
3 files changed, 23 insertions, 62 deletions
diff --git a/kamon-testkit/src/main/scala/kamon/testkit/ContextTesting.scala b/kamon-testkit/src/main/scala/kamon/testkit/ContextTesting.scala index 9f17dff0..d36d0df2 100644 --- a/kamon-testkit/src/main/scala/kamon/testkit/ContextTesting.scala +++ b/kamon-testkit/src/main/scala/kamon/testkit/ContextTesting.scala @@ -15,14 +15,17 @@ package kamon.testkit -import kamon.context.{Context, Key} +import kamon.context.{Context} trait ContextTesting { - val StringKey = Key.local[Option[String]]("string-key", None) - val StringBroadcastKey = Key.broadcastString("string-broadcast-key") + val StringKey = Context.key[Option[String]]("string-key", None) + val StringBroadcastKey = Context.key[Option[String]]("string-broadcast-key", None) def contextWithLocal(value: String): Context = - Context.create(StringKey, Some(value)) + Context.of(StringKey, Some(value)) + + + } object ContextTesting extends ContextTesting diff --git a/kamon-testkit/src/main/scala/kamon/testkit/SimpleStringCodec.scala b/kamon-testkit/src/main/scala/kamon/testkit/SimpleStringCodec.scala deleted file mode 100644 index c755b0af..00000000 --- a/kamon-testkit/src/main/scala/kamon/testkit/SimpleStringCodec.scala +++ /dev/null @@ -1,57 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2017 the kamon project <http://kamon.io/> - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - -package kamon.testkit - -import java.nio.ByteBuffer - -import kamon.context.{Codecs, Context, TextMap} - -object SimpleStringCodec { - final class Headers extends Codecs.ForEntry[TextMap] { - private val dataKey = "X-String-Value" - - override def encode(context: Context): TextMap = { - val textMap = TextMap.Default() - context.get(ContextTesting.StringBroadcastKey).foreach { value => - textMap.put(dataKey, value) - } - - textMap - } - - override def decode(carrier: TextMap, context: Context): Context = { - carrier.get(dataKey) match { - case value @ Some(_) => context.withKey(ContextTesting.StringBroadcastKey, value) - case None => context - } - } - } - - final class Binary extends Codecs.ForEntry[ByteBuffer] { - val emptyBuffer: ByteBuffer = ByteBuffer.allocate(0) - - override def encode(context: Context): ByteBuffer = { - context.get(ContextTesting.StringBroadcastKey) match { - case Some(value) => ByteBuffer.wrap(value.getBytes) - case None => emptyBuffer - } - } - - override def decode(carrier: ByteBuffer, context: Context): Context = { - context.withKey(ContextTesting.StringBroadcastKey, Some(new String(carrier.array()))) - } - } -} diff --git a/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala b/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala index fbfdc7c3..c28ace64 100644 --- a/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala +++ b/kamon-testkit/src/main/scala/kamon/testkit/SpanInspection.scala @@ -19,7 +19,7 @@ import java.time.Instant import kamon.Kamon import kamon.trace.{Span, SpanContext} -import kamon.trace.Span.FinishedSpan +import kamon.trace.Span.{FinishedSpan, TagValue} import scala.reflect.ClassTag import scala.util.Try @@ -52,9 +52,21 @@ object SpanInspection { def spanTags(): Map[String, Span.TagValue] = spanData.tags + def tag(key: String): Option[String] = { + spanTag(key).map { + case TagValue.String(string) => string + case TagValue.Number(number) => number.toString + case TagValue.True => "true" + case TagValue.False => "false" + } orElse(metricTag(key)) + } + def metricTags(): Map[String, String] = getField[Span.Local, Map[String, String]](realSpan, "customMetricTags") + def metricTag(key: String): Option[String] = + metricTags().get(key) + def from(): Instant = getField[Span.Local, Instant](realSpan, "from") @@ -64,6 +76,9 @@ object SpanInspection { def operationName(): String = spanData.operationName + def hasMetricsEnabled(): Boolean = + getField[Span.Local, Boolean](realSpan, "collectMetrics") + private def getField[T, R](target: Any, fieldName: String)(implicit classTag: ClassTag[T]): R = { val toFinishedSpanMethod = classTag.runtimeClass.getDeclaredFields.find(_.getName.contains(fieldName)).get |