aboutsummaryrefslogtreecommitdiff
path: root/kamon-core-tests/src/test/scala/kamon/trace/TracerSpec.scala
blob: f325d1d16e3b740f71946f8215aa9011a6f1507d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* =========================================================================================
 * 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.trace

import java.time.Instant

import com.typesafe.config.ConfigFactory
import kamon.Kamon
import kamon.context.Context
import kamon.testkit.{SpanBuilding, SpanInspection}
import kamon.trace.Span.TagValue
import kamon.trace.SpanContext.SamplingDecision
import org.scalatest.{Matchers, OptionValues, WordSpec}

class TracerSpec extends WordSpec with Matchers with SpanBuilding with SpanInspection with OptionValues {

  "the Kamon tracer" should {
    "construct a minimal Span that only has a operation name" in {
      val span = tracer.buildSpan("myOperation").start()
      val spanData = inspect(span)

      spanData.operationName() shouldBe "myOperation"
      spanData.metricTags() shouldBe empty
      spanData.spanTags() shouldBe empty
    }

    "pass the operation name and tags to started Span" in {
      val span = tracer.buildSpan("myOperation")
        .withMetricTag("metric-tag", "value")
        .withMetricTag("metric-tag", "value")
        .withTag("hello", "world")
        .withTag("kamon", "rulez")
        .withTag("number", 123)
        .withTag("boolean", true)
        .start()

      val spanData = inspect(span)
      spanData.operationName() shouldBe "myOperation"
      spanData.metricTags() should contain only (
        ("metric-tag" -> "value"))

      spanData.spanTags() should contain allOf(
        ("hello" -> TagValue.String("world")),
        ("kamon" -> TagValue.String("rulez")),
        ("number" -> TagValue.Number(123)),
        ("boolean" -> TagValue.True))
    }

    "not have any parent Span if there is no Span in the current context and no parent was explicitly given" in {
      val span = tracer.buildSpan("myOperation").start()
      val spanData = inspect(span)
      spanData.context().parentID shouldBe IdentityProvider.NoIdentifier
    }


    "automatically take the Span from the current Context as parent" in {
      val parent = tracer.buildSpan("myOperation").start()
      val child = Kamon.withSpan(parent) {
        tracer.buildSpan("childOperation").asChildOf(parent).start()
      }

      val parentData = inspect(parent)
      val childData = inspect(child)
      parentData.context().spanID shouldBe childData.context().parentID
    }

    "ignore the span from the current context as parent if explicitly requested" in {
      val parent = tracer.buildSpan("myOperation").start()
      val child = Kamon.withSpan(parent) {
        tracer.buildSpan("childOperation").ignoreParentFromContext().start()
      }

      val childData = inspect(child)
      childData.context().parentID shouldBe IdentityProvider.NoIdentifier
    }

    "allow overriding the start timestamp for a Span" in {
      val span = tracer.buildSpan("myOperation").withFrom(Instant.EPOCH.plusMillis(321)).start()
      val spanData = inspect(span)
      spanData.from() shouldBe Instant.EPOCH.plusMillis(321)
    }

    "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()
      Kamon.reconfigure {
        ConfigFactory.parseString("kamon.trace.join-remote-parents-with-same-span-id = yes")
          .withFallback(Kamon.config())
      }

      val remoteParent = Span.Remote(createSpanContext())
      val childData = inspect(tracer.buildSpan("local").asChildOf(remoteParent).start())

      childData.context().traceID shouldBe remoteParent.context.traceID
      childData.context().parentID shouldBe remoteParent.context.parentID
      childData.context().spanID shouldBe remoteParent.context.spanID

      Kamon.reconfigure(previousConfig)
    }

    "propagate sampling decisions from parent to child spans, if the decision is known" in {
      val sampledRemoteParent = Span.Remote(createSpanContext().copy(samplingDecision = SamplingDecision.Sample))
      val notSampledRemoteParent = Span.Remote(createSpanContext().copy(samplingDecision = SamplingDecision.DoNotSample))

      tracer.buildSpan("childOfSampled").asChildOf(sampledRemoteParent).start().context()
        .samplingDecision shouldBe(SamplingDecision.Sample)

      tracer.buildSpan("childOfNotSampled").asChildOf(notSampledRemoteParent).start().context()
        .samplingDecision shouldBe(SamplingDecision.DoNotSample)
    }

    "take a sampling decision if the parent's decision is unknown" in {
      val previousConfig = Kamon.config()
      Kamon.reconfigure {
        ConfigFactory.parseString("kamon.trace.sampler = always")
          .withFallback(Kamon.config())
      }

      val unknownSamplingRemoteParent = Span.Remote(createSpanContext().copy(samplingDecision = SamplingDecision.Unknown))
      tracer.buildSpan("childOfSampled").asChildOf(unknownSamplingRemoteParent).start().context()
        .samplingDecision shouldBe(SamplingDecision.Sample)

      Kamon.reconfigure(previousConfig)
    }

  }

  val tracer: Tracer = Kamon

}