From b97db723aaad1d9f2b9cf0cb03fff5e33b7d58e5 Mon Sep 17 00:00:00 2001 From: Vivek Mahajan Date: Thu, 26 Oct 2023 11:08:45 +0200 Subject: [PATCH] Properly parse unsigned integer trace id (#1305) * Fix issue * import Long * Add tests * fix test name * checking upper bound * fix tests --------- Co-authored-by: Vivek Mahajan Co-authored-by: hughsimpson --- .../trace/DataDogSpanPropagationSpec.scala | 22 +++++++++++++++++++ .../scala/kamon/trace/SpanPropagation.scala | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/kamon-core-tests/src/test/scala/kamon/trace/DataDogSpanPropagationSpec.scala b/core/kamon-core-tests/src/test/scala/kamon/trace/DataDogSpanPropagationSpec.scala index 8a22c1fd5..3cfb1682f 100644 --- a/core/kamon-core-tests/src/test/scala/kamon/trace/DataDogSpanPropagationSpec.scala +++ b/core/kamon-core-tests/src/test/scala/kamon/trace/DataDogSpanPropagationSpec.scala @@ -118,6 +118,28 @@ class DataDogSpanPropagationSpec extends AnyWordSpec with Matchers with OptionVa } } + + "SpanPropagation.DataDog.decodeUnsignedLongToHex" should { + "decode unsigned long to expected hex value " in { + val expectedHex1 = "0"; + val actualHex1 = SpanPropagation.DataDog.decodeUnsignedLongToHex("0"); + expectedHex1 shouldBe actualHex1; + + val expectedHex2 = "ff"; + val actualHex2 = SpanPropagation.DataDog.decodeUnsignedLongToHex("255"); + expectedHex2 shouldBe actualHex2; + + val expectedHex3 = "c5863f7d672b65bf"; + val actualHex3 = SpanPropagation.DataDog.decodeUnsignedLongToHex("14233133480185390527"); + expectedHex3 shouldBe actualHex3; + + val expectedHex4 = "ffffffffffffffff"; + val actualHex4 = SpanPropagation.DataDog.decodeUnsignedLongToHex("18446744073709551615"); + expectedHex4 shouldBe actualHex4; + + } + } + def unsignedLongString(id: String): String = BigInt(id, 16).toString def headerReaderFromMap(map: Map[String, String]): HttpPropagation.HeaderReader = new HttpPropagation.HeaderReader { diff --git a/core/kamon-core/src/main/scala/kamon/trace/SpanPropagation.scala b/core/kamon-core/src/main/scala/kamon/trace/SpanPropagation.scala index 0133bc199..77034f739 100644 --- a/core/kamon-core/src/main/scala/kamon/trace/SpanPropagation.scala +++ b/core/kamon-core/src/main/scala/kamon/trace/SpanPropagation.scala @@ -24,6 +24,7 @@ import kamon.context.HttpPropagation.{HeaderReader, HeaderWriter} import kamon.context.generated.binary.span.{Span => ColferSpan} import kamon.context.{Context, _} import kamon.trace.Trace.SamplingDecision +import java.lang.{Long => JLong} import scala.util.Try @@ -450,7 +451,7 @@ object W3CTraceContext { * https://docs.datadoghq.com/tracing/guide/send_traces_to_agent_by_api/ */ def decodeUnsignedLongToHex(id: String): String = - urlDecode(id).toLong.toHexString + JLong.parseUnsignedLong(urlDecode(id), 10).toHexString } class DataDog extends Propagation.EntryReader[HeaderReader] with Propagation.EntryWriter[HeaderWriter] {