diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-08-14 23:28:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 23:28:52 +0200 |
commit | a949c875684d78818224cd2ca7aaf79aa7878724 (patch) | |
tree | ce84ab802ba3c543b4b107e32b7cac4dea610fc4 /kamon-core/src/test/scala/kamon | |
parent | 18b9fc25d556fef50c5033f8880fab2594783caa (diff) | |
parent | 3144a04ea42a4e333b7a608597c07c0458c9f147 (diff) | |
download | Kamon-a949c875684d78818224cd2ca7aaf79aa7878724.tar.gz Kamon-a949c875684d78818224cd2ca7aaf79aa7878724.tar.bz2 Kamon-a949c875684d78818224cd2ca7aaf79aa7878724.zip |
Merge pull request #1 from ivantopo/wip/context-management-reloaded
remove context management from the Tracer
Diffstat (limited to 'kamon-core/src/test/scala/kamon')
-rw-r--r-- | kamon-core/src/test/scala/kamon/LogInterceptor.scala | 30 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/context/ContextCodecSpec.scala | 18 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/context/ThreadLocalStorageSpec.scala | 41 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/testkit/SpanBuilding.scala | 16 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala | 8 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala | 65 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/B3SpanCodecSpec.scala (renamed from kamon-core/src/test/scala/kamon/trace/ExtendedB3SpanContextCodecSpec.scala) | 108 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/DefaultIdentityGeneratorSpec.scala | 4 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/DoubleLengthTraceIdentityGeneratorSpec.scala | 4 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/LocalSpanSpec.scala (renamed from kamon-core/src/test/scala/kamon/trace/RealSpanSpec.scala) | 82 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/TracerSpec.scala | 83 |
11 files changed, 128 insertions, 331 deletions
diff --git a/kamon-core/src/test/scala/kamon/LogInterceptor.scala b/kamon-core/src/test/scala/kamon/LogInterceptor.scala deleted file mode 100644 index 76480a2f..00000000 --- a/kamon-core/src/test/scala/kamon/LogInterceptor.scala +++ /dev/null @@ -1,30 +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 - -//import uk.org.lidalia.slf4jext.Level -//import uk.org.lidalia.slf4jtest.{LoggingEvent, TestLogger} -// -//trait LogInterceptor { -// -// def interceptLog[T](level: Level)(code: => T)(implicit tl: TestLogger): Seq[LoggingEvent] = { -// import scala.collection.JavaConverters._ -// tl.clear() -// val run = code -// tl.getLoggingEvents().asScala.filter(_.getLevel == level) -// } -//} diff --git a/kamon-core/src/test/scala/kamon/context/ContextCodecSpec.scala b/kamon-core/src/test/scala/kamon/context/ContextCodecSpec.scala new file mode 100644 index 00000000..11be85a7 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/context/ContextCodecSpec.scala @@ -0,0 +1,18 @@ +package kamon.context + +import kamon.Kamon +import org.scalatest.{Matchers, WordSpec} + +class ContextCodecSpec extends WordSpec with Matchers { + "the Context Codec" when { + "encoding/decoding to HttpHeaders" should { + "encode stuff" in { + + + + } + } + } + + val ContextCodec = new Codec(Kamon.identityProvider, Kamon.config()) +} diff --git a/kamon-core/src/test/scala/kamon/context/ThreadLocalStorageSpec.scala b/kamon-core/src/test/scala/kamon/context/ThreadLocalStorageSpec.scala new file mode 100644 index 00000000..39f316ba --- /dev/null +++ b/kamon-core/src/test/scala/kamon/context/ThreadLocalStorageSpec.scala @@ -0,0 +1,41 @@ +package kamon.context + + +import org.scalatest.{Matchers, WordSpec} + +class ThreadLocalStorageSpec extends WordSpec with Matchers { + + "the Storage.ThreadLocal implementation of Context storage" should { + "return a empty context when no context has been set" in { + TLS.current() shouldBe Context.Empty + } + + "return the empty value for keys that have not been set in the context" in { + TLS.current().get(TestKey) shouldBe 42 + TLS.current().get(AnotherKey) shouldBe 99 + TLS.current().get(BroadcastKey) shouldBe "i travel around" + + ScopeWithKey.get(TestKey) shouldBe 43 + ScopeWithKey.get(AnotherKey) shouldBe 99 + ScopeWithKey.get(BroadcastKey) shouldBe "i travel around" + } + + "allow setting a context as current and remove it when closing the Scope" in { + TLS.current() shouldBe Context.Empty + + val scope = TLS.store(ScopeWithKey) + TLS.current() shouldBe theSameInstanceAs(ScopeWithKey) + scope.close() + + TLS.current() shouldBe Context.Empty + } + + + } + + val TLS: Storage = new Storage.ThreadLocal + val TestKey = Key.local("test-key", 42) + val AnotherKey = Key.local("another-key", 99) + val BroadcastKey = Key.broadcast("broadcast", "i travel around") + val ScopeWithKey = Context.create().withKey(TestKey, 43) +} diff --git a/kamon-core/src/test/scala/kamon/testkit/SpanBuilding.scala b/kamon-core/src/test/scala/kamon/testkit/SpanBuilding.scala index 9b845ac9..7a216ecc 100644 --- a/kamon-core/src/test/scala/kamon/testkit/SpanBuilding.scala +++ b/kamon-core/src/test/scala/kamon/testkit/SpanBuilding.scala @@ -1,20 +1,16 @@ package kamon.testkit -import kamon.trace.SpanContext.{SamplingDecision, Source} -import kamon.trace.{IdentityProvider, SpanContext, SpanContextCodec} +import kamon.trace.SpanContext.SamplingDecision +import kamon.trace.{IdentityProvider, SpanContext} trait SpanBuilding { private val identityProvider = IdentityProvider.Default() - private val extendedB3Codec = SpanContextCodec.ExtendedB3(identityProvider) def createSpanContext(samplingDecision: SamplingDecision = SamplingDecision.Sample): SpanContext = SpanContext( - traceID = identityProvider.traceIdentifierGenerator().generate(), - spanID = identityProvider.spanIdentifierGenerator().generate(), - parentID = identityProvider.spanIdentifierGenerator().generate(), - samplingDecision = samplingDecision, - baggage = SpanContext.Baggage(), - source = Source.Local + traceID = identityProvider.traceIdGenerator().generate(), + spanID = identityProvider.spanIdGenerator().generate(), + parentID = identityProvider.spanIdGenerator().generate(), + samplingDecision = samplingDecision ) - } diff --git a/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala b/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala index ab58e446..f23fba98 100644 --- a/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala +++ b/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala @@ -10,10 +10,10 @@ import scala.util.Try class SpanInspector(span: Span) { private val (realSpan, spanData) = Try { val realSpan = span match { - case _: Span.Real => span + case _: Span.Local => span } - val spanData = invoke[Span.Real, FinishedSpan](realSpan, "toFinishedSpan", classOf[Long] -> Long.box(Clock.microTimestamp())) + val spanData = invoke[Span.Local, FinishedSpan](realSpan, "toFinishedSpan", classOf[Long] -> Long.box(Clock.microTimestamp())) (realSpan, spanData) }.getOrElse((null, null)) @@ -27,10 +27,10 @@ class SpanInspector(span: Span) { spanData.tags def metricTags(): Map[String, String] = - getField[Span.Real, Map[String, String]](realSpan, "customMetricTags") + getField[Span.Local, Map[String, String]](realSpan, "customMetricTags") def startTimestamp(): Long = - getField[Span.Real, Long](realSpan, "startTimestampMicros") + getField[Span.Local, Long](realSpan, "startTimestampMicros") def context(): SpanContext = spanData.context diff --git a/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala b/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala deleted file mode 100644 index a6a7bc3a..00000000 --- a/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala +++ /dev/null @@ -1,65 +0,0 @@ -package kamon.trace - -import kamon.Kamon -import kamon.testkit.SpanInspector -import kamon.trace.Span.Annotation -import kamon.util.Clock -import org.scalatest.{Matchers, WordSpec} - -class ActiveSpanManagementSpec extends WordSpec with Matchers { - - "Kamon acting as a ActiveSpanSource" should { - "return a empty span when there is no currently active Span" in { - inspect(Kamon.activeSpan()) shouldBe empty - } - - "safely operate on a empty Span" in { - val emptySpan = Kamon.activeSpan() - val activeSpanData = inspect(Kamon.activeSpan()) - activeSpanData shouldBe empty - - emptySpan - .setOperationName("test") - .addBaggage("key", "value") - .addMetricTag("key", "value") - .addSpanTag("string", "string") - .addSpanTag("number", 42) - .addSpanTag("boolean-true", true) - .addSpanTag("boolean-false", false) - .annotate(Annotation(Clock.microTimestamp(), "event", Map("k" -> "v"))) - - val baggage = emptySpan.context().baggage - baggage.add("key", "value") - baggage.get("key") shouldBe empty - baggage.getAll() shouldBe empty - - Kamon.withActiveSpan(emptySpan) { - inspect(Kamon.activeSpan()) shouldBe empty - } - - inspect(Kamon.activeSpan()) shouldBe empty - } - - "set a Span as active when using activate" in { - val span = Kamon.buildSpan("mySpan").start() - val scope = Kamon.activate(span) - Kamon.activeSpan() shouldBe theSameInstanceAs(span) - scope.close() - } - - "restore the previously active Span when a scope is closed" in { - val previouslyActiveSpan = Kamon.activeSpan() - inspect(Kamon.activeSpan()) shouldBe empty - - val span = Kamon.buildSpan("mySpan").start() - Kamon.withActiveSpan(span) { - Kamon.activeSpan() shouldBe theSameInstanceAs(span) - } - - Kamon.activeSpan() shouldBe theSameInstanceAs(previouslyActiveSpan) - } - } - - def inspect(span: Span): SpanInspector = - SpanInspector(span) -} diff --git a/kamon-core/src/test/scala/kamon/trace/ExtendedB3SpanContextCodecSpec.scala b/kamon-core/src/test/scala/kamon/trace/B3SpanCodecSpec.scala index 24cb7ef5..e6fa283e 100644 --- a/kamon-core/src/test/scala/kamon/trace/ExtendedB3SpanContextCodecSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/B3SpanCodecSpec.scala @@ -16,50 +16,34 @@ package kamon.trace +import kamon.context.{Context, TextMap} import kamon.testkit.SpanBuilding import kamon.trace.IdentityProvider.Identifier import kamon.trace.SpanContext.SamplingDecision import org.scalatest.{Matchers, OptionValues, WordSpecLike} -class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with OptionValues with SpanBuilding { - val identityProvider = IdentityProvider.Default() - val extendedB3Codec = SpanContextCodec.ExtendedB3(identityProvider) +class B3SpanCodecSpec extends WordSpecLike with Matchers with OptionValues with SpanBuilding { + val extendedB3Codec = SpanCodec.B3() "The ExtendedB3 SpanContextCodec" should { "return a TextMap containing the SpanContext data" in { - val context = testSpanContext() - context.baggage.add("some", "baggage") - context.baggage.add("more", "baggage") + val context = testContext() - val textMap = extendedB3Codec.inject(context) + val textMap = extendedB3Codec.encode(context) textMap.get("X-B3-TraceId").value shouldBe "1234" textMap.get("X-B3-ParentSpanId").value shouldBe "2222" textMap.get("X-B3-SpanId").value shouldBe "4321" textMap.get("X-B3-Sampled").value shouldBe "1" - textMap.get("X-B3-Extra-Baggage").value shouldBe "some=baggage;more=baggage" } - "allow to provide the TextMap to be used for encoding" in { - val context = testSpanContext() - context.baggage.add("some", "baggage") - context.baggage.add("more", "baggage") - val textMap = TextMap.Default() - extendedB3Codec.inject(context, textMap) - textMap.get("X-B3-TraceId").value shouldBe "1234" - textMap.get("X-B3-ParentSpanId").value shouldBe "2222" - textMap.get("X-B3-SpanId").value shouldBe "4321" - textMap.get("X-B3-Sampled").value shouldBe "1" - textMap.get("X-B3-Extra-Baggage").value shouldBe "some=baggage;more=baggage" - } - - "not inject anything if the SpanContext is empty" in { - val textMap = extendedB3Codec.inject(SpanContext.EmptySpanContext) + "not inject anything if there is no Span in the Context" in { + val textMap = extendedB3Codec.encode(Context.Empty) textMap.values shouldBe empty } - "extract a SpanContext from a TextMap when all fields are set" in { + "extract a RemoteSpan from a TextMap when all fields are set" in { val textMap = TextMap.Default() textMap.put("X-B3-TraceId", "1234") textMap.put("X-B3-ParentSpanId", "2222") @@ -67,15 +51,11 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt textMap.put("X-B3-Sampled", "1") textMap.put("X-B3-Extra-Baggage", "some=baggage;more=baggage") - val spanContext = extendedB3Codec.extract(textMap).value + val spanContext = extendedB3Codec.decode(textMap, Context.Empty).get(Span.ContextKey).context() spanContext.traceID.string shouldBe "1234" spanContext.spanID.string shouldBe "4321" spanContext.parentID.string shouldBe "2222" spanContext.samplingDecision shouldBe SamplingDecision.Sample - spanContext.baggage.getAll() should contain allOf( - "some" -> "baggage", - "more" -> "baggage" - ) } "decode the sampling decision based on the X-B3-Sampled header" in { @@ -93,19 +73,27 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt noSamplingTextMap.put("X-B3-TraceId", "1234") noSamplingTextMap.put("X-B3-SpanId", "4321") - extendedB3Codec.extract(sampledTextMap).value.samplingDecision shouldBe SamplingDecision.Sample - extendedB3Codec.extract(notSampledTextMap).value.samplingDecision shouldBe SamplingDecision.DoNotSample - extendedB3Codec.extract(noSamplingTextMap).value.samplingDecision shouldBe SamplingDecision.Unknown + extendedB3Codec.decode(sampledTextMap, Context.Empty) + .get(Span.ContextKey).context().samplingDecision shouldBe SamplingDecision.Sample + + extendedB3Codec.decode(notSampledTextMap, Context.Empty) + .get(Span.ContextKey).context().samplingDecision shouldBe SamplingDecision.DoNotSample + + extendedB3Codec.decode(noSamplingTextMap, Context.Empty) + .get(Span.ContextKey).context().samplingDecision shouldBe SamplingDecision.Unknown } "not include the X-B3-Sampled header if the sampling decision is unknown" in { - val sampledSpanContext = testSpanContext() - val notSampledSpanContext = testSpanContext().copy(samplingDecision = SamplingDecision.DoNotSample) - val unknownSamplingSpanContext = testSpanContext().copy(samplingDecision = SamplingDecision.Unknown) - - extendedB3Codec.inject(sampledSpanContext).get("X-B3-Sampled").value shouldBe("1") - extendedB3Codec.inject(notSampledSpanContext).get("X-B3-Sampled").value shouldBe("0") - extendedB3Codec.inject(unknownSamplingSpanContext).get("X-B3-Sampled") shouldBe empty + val context = testContext() + val sampledSpanContext = context.get(Span.ContextKey).context() + val notSampledSpanContext = Context.Empty.withKey(Span.ContextKey, + Span.Remote(sampledSpanContext.copy(samplingDecision = SamplingDecision.DoNotSample))) + val unknownSamplingSpanContext = Context.Empty.withKey(Span.ContextKey, + Span.Remote(sampledSpanContext.copy(samplingDecision = SamplingDecision.Unknown))) + + extendedB3Codec.encode(context).get("X-B3-Sampled").value shouldBe("1") + extendedB3Codec.encode(notSampledSpanContext).get("X-B3-Sampled").value shouldBe("0") + extendedB3Codec.encode(unknownSamplingSpanContext).get("X-B3-Sampled") shouldBe empty } "use the Debug flag to override the sampling decision, if provided." in { @@ -115,7 +103,7 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt textMap.put("X-B3-Sampled", "0") textMap.put("X-B3-Flags", "1") - val spanContext = extendedB3Codec.extract(textMap).value + val spanContext = extendedB3Codec.decode(textMap, Context.Empty).get(Span.ContextKey).context() spanContext.samplingDecision shouldBe SamplingDecision.Sample } @@ -125,7 +113,7 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt textMap.put("X-B3-SpanId", "4321") textMap.put("X-B3-Flags", "1") - val spanContext = extendedB3Codec.extract(textMap).value + val spanContext = extendedB3Codec.decode(textMap, Context.Empty).get(Span.ContextKey).context() spanContext.samplingDecision shouldBe SamplingDecision.Sample } @@ -134,12 +122,11 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt textMap.put("X-B3-TraceId", "1234") textMap.put("X-B3-SpanId", "4321") - val spanContext = extendedB3Codec.extract(textMap).value + val spanContext = extendedB3Codec.decode(textMap, Context.Empty).get(Span.ContextKey).context() spanContext.traceID.string shouldBe "1234" spanContext.spanID.string shouldBe "4321" spanContext.parentID shouldBe IdentityProvider.NoIdentifier spanContext.samplingDecision shouldBe SamplingDecision.Unknown - spanContext.baggage.getAll() shouldBe empty } "do not extract a SpanContext if Trace ID and Span ID are not provided" in { @@ -157,34 +144,26 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt noIds.put("X-B3-Sampled", "0") noIds.put("X-B3-Flags", "1") - extendedB3Codec.extract(onlyTraceID) shouldBe empty - extendedB3Codec.extract(onlySpanID) shouldBe empty - extendedB3Codec.extract(noIds) shouldBe empty + extendedB3Codec.decode(onlyTraceID, Context.Empty).get(Span.ContextKey) shouldBe Span.Empty + extendedB3Codec.decode(onlySpanID, Context.Empty).get(Span.ContextKey) shouldBe Span.Empty + extendedB3Codec.decode(noIds, Context.Empty).get(Span.ContextKey) shouldBe Span.Empty } - "round trip a SpanContext from TextMap -> SpanContext -> TextMap" in { + "round trip a Span from TextMap -> Context -> TextMap" in { val textMap = TextMap.Default() textMap.put("X-B3-TraceId", "1234") textMap.put("X-B3-ParentSpanId", "2222") textMap.put("X-B3-SpanId", "4321") textMap.put("X-B3-Sampled", "1") - textMap.put("X-B3-Extra-Baggage", "some=baggage;more=baggage") - val spanContext = extendedB3Codec.extract(textMap).value - val injectTextMap = extendedB3Codec.inject(spanContext) + val context = extendedB3Codec.decode(textMap, Context.Empty) + val injectTextMap = extendedB3Codec.encode(context) textMap.values.toSeq should contain theSameElementsAs(injectTextMap.values.toSeq) } - "round trip a baggage that has special characters in there" in { - val spanContext = testSpanContext() - spanContext.baggage.add("key-with-!specials", "value=with~spec;als") - - val textMap = extendedB3Codec.inject(spanContext) - val extractedSpanContext = extendedB3Codec.extract(textMap).value - extractedSpanContext.baggage.getAll().values.toSeq should contain theSameElementsAs(spanContext.baggage.getAll().values.toSeq) - } - + /* + // TODO: Should we be supporting this use case? maybe even have the concept of Debug requests ourselves? "internally carry the X-B3-Flags value so that it can be injected in outgoing requests" in { val textMap = TextMap.Default() textMap.put("X-B3-TraceId", "1234") @@ -192,19 +171,22 @@ class ExtendedB3SpanContextCodecSpec extends WordSpecLike with Matchers with Opt textMap.put("X-B3-SpanId", "4321") textMap.put("X-B3-Sampled", "1") textMap.put("X-B3-Flags", "1") - textMap.put("X-B3-Extra-Baggage", "some=baggage;more=baggage") val spanContext = extendedB3Codec.extract(textMap).value val injectTextMap = extendedB3Codec.inject(spanContext) injectTextMap.get("X-B3-Flags").value shouldBe("1") - } + }*/ } - def testSpanContext(): SpanContext = - createSpanContext().copy( + def testContext(): Context = { + val spanContext = createSpanContext().copy( traceID = Identifier("1234", Array[Byte](1, 2, 3, 4)), spanID = Identifier("4321", Array[Byte](4, 3, 2, 1)), parentID = Identifier("2222", Array[Byte](2, 2, 2, 2)) ) + + Context.create().withKey(Span.ContextKey, Span.Remote(spanContext)) + } + }
\ No newline at end of file diff --git a/kamon-core/src/test/scala/kamon/trace/DefaultIdentityGeneratorSpec.scala b/kamon-core/src/test/scala/kamon/trace/DefaultIdentityGeneratorSpec.scala index 8977e3cd..8f9af7b0 100644 --- a/kamon-core/src/test/scala/kamon/trace/DefaultIdentityGeneratorSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/DefaultIdentityGeneratorSpec.scala @@ -6,8 +6,8 @@ import org.scalactic.TimesOnInt._ class DefaultIdentityGeneratorSpec extends WordSpecLike with Matchers with OptionValues { val idProvider = IdentityProvider.Default() - val traceGenerator = idProvider.traceIdentifierGenerator() - val spanGenerator = idProvider.spanIdentifierGenerator() + val traceGenerator = idProvider.traceIdGenerator() + val spanGenerator = idProvider.spanIdGenerator() validateGenerator("TraceID Generator", traceGenerator) validateGenerator("SpanID Generator", spanGenerator) diff --git a/kamon-core/src/test/scala/kamon/trace/DoubleLengthTraceIdentityGeneratorSpec.scala b/kamon-core/src/test/scala/kamon/trace/DoubleLengthTraceIdentityGeneratorSpec.scala index 54e590ad..b22f17e1 100644 --- a/kamon-core/src/test/scala/kamon/trace/DoubleLengthTraceIdentityGeneratorSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/DoubleLengthTraceIdentityGeneratorSpec.scala @@ -6,8 +6,8 @@ import org.scalatest.{Matchers, OptionValues, WordSpecLike} class DoubleLengthTraceIdentityGeneratorSpec extends WordSpecLike with Matchers with OptionValues { val idProvider = IdentityProvider.DoubleSizeTraceID() - val traceGenerator = idProvider.traceIdentifierGenerator() - val spanGenerator = idProvider.spanIdentifierGenerator() + val traceGenerator = idProvider.traceIdGenerator() + val spanGenerator = idProvider.spanIdGenerator() "The DoubleSizeTraceID identity provider" when { "generating trace identifiers" should { diff --git a/kamon-core/src/test/scala/kamon/trace/RealSpanSpec.scala b/kamon-core/src/test/scala/kamon/trace/LocalSpanSpec.scala index e019e15a..e24f8727 100644 --- a/kamon-core/src/test/scala/kamon/trace/RealSpanSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/LocalSpanSpec.scala @@ -8,7 +8,7 @@ import org.scalatest.concurrent.Eventually import org.scalatest.{BeforeAndAfterAll, Matchers, OptionValues, WordSpec} import org.scalatest.time.SpanSugar._ -class RealSpanSpec extends WordSpec with Matchers with BeforeAndAfterAll with Eventually with OptionValues +class LocalSpanSpec extends WordSpec with Matchers with BeforeAndAfterAll with Eventually with OptionValues with Reconfigure with MetricInspection { "a real span" when { @@ -37,7 +37,6 @@ class RealSpanSpec extends WordSpec with Matchers with BeforeAndAfterAll with Ev .withSpanTag("builder-number-tag", 42) .withStartTimestamp(100) .start() - .addBaggage("baggage", "value") .addSpanTag("span-string-tag", "value") .addSpanTag("span-boolean-tag-true", true) .addSpanTag("span-boolean-tag-false", false) @@ -81,87 +80,8 @@ class RealSpanSpec extends WordSpec with Matchers with BeforeAndAfterAll with Ev val customAnnotationTwo = annotations("custom-annotation-2").head customAnnotationTwo.timestampMicros shouldBe (4201) customAnnotationTwo.fields shouldBe (Map("custom" -> "yes-2")) - - finishedSpan.context.baggage.getAll() should contain( - "baggage" -> "value" - ) - } - } - - "pass all the tags, annotations and baggage to the FinishedSpan instance when started, activated and finished" in { - val scope = Kamon.activate(Kamon.buildSpan("full-span") - .withSpanTag("builder-string-tag", "value") - .withSpanTag("builder-boolean-tag-true", true) - .withSpanTag("builder-boolean-tag-false", false) - .withSpanTag("builder-number-tag", 42) - .withStartTimestamp(100) - .start()) - - Kamon.activeSpan() - .addBaggage("baggage", "value") - .addSpanTag("span-string-tag", "value") - .addSpanTag("span-boolean-tag-true", true) - .addSpanTag("span-boolean-tag-false", false) - .addSpanTag("span-number-tag", 42) - .annotate("simple-annotation") - .annotate("regular-annotation", Map("data" -> "something")) - .annotate(4200, "custom-annotation-1", Map("custom" -> "yes-1")) - .annotate(Annotation(4201, "custom-annotation-2", Map("custom" -> "yes-2"))) - .setOperationName("fully-populated-active-span") - .finish(200) - - scope.close() - - eventually(timeout(2 seconds)) { - val finishedSpan = reporter.nextSpan().value - finishedSpan.operationName shouldBe ("fully-populated-active-span") - finishedSpan.startTimestampMicros shouldBe 100 - finishedSpan.endTimestampMicros shouldBe 200 - finishedSpan.tags should contain allOf( - "builder-string-tag" -> TagValue.String("value"), - "builder-boolean-tag-true" -> TagValue.True, - "builder-boolean-tag-false" -> TagValue.False, - "builder-number-tag" -> TagValue.Number(42), - "span-string-tag" -> TagValue.String("value"), - "span-boolean-tag-true" -> TagValue.True, - "span-boolean-tag-false" -> TagValue.False, - "span-number-tag" -> TagValue.Number(42) - ) - - finishedSpan.annotations.length shouldBe (4) - val annotations = finishedSpan.annotations.groupBy(_.name) - annotations.keys should contain allOf( - "simple-annotation", - "regular-annotation", - "custom-annotation-1", - "custom-annotation-2" - ) - - val customAnnotationOne = annotations("custom-annotation-1").head - customAnnotationOne.timestampMicros shouldBe (4200) - customAnnotationOne.fields shouldBe (Map("custom" -> "yes-1")) - - val customAnnotationTwo = annotations("custom-annotation-2").head - customAnnotationTwo.timestampMicros shouldBe (4201) - customAnnotationTwo.fields shouldBe (Map("custom" -> "yes-2")) - - finishedSpan.context.baggage.getAll() should contain( - "baggage" -> "value" - ) } } - - "allow storing and retrieving baggage items" in { - val span = Kamon.buildSpan("span-with-baggage").start() - span.addBaggage("my-baggage", "value-1") - span.addBaggage("my-baggage", "value-2") - span.addBaggage("my-other-baggage", "value-3") - - span.context().baggage.getAll() should contain only( - "my-baggage" -> "value-2", - "my-other-baggage" -> "value-3" - ) - } } } diff --git a/kamon-core/src/test/scala/kamon/trace/TracerSpec.scala b/kamon-core/src/test/scala/kamon/trace/TracerSpec.scala index 5abfe723..fb5bb313 100644 --- a/kamon-core/src/test/scala/kamon/trace/TracerSpec.scala +++ b/kamon-core/src/test/scala/kamon/trace/TracerSpec.scala @@ -2,10 +2,9 @@ package kamon.trace import com.typesafe.config.ConfigFactory import kamon.Kamon +import kamon.context.Context import kamon.testkit.{SpanBuilding, SpanInspector} import kamon.trace.Span.TagValue -import kamon.trace.SpanContext.Source -import kamon.trace.SpanContextCodec.Format import org.scalatest.{Matchers, OptionValues, WordSpec} class TracerSpec extends WordSpec with Matchers with SpanBuilding with OptionValues { @@ -42,33 +41,16 @@ class TracerSpec extends WordSpec with Matchers with SpanBuilding with OptionVal ("boolean" -> TagValue.True)) } -// "do not interfere with the currently active Span if not requested when starting a Span" in { -// val previouslyActiveSpan = tracer.activeSpan() -// tracer.buildSpan("myOperation").start() -// tracer.activeSpan() should be theSameInstanceAs(previouslyActiveSpan) -// } -// -// "make a span active with started with the .startActive() function and restore the previous Span when deactivated" in { -// val previouslyActiveSpan = tracer.activeSpan() -// val activeSpan = tracer.buildSpan("myOperation").startActive() -// -// tracer.activeSpan() shouldNot be theSameInstanceAs(previouslyActiveSpan) -// val activeSpanData = inspect(activeSpan) -// activeSpanData.operationName() shouldBe "myOperation" -// -// activeSpan.deactivate() -// tracer.activeSpan() should be theSameInstanceAs(previouslyActiveSpan) -// } - "not have any parent Span if there is ActiveSpan and no parent was explicitly given" in { val span = tracer.buildSpan("myOperation").start() val spanData = inspect(span) spanData.context().parentID shouldBe IdentityProvider.NoIdentifier } - "use the currently active span as parent" in { + + "automatically take the Span from the current Context as parent" in { val parent = tracer.buildSpan("myOperation").start() - val child = Kamon.withActiveSpan(parent) { + val child = Kamon.withContext(Context.create(Span.ContextKey, parent)) { tracer.buildSpan("childOperation").asChildOf(parent).start() } @@ -79,7 +61,7 @@ class TracerSpec extends WordSpec with Matchers with SpanBuilding with OptionVal "ignore the currently active span as parent if explicitly requested" in { val parent = tracer.buildSpan("myOperation").start() - val child = Kamon.withActiveSpan(parent) { + val child = Kamon.withContext(Context.create(Span.ContextKey, parent)) { tracer.buildSpan("childOperation").ignoreActiveSpan().start() } @@ -93,53 +75,6 @@ class TracerSpec extends WordSpec with Matchers with SpanBuilding with OptionVal spanData.startTimestamp() shouldBe 100 } - "inject and extract a SpanContext from a TextMap carrier" in { - val spanContext = createSpanContext() - val injected = Kamon.inject(spanContext, Format.TextMap) - val extractedSpanContext = Kamon.extract(Format.TextMap, injected).value - - spanContext.traceID shouldBe(extractedSpanContext.traceID) - spanContext.spanID shouldBe(extractedSpanContext.spanID) - spanContext.parentID shouldBe(extractedSpanContext.parentID) - spanContext.baggage.getAll() shouldBe(extractedSpanContext.baggage.getAll()) - } - - "inject and extract a SpanContext from a TextMap carrier supplied by the caller" in { - val spanContext = createSpanContext() - val carrier = TextMap.Default() - Kamon.inject(spanContext, Format.TextMap, carrier) - val extractedSpanContext = Kamon.extract(Format.TextMap, carrier).value - - spanContext.traceID shouldBe(extractedSpanContext.traceID) - spanContext.spanID shouldBe(extractedSpanContext.spanID) - spanContext.parentID shouldBe(extractedSpanContext.parentID) - spanContext.baggage.getAll() shouldBe(extractedSpanContext.baggage.getAll()) - } - - "inject and extract a SpanContext from a HttpHeaders carrier" in { - val spanContext = createSpanContext() - val injected = Kamon.inject(spanContext, Format.HttpHeaders) - val extractedSpanContext = Kamon.extract(Format.HttpHeaders, injected).value - - spanContext.traceID shouldBe(extractedSpanContext.traceID) - spanContext.spanID shouldBe(extractedSpanContext.spanID) - spanContext.parentID shouldBe(extractedSpanContext.parentID) - spanContext.baggage.getAll() shouldBe(extractedSpanContext.baggage.getAll()) - } - - "inject and extract a SpanContext from a HttpHeaders using a TextMap provided by the caller" in { - val spanContext = createSpanContext() - val carrier = TextMap.Default() - Kamon.inject(spanContext, Format.HttpHeaders, carrier) - val extractedSpanContext = Kamon.extract(Format.HttpHeaders, carrier).value - - spanContext.traceID shouldBe(extractedSpanContext.traceID) - spanContext.spanID shouldBe(extractedSpanContext.spanID) - spanContext.parentID shouldBe(extractedSpanContext.parentID) - spanContext.baggage.getAll() shouldBe(extractedSpanContext.baggage.getAll()) - } - - "preserve the same Span and Parent identifier when creating a Span with a remote parent if join-remote-parents-with-same-span-id is enabled" in { val previousConfig = Kamon.config() @@ -148,12 +83,12 @@ class TracerSpec extends WordSpec with Matchers with SpanBuilding with OptionVal .withFallback(Kamon.config()) } - val remoteParent = createSpanContext().copy(source = Source.Remote) + val remoteParent = Span.Remote(createSpanContext()) val childData = inspect(tracer.buildSpan("local").asChildOf(remoteParent).start()) - childData.context().traceID shouldBe remoteParent.traceID - childData.context().parentID shouldBe remoteParent.parentID - childData.context().spanID shouldBe remoteParent.spanID + childData.context().traceID shouldBe remoteParent.context.traceID + childData.context().parentID shouldBe remoteParent.context.parentID + childData.context().spanID shouldBe remoteParent.context.spanID Kamon.reconfigure(previousConfig) } |