Skip to content

Commit 5fb7826

Browse files
authored
fix(otel): ensures ids in span links are in hex (#8054)
Resolves: #8049 ## Reproduction ``` import ddtrace with ddtrace.tracer.trace("s1", service="upstream") as s1: pass with ddtrace.tracer.trace("s2", service="downstream") as s2: s2.link_span( s1.context, { "link.name": "s1_to_s2", "link.kind": "scheduled_by", "key1": "value2", "key2": [True, 2, ["hello", 4, ["5", "6asda"]]], } ) ``` ## Datadog UI with fix <img width="1050" alt="Screenshot 2024-01-09 at 5 43 13 PM" src="https://github.com/DataDog/dd-trace-py/assets/62392438/5380023e-1ec1-4da1-a342-6a051e92a821"> ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Title is accurate. - [ ] No unnecessary changes are introduced. - [ ] Description motivates each change. - [ ] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Testing strategy adequately addresses listed risk(s). - [ ] Change is maintainable (easy to change, telemetry, documentation). - [ ] Release note makes sense to a user of the library. - [ ] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [ ] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [ ] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [ ] This PR doesn't touch any of that.
1 parent 7683e60 commit 5fb7826

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

ddtrace/internal/_encoding.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ cdef class MsgpackEncoderV03(MsgpackEncoderBase):
571571
# SpanLink.to_dict() returns all serializable span link fields
572572
d = link.to_dict()
573573
# Encode 128 bit trace ids usings two 64bit integers
574-
d["trace_id_high"] = d["trace_id"] >> 64
575-
d["trace_id"] = MAX_UINT_64BITS & d["trace_id"]
574+
d["trace_id_high"] = d["trace_id"][:16]
575+
d["trace_id"] = d["trace_id"][16:]
576576

577577
ret = msgpack_pack_map(&self.pk, len(d))
578578
if ret != 0:

ddtrace/tracing/_span_link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def _drop_attribute(self, key):
8080

8181
def to_dict(self):
8282
d = {
83-
"trace_id": self.trace_id,
84-
"span_id": self.span_id,
83+
"trace_id": "{:032x}".format(self.trace_id),
84+
"span_id": "{:016x}".format(self.span_id),
8585
}
8686
if self.attributes:
8787
d["attributes"] = {k: str(v) for k, v in self.attributes.items()}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: Fixes a bug that prevents span links from being visualized in the Datadog UI.

tests/tracer/test_encoders.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ def test_span_link_v04_encoding():
463463
assert b"span_links" in decoded_span
464464
assert decoded_span[b"span_links"] == [
465465
{
466-
b"trace_id": 456,
467-
b"span_id": 2,
466+
b"trace_id": b"00000000000001c8",
467+
b"span_id": b"0000000000000002",
468468
b"attributes": {
469469
b"moon": b"ears",
470470
b"link.name": b"link_name",
@@ -474,7 +474,7 @@ def test_span_link_v04_encoding():
474474
b"dropped_attributes_count": 1,
475475
b"tracestate": b"congo=t61rcWkgMzE",
476476
b"flags": 1,
477-
b"trace_id_high": 123,
477+
b"trace_id_high": b"000000000000007b",
478478
}
479479
]
480480

@@ -487,8 +487,8 @@ def test_span_link_v05_encoding():
487487
context=Context(sampling_priority=1),
488488
links=[
489489
SpanLink(
490-
trace_id=1,
491-
span_id=2,
490+
trace_id=(2**127) - 1,
491+
span_id=(2**64) - 1,
492492
tracestate="congo=t61rcWkgMzE",
493493
flags=0,
494494
attributes={"moon": "ears", "link.name": "link_name", "link.kind": "link_kind", "drop_me": "bye"},
@@ -511,9 +511,9 @@ def test_span_link_v05_encoding():
511511
encoded_span_meta = decoded_trace[0][0][9]
512512
assert b"_dd.span_links" in encoded_span_meta
513513
assert (
514-
encoded_span_meta[b"_dd.span_links"] == b'[{"trace_id": 1, "span_id": 2, '
515-
b'"attributes": {"moon": "ears", "link.name": "link_name", "link.kind": "link_kind"}, '
516-
b'"dropped_attributes_count": 1, "tracestate": "congo=t61rcWkgMzE", "flags": 0}]'
514+
encoded_span_meta[b"_dd.span_links"] == b'[{"trace_id": "7fffffffffffffffffffffffffffffff", '
515+
b'"span_id": "ffffffffffffffff", "attributes": {"moon": "ears", "link.name": "link_name", "link.kind": '
516+
b'"link_kind"}, "dropped_attributes_count": 1, "tracestate": "congo=t61rcWkgMzE", "flags": 0}]'
517517
)
518518

519519

0 commit comments

Comments
 (0)