aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2018-04-05 21:20:34 +0200
committerIvan Topolnjak <ivantopo@gmail.com>2018-04-05 21:33:15 +0200
commit2f05b3b4823f98a94de5c4b49fb5efeb0f2b0e2d (patch)
treeb09a7ae7cb2159c285fbd0a89cb9096a7f8e03fc
parente711bb0170f0939f9054cea76da0ce09005ccef3 (diff)
downloadKamon-2f05b3b4823f98a94de5c4b49fb5efeb0f2b0e2d.tar.gz
Kamon-2f05b3b4823f98a94de5c4b49fb5efeb0f2b0e2d.tar.bz2
Kamon-2f05b3b4823f98a94de5c4b49fb5efeb0f2b0e2d.zip
do not encode ParentID on B3 headers if not present, fixes kamon-io/kamon-akka-http#35
-rw-r--r--kamon-core-tests/src/test/scala/kamon/trace/B3SpanCodecSpec.scala22
-rw-r--r--kamon-core/src/main/scala/kamon/trace/SpanCodec.scala4
2 files changed, 22 insertions, 4 deletions
diff --git a/kamon-core-tests/src/test/scala/kamon/trace/B3SpanCodecSpec.scala b/kamon-core-tests/src/test/scala/kamon/trace/B3SpanCodecSpec.scala
index e6fa283e..f718d806 100644
--- a/kamon-core-tests/src/test/scala/kamon/trace/B3SpanCodecSpec.scala
+++ b/kamon-core-tests/src/test/scala/kamon/trace/B3SpanCodecSpec.scala
@@ -28,15 +28,21 @@ class B3SpanCodecSpec extends WordSpecLike with Matchers with OptionValues with
"The ExtendedB3 SpanContextCodec" should {
"return a TextMap containing the SpanContext data" in {
- val context = testContext()
-
- val textMap = extendedB3Codec.encode(context)
+ val textMap = extendedB3Codec.encode(testContext())
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"
}
+ "do not include the X-B3-ParentSpanId if there is no parent" in {
+ val textMap = extendedB3Codec.encode(testContextWithoutParent())
+ textMap.get("X-B3-TraceId").value shouldBe "1234"
+ textMap.get("X-B3-ParentSpanId") shouldBe empty
+ textMap.get("X-B3-SpanId").value shouldBe "4321"
+ textMap.get("X-B3-Sampled").value shouldBe "1"
+ }
+
"not inject anything if there is no Span in the Context" in {
val textMap = extendedB3Codec.encode(Context.Empty)
@@ -189,4 +195,14 @@ class B3SpanCodecSpec extends WordSpecLike with Matchers with OptionValues with
Context.create().withKey(Span.ContextKey, Span.Remote(spanContext))
}
+ def testContextWithoutParent(): Context = {
+ val spanContext = createSpanContext().copy(
+ traceID = Identifier("1234", Array[Byte](1, 2, 3, 4)),
+ spanID = Identifier("4321", Array[Byte](4, 3, 2, 1)),
+ parentID = IdentityProvider.NoIdentifier
+ )
+
+ Context.create().withKey(Span.ContextKey, Span.Remote(spanContext))
+ }
+
} \ No newline at end of file
diff --git a/kamon-core/src/main/scala/kamon/trace/SpanCodec.scala b/kamon-core/src/main/scala/kamon/trace/SpanCodec.scala
index ae78ee67..093257c0 100644
--- a/kamon-core/src/main/scala/kamon/trace/SpanCodec.scala
+++ b/kamon-core/src/main/scala/kamon/trace/SpanCodec.scala
@@ -37,7 +37,9 @@ object SpanCodec {
val spanContext = span.context()
carrier.put(Headers.TraceIdentifier, urlEncode(spanContext.traceID.string))
carrier.put(Headers.SpanIdentifier, urlEncode(spanContext.spanID.string))
- carrier.put(Headers.ParentSpanIdentifier, urlEncode(spanContext.parentID.string))
+
+ if(spanContext.parentID != IdentityProvider.NoIdentifier)
+ carrier.put(Headers.ParentSpanIdentifier, urlEncode(spanContext.parentID.string))
encodeSamplingDecision(spanContext.samplingDecision).foreach { samplingDecision =>
carrier.put(Headers.Sampled, samplingDecision)