Skip to content

Commit

Permalink
Properly parse unsigned integer trace id (#1305)
Browse files Browse the repository at this point in the history
* Fix issue

* import Long

* Add tests

* fix test name

* checking upper bound

* fix tests

---------

Co-authored-by: Vivek Mahajan <[email protected]>
Co-authored-by: hughsimpson <[email protected]>
  • Loading branch information
3 people committed Oct 26, 2023
1 parent 99fb2df commit 8fd9f24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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] {
Expand Down

0 comments on commit 8fd9f24

Please sign in to comment.