Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixes #842 - No longer URL-encode Uber propagation header, properly handle URL-encoded headers on read #849

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class UberSpanPropagationSpec extends WordSpecLike with Matchers with OptionValu
val headersMap = mutable.Map.empty[String, String]
uberPropagation.write(testContext(), headerWriterFromMap(headersMap))

headersMap.get("uber-trace-id").value shouldBe "1234%3A4321%3A2222%3A1"
headersMap.get("uber-trace-id").value shouldBe "1234:4321:2222:1"
}

"do not include the ParentSpanId if there is no parent" in {
val headersMap = mutable.Map.empty[String, String]
uberPropagation.write(testContextWithoutParent(), headerWriterFromMap(headersMap))

headersMap.get(Uber.HeaderName).value shouldBe "1234%3A4321%3A0%3A1"
headersMap.get(Uber.HeaderName).value shouldBe "1234:4321:0:1"
}


Expand Down Expand Up @@ -89,15 +89,15 @@ class UberSpanPropagationSpec extends WordSpecLike with Matchers with OptionValu
val headersMap = mutable.Map.empty[String, String]

uberPropagation.write(context, headerWriterFromMap(headersMap))
headersMap.get(Uber.HeaderName).value shouldBe "1234%3A4321%3A2222%3A1"
headersMap.get(Uber.HeaderName).value shouldBe "1234:4321:2222:1"
headersMap.clear()

uberPropagation.write(notSampledSpanContext, headerWriterFromMap(headersMap))
headersMap.get(Uber.HeaderName).value shouldBe "1234%3A4321%3A2222%3A0"
headersMap.get(Uber.HeaderName).value shouldBe "1234:4321:2222:0"
headersMap.clear()

uberPropagation.write(unknownSamplingSpanContext,headerWriterFromMap(headersMap))
headersMap.get(Uber.HeaderName).value shouldBe "1234%3A4321%3A2222%3A0"
headersMap.get(Uber.HeaderName).value shouldBe "1234:4321:2222:0"
headersMap.clear()
}

Expand Down Expand Up @@ -143,6 +143,16 @@ class UberSpanPropagationSpec extends WordSpecLike with Matchers with OptionValu
uberPropagation.write(context, headerWriterFromMap(writenHeaders))
writenHeaders.map { case (k, v) => k -> SpanPropagation.Util.urlDecode(v) } should contain theSameElementsAs headers
}

"extract a SpanContext from a URL-encoded header" in {
val headers = Map(Uber.HeaderName -> "1234:5678:4321:1")

val span = uberPropagation.read(headerReaderFromMap(headers), Context.Empty).get(Span.Key)
span.id.string shouldBe "5678"
span.parentId.string shouldBe "4321"
span.trace.id.string shouldBe "1234"
span.trace.samplingDecision shouldBe SamplingDecision.Sample
}
}

def headerReaderFromMap(map: Map[String, String]): HttpPropagation.HeaderReader = new HttpPropagation.HeaderReader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ object SpanPropagation {
val flags = (sampling + (debug << 1)).toHexString
val headerValue = Seq(span.trace.id.string, span.id.string, parentContext, flags).mkString(Separator)

writer.write(HeaderName, urlEncode(headerValue))
writer.write(HeaderName, headerValue)
}

}

override def read(reader: HttpPropagation.HeaderReader, context: Context): Context = {
val identifierScheme = Kamon.identifierScheme
val header = reader.read(HeaderName)
val headerParts = header.toList.flatMap(_.split(':'))
val headerParts = header.map(urlDecode).toList.flatMap(_.split(':'))
val parts = headerParts ++ List.fill(4)("") // all parts are mandatory, but we want to be resilient

val List(traceID, spanID, parentContext, flags) = parts.take(4)
Expand All @@ -257,7 +257,7 @@ object SpanPropagation {

private def stringToId(identifierScheme: Identifier.Scheme, s: String) = {
val str = if (s == null || s.isEmpty) None else Option(s)
val id = str.map(urlDecode).map(identifierScheme.traceIdFactory.from)
val id = str.map(identifierScheme.traceIdFactory.from)
id.getOrElse(Identifier.Empty)
}

Expand Down