aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/legacy-test/scala/kamon/trace
diff options
context:
space:
mode:
Diffstat (limited to 'kamon-core/src/legacy-test/scala/kamon/trace')
-rw-r--r--kamon-core/src/legacy-test/scala/kamon/trace/SamplerSpec.scala76
-rw-r--r--kamon-core/src/legacy-test/scala/kamon/trace/SimpleTraceSpec.scala167
-rw-r--r--kamon-core/src/legacy-test/scala/kamon/trace/TraceContextManipulationSpec.scala113
-rw-r--r--kamon-core/src/legacy-test/scala/kamon/trace/TraceLocalSpec.scala108
-rw-r--r--kamon-core/src/legacy-test/scala/kamon/trace/logging/MdcKeysSupportSpec.scala44
5 files changed, 508 insertions, 0 deletions
diff --git a/kamon-core/src/legacy-test/scala/kamon/trace/SamplerSpec.scala b/kamon-core/src/legacy-test/scala/kamon/trace/SamplerSpec.scala
new file mode 100644
index 00000000..88cdf116
--- /dev/null
+++ b/kamon-core/src/legacy-test/scala/kamon/trace/SamplerSpec.scala
@@ -0,0 +1,76 @@
+package kamon.trace
+
+import kamon.testkit.BaseKamonSpec
+import kamon.util.NanoInterval
+
+class SamplerSpec extends BaseKamonSpec("sampler-spec") {
+
+ "the Sampler" should {
+ "work as intended" when {
+ "using all mode" in {
+ val sampler = SampleAll
+
+ sampler.shouldTrace should be(true)
+
+ sampler.shouldReport(NanoInterval.default) should be(true)
+ }
+
+ "using random mode" in {
+ val sampler = new RandomSampler(100)
+
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+
+ sampler.shouldReport(NanoInterval.default) should be(true)
+ }
+
+ "using ordered mode" in {
+ var sampler = new OrderedSampler(1)
+
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(true)
+
+ sampler = new OrderedSampler(2)
+
+ sampler.shouldTrace should be(false)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(false)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(false)
+ sampler.shouldTrace should be(true)
+
+ sampler.shouldReport(NanoInterval.default) should be(true)
+ }
+
+ "using threshold mode" in {
+ val sampler = new ThresholdSampler(new NanoInterval(10000000L))
+
+ sampler.shouldTrace should be(true)
+
+ sampler.shouldReport(new NanoInterval(5000000L)) should be(false)
+ sampler.shouldReport(new NanoInterval(10000000L)) should be(true)
+ sampler.shouldReport(new NanoInterval(15000000L)) should be(true)
+ sampler.shouldReport(new NanoInterval(0L)) should be(false)
+ }
+
+ "using clock mode" in {
+ val sampler = new ClockSampler(new NanoInterval(10000000L))
+
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(false)
+ Thread.sleep(1L)
+ sampler.shouldTrace should be(false)
+ Thread.sleep(10L)
+ sampler.shouldTrace should be(true)
+ sampler.shouldTrace should be(false)
+
+ sampler.shouldReport(NanoInterval.default) should be(true)
+ }
+ }
+ }
+
+}
diff --git a/kamon-core/src/legacy-test/scala/kamon/trace/SimpleTraceSpec.scala b/kamon-core/src/legacy-test/scala/kamon/trace/SimpleTraceSpec.scala
new file mode 100644
index 00000000..fe7ad053
--- /dev/null
+++ b/kamon-core/src/legacy-test/scala/kamon/trace/SimpleTraceSpec.scala
@@ -0,0 +1,167 @@
+/*
+ * =========================================================================================
+ * Copyright © 2013-2016 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.trace
+
+import kamon.Kamon
+import kamon.testkit.BaseKamonSpec
+
+import scala.concurrent.duration._
+import scala.util.control.NoStackTrace
+
+class SimpleTraceSpec extends BaseKamonSpec("simple-trace-spec") {
+
+ "the simple tracing" should {
+ "send a TraceInfo when the trace has finished and all segments are finished" in {
+ Kamon.tracer.subscribe(testActor)
+
+ Tracer.withContext(newContext("simple-trace-without-segments")) {
+ Tracer.currentContext.startSegment("segment-one", "test-segment", "test").finish()
+ Tracer.currentContext.startSegment("segment-two", "test-segment", "test").finish()
+ Tracer.currentContext.finish()
+ }
+
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-without-segments")
+ traceInfo.segments.size should be(2)
+ traceInfo.segments.find(_.name == "segment-one") should be('defined)
+ traceInfo.segments.find(_.name == "segment-two") should be('defined)
+ }
+
+ "send a TraceInfo when the trace has finished with error and all segments are finished" in {
+ Kamon.tracer.subscribe(testActor)
+
+ Tracer.withContext(newContext("simple-trace-with-error-and-without-segments")) {
+ Tracer.currentContext.startSegment("segment-one", "test-segment", "test").finish()
+ Tracer.currentContext.startSegment("segment-two", "test-segment", "test").finish()
+ Tracer.currentContext.finishWithError(TraceException("awesome-trace-error"))
+ }
+
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-with-error-and-without-segments")
+ traceInfo.status should be(Status.FinishedWithError)
+ traceInfo.segments.size should be(2)
+ traceInfo.segments.find(_.name == "segment-one") should be('defined)
+ traceInfo.segments.find(_.name == "segment-two") should be('defined)
+ }
+
+ "send a TraceInfo when the trace has finished with error and all segments are finished with error" in {
+ Kamon.tracer.subscribe(testActor)
+
+ Tracer.withContext(newContext("simple-trace-with-error-and-without-segments")) {
+ Tracer.currentContext.startSegment("segment-one-finished-with-error", "test-segment", "test").finishWithError(SegmentException("awesome-segment-exception"))
+ Tracer.currentContext.startSegment("segment-two-finished-with-error", "test-segment", "test").finishWithError(SegmentException("awesome-segment-exception"))
+ Tracer.currentContext.finishWithError(TraceException("awesome-trace-error"))
+ }
+
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-with-error-and-without-segments")
+ traceInfo.status should be(Status.FinishedWithError)
+ traceInfo.segments.size should be(2)
+
+ val segmentOne = traceInfo.segments.find(_.name == "segment-one-finished-with-error")
+ val segmentTwo = traceInfo.segments.find(_.name == "segment-two-finished-with-error")
+
+ segmentOne.get.status should be(Status.FinishedWithError)
+ segmentTwo.get.status should be(Status.FinishedWithError)
+ }
+
+ "send a TraceInfo when the trace has finished and all segments are finished and both contains tags" in {
+ Kamon.tracer.subscribe(testActor)
+
+ Tracer.withContext(newContext("simple-trace-without-segments", "awesome-token", Map("environment" → "production"))) {
+ Tracer.currentContext.startSegment("segment-one", "test-segment", "test", Map("segment-one-info" → "info")).finish()
+ Tracer.currentContext.startSegment("segment-two", "test-segment", "test", Map("segment-two-info" → "info")).finish()
+ Tracer.currentContext.finish()
+ }
+
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-without-segments")
+ traceInfo.tags should be(Map("environment" → "production"))
+ traceInfo.segments.size should be(2)
+
+ val segmentOne = traceInfo.segments.find(_.name == "segment-one")
+ val segmentTwo = traceInfo.segments.find(_.name == "segment-two")
+
+ segmentOne.get.tags should be(Map("segment-one-info" → "info"))
+ segmentTwo.get.tags should be(Map("segment-two-info" → "info"))
+ }
+
+ "send a TraceInfo when the trace has finished and all segments are finished and contains added tag" in {
+ Kamon.tracer.subscribe(testActor)
+
+ Tracer.withContext(newContext("simple-trace-without-segments", "awesome-token")) {
+ Tracer.currentContext.addTag("environment", "production")
+ val segmentOne = Tracer.currentContext.startSegment("segment-one", "test-segment", "test")
+ segmentOne.addTag("segment-one-info", "info")
+ segmentOne.finish()
+ val segmentTwo = Tracer.currentContext.startSegment("segment-two", "test-segment", "test")
+ segmentTwo.addTag("segment-two-info", "info")
+ segmentTwo.finish()
+ Tracer.currentContext.finish()
+ }
+
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-without-segments")
+ traceInfo.tags should be(Map("environment" → "production"))
+ traceInfo.segments.size should be(2)
+
+ val segmentOne = traceInfo.segments.find(_.name == "segment-one")
+ val segmentTwo = traceInfo.segments.find(_.name == "segment-two")
+
+ segmentOne.get.tags should be(Map("segment-one-info" → "info"))
+ segmentTwo.get.tags should be(Map("segment-two-info" → "info"))
+ }
+
+ "incubate the tracing context if there are open segments after finishing" in {
+ Kamon.tracer.subscribe(testActor)
+
+ val secondSegment = Tracer.withContext(newContext("simple-trace-without-segments")) {
+ Tracer.currentContext.startSegment("segment-one", "test-segment", "test").finish()
+ val segment = Tracer.currentContext.startSegment("segment-two", "test-segment", "test")
+ Tracer.currentContext.finish()
+ segment
+ }
+
+ expectNoMsg(2 seconds)
+ secondSegment.finish()
+
+ within(10 seconds) {
+ val traceInfo = expectMsgType[TraceInfo]
+ Kamon.tracer.unsubscribe(testActor)
+
+ traceInfo.name should be("simple-trace-without-segments")
+ traceInfo.segments.size should be(2)
+ traceInfo.segments.find(_.name == "segment-one") should be('defined)
+ traceInfo.segments.find(_.name == "segment-two") should be('defined)
+ }
+ }
+
+ }
+}
+
+case class TraceException(message: String) extends RuntimeException(message) with NoStackTrace
+case class SegmentException(message: String) extends RuntimeException(message) with NoStackTrace \ No newline at end of file
diff --git a/kamon-core/src/legacy-test/scala/kamon/trace/TraceContextManipulationSpec.scala b/kamon-core/src/legacy-test/scala/kamon/trace/TraceContextManipulationSpec.scala
new file mode 100644
index 00000000..d817186a
--- /dev/null
+++ b/kamon-core/src/legacy-test/scala/kamon/trace/TraceContextManipulationSpec.scala
@@ -0,0 +1,113 @@
+/*
+ * =========================================================================================
+ * Copyright © 2013-2015 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.trace
+
+import kamon.testkit.BaseKamonSpec
+
+class TraceContextManipulationSpec extends BaseKamonSpec("trace-metrics-spec") {
+
+ "the TraceContext api" should {
+ "allow starting a trace within a specified block of code, and only within that block of code" in {
+ val createdContext = Tracer.withContext(newContext("start-context")) {
+ Tracer.currentContext should not be empty
+ Tracer.currentContext
+ }
+
+ Tracer.currentContext shouldBe empty
+ createdContext.name shouldBe ("start-context")
+ }
+
+ "allow starting a trace within a specified block of code, providing a trace-token and only within that block of code" in {
+ val createdContext = Tracer.withContext(newContext("start-context-with-token", "token-1")) {
+ Tracer.currentContext should not be empty
+ Tracer.currentContext
+ }
+
+ Tracer.currentContext shouldBe empty
+ createdContext.name shouldBe ("start-context-with-token")
+ createdContext.token should be("token-1")
+ }
+
+ "allow providing a TraceContext and make it available within a block of code" in {
+ val createdContext = newContext("manually-provided-trace-context")
+
+ Tracer.currentContext shouldBe empty
+ Tracer.withContext(createdContext) {
+ Tracer.currentContext should be(createdContext)
+ }
+
+ Tracer.currentContext shouldBe empty
+ }
+
+ "allow renaming a trace" in {
+ val createdContext = Tracer.withContext(newContext("trace-before-rename")) {
+ Tracer.currentContext.rename("renamed-trace")
+ Tracer.currentContext
+ }
+
+ Tracer.currentContext shouldBe empty
+ createdContext.name shouldBe "renamed-trace"
+ }
+
+ "allow tagging and untagging a trace" in {
+ val createdContext = Tracer.withContext(newContext("trace-before-rename")) {
+ Tracer.currentContext.addTag("trace-tag", "tag-1")
+ Tracer.currentContext
+ }
+
+ Tracer.currentContext shouldBe empty
+ createdContext.tags shouldBe Map("trace-tag" → "tag-1")
+
+ createdContext.removeTag("trace-tag", "tag-1")
+
+ createdContext.tags shouldBe Map.empty
+ }
+
+ "allow creating a segment within a trace" in {
+ val createdContext = Tracer.withContext(newContext("trace-with-segments")) {
+ Tracer.currentContext.startSegment("segment-1", "segment-1-category", "segment-library")
+ Tracer.currentContext
+ }
+
+ Tracer.currentContext shouldBe empty
+ createdContext.name shouldBe "trace-with-segments"
+ }
+
+ "allow renaming a segment" in {
+ Tracer.withContext(newContext("trace-with-renamed-segment")) {
+ val segment = Tracer.currentContext.startSegment("original-segment-name", "segment-label", "segment-library")
+ segment.name should be("original-segment-name")
+
+ segment.rename("new-segment-name")
+ segment.name should be("new-segment-name")
+ }
+ }
+
+ "allow tagging and untagging a segment" in {
+ Tracer.withContext(newContext("trace-with-renamed-segment")) {
+ val segment = Tracer.currentContext.startSegment("segment-name", "segment-label", "segment-library")
+ segment.tags should be(Map.empty)
+
+ segment.addTag("segment-tag", "tag-1")
+ segment.tags should be(Map("segment-tag" → "tag-1"))
+
+ segment.removeTag("segment-tag", "tag-1")
+ segment.tags should be(Map.empty)
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/kamon-core/src/legacy-test/scala/kamon/trace/TraceLocalSpec.scala b/kamon-core/src/legacy-test/scala/kamon/trace/TraceLocalSpec.scala
new file mode 100644
index 00000000..41d5bc83
--- /dev/null
+++ b/kamon-core/src/legacy-test/scala/kamon/trace/TraceLocalSpec.scala
@@ -0,0 +1,108 @@
+/*
+ * =========================================================================================
+ * Copyright © 2013-2014 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.trace
+
+import kamon.testkit.BaseKamonSpec
+import kamon.trace.TraceLocal.AvailableToMdc
+import kamon.trace.logging.MdcKeysSupport
+import kamon.util.Supplier
+import org.scalatest.concurrent.PatienceConfiguration
+import org.scalatest.OptionValues
+import org.slf4j.MDC
+
+class TraceLocalSpec extends BaseKamonSpec("trace-local-spec") with PatienceConfiguration with OptionValues with MdcKeysSupport {
+ val SampleTraceLocalKeyAvailableToMDC = AvailableToMdc("someKey")
+
+ object SampleTraceLocalKey extends TraceLocal.TraceLocalKey[String]
+
+ "the TraceLocal storage" should {
+ "allow storing and retrieving values" in {
+ Tracer.withContext(newContext("store-and-retrieve-trace-local")) {
+ val testString = "Hello World"
+
+ TraceLocal.store(SampleTraceLocalKey)(testString)
+ TraceLocal.retrieve(SampleTraceLocalKey).value should equal(testString)
+ }
+ }
+
+ "return None when retrieving a non existent key" in {
+ Tracer.withContext(newContext("non-existent-key")) {
+ TraceLocal.retrieve(SampleTraceLocalKey) should equal(None)
+ }
+ }
+
+ "throws an exception when trying to get a non existent key" in {
+ Tracer.withContext(newContext("non-existent-key")) {
+ intercept[NoSuchElementException] {
+ TraceLocal.get(SampleTraceLocalKey)
+ }
+ }
+ }
+
+ "return the given value when retrieving a non existent key" in {
+ Tracer.withContext(newContext("non-existent-key")) {
+ TraceLocal.getOrElse(SampleTraceLocalKey, new Supplier[String] { def get = "optionalValue" }) should equal("optionalValue")
+ }
+ }
+
+ "return None when retrieving a key without a current TraceContext" in {
+ TraceLocal.retrieve(SampleTraceLocalKey) should equal(None)
+ }
+
+ "be attached to the TraceContext when it is propagated" in {
+ val testString = "Hello World"
+ val testContext = Tracer.withContext(newContext("manually-propagated-trace-local")) {
+ TraceLocal.store(SampleTraceLocalKey)(testString)
+ TraceLocal.retrieve(SampleTraceLocalKey).value should equal(testString)
+ Tracer.currentContext
+ }
+
+ /** No TraceLocal should be available here */
+ TraceLocal.retrieve(SampleTraceLocalKey) should equal(None)
+
+ Tracer.withContext(testContext) {
+ TraceLocal.retrieve(SampleTraceLocalKey).value should equal(testString)
+ }
+ }
+
+ "allow retrieve a value from the MDC when was created a key with AvailableToMdc(cool-key)" in {
+ Tracer.withContext(newContext("store-and-retrieve-trace-local-and-copy-to-mdc")) {
+ val testString = "Hello MDC"
+
+ TraceLocal.store(SampleTraceLocalKeyAvailableToMDC)(testString)
+ TraceLocal.retrieve(SampleTraceLocalKeyAvailableToMDC).value should equal(testString)
+
+ withMdc {
+ MDC.get("someKey") should equal(testString)
+ }
+ }
+ }
+
+ "allow retrieve a value from the MDC when was created a key with AvailableToMdc.storeForMdc(String, String)" in {
+ Tracer.withContext(newContext("store-and-retrieve-trace-local-and-copy-to-mdc")) {
+ val testString = "Hello MDC"
+
+ TraceLocal.storeForMdc("someKey", testString)
+ TraceLocal.retrieve(SampleTraceLocalKeyAvailableToMDC).value should equal(testString)
+
+ withMdc {
+ MDC.get("someKey") should equal(testString)
+ }
+ }
+ }
+ }
+}
diff --git a/kamon-core/src/legacy-test/scala/kamon/trace/logging/MdcKeysSupportSpec.scala b/kamon-core/src/legacy-test/scala/kamon/trace/logging/MdcKeysSupportSpec.scala
new file mode 100644
index 00000000..39374e79
--- /dev/null
+++ b/kamon-core/src/legacy-test/scala/kamon/trace/logging/MdcKeysSupportSpec.scala
@@ -0,0 +1,44 @@
+package kamon.trace.logging
+
+import kamon.testkit.BaseKamonSpec
+import kamon.trace.{EmptyTraceContext, Tracer}
+import org.slf4j.MDC
+
+class MdcKeysSupportSpec extends BaseKamonSpec("mdc-keys-support-spec") {
+
+ "Running code with MDC support" should {
+ "add nothing to the MDC" when {
+ "the trace context is empty" in {
+ // Given an empty trace context.
+ Tracer.withContext(EmptyTraceContext) {
+ // When some code is executed with MDC support.
+ MdcKeysSupport.withMdc {
+ // Then the MDC should not contain the trace token.
+ Option(MDC.get(MdcKeysSupport.traceTokenKey)) should be(None)
+ // Or name
+ Option(MDC.get(MdcKeysSupport.traceNameKey)) should be(None)
+ }
+ }
+ }
+ }
+ "add the trace token and name to the context" when {
+ "the trace context is not empty" in {
+ // Given a trace context.
+ Tracer.withNewContext("name", Some("token")) {
+ // When some code is executed with MDC support.
+ MdcKeysSupport.withMdc {
+ // Then the MDC should contain the trace token.
+ Option(MDC.get(MdcKeysSupport.traceTokenKey)) should be(Some("token"))
+ // And name
+ Option(MDC.get(MdcKeysSupport.traceNameKey)) should be(Some("name"))
+ }
+
+ // Then after code is executed the MDC should have been cleared.
+ Option(MDC.get(MdcKeysSupport.traceTokenKey)) should be(None)
+ Option(MDC.get(MdcKeysSupport.traceNameKey)) should be(None)
+ }
+ }
+ }
+ }
+
+}