Skip to content

Commit 15a44de

Browse files
chore: [SVLS-5265] add span links to encoded spans (#10850)
## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has 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)
1 parent b98138c commit 15a44de

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

ddtrace/internal/encoding.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def _span_to_dict(span):
8383
if span.span_type:
8484
d["type"] = span.span_type
8585

86+
if span._links:
87+
d["span_links"] = [link.to_dict() for link in span._links]
88+
8689
return d
8790

8891

tests/integration/test_encoding.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,22 @@ def test_trace_with_metrics_accepted_by_agent(self, metrics):
6363
tracer.shutdown()
6464
log.warning.assert_not_called()
6565
log.error.assert_not_called()
66+
67+
@pytest.mark.parametrize(
68+
"span_links_kwargs",
69+
[
70+
{"trace_id": 12345, "span_id": 67890},
71+
],
72+
)
73+
def test_trace_with_links_accepted_by_agent(self, span_links_kwargs):
74+
"""Links should not break things."""
75+
tracer = Tracer()
76+
with mock.patch("ddtrace.internal.writer.writer.log") as log:
77+
with tracer.trace("root", service="test_encoding", resource="test_resource") as root:
78+
root.set_link(**span_links_kwargs)
79+
for _ in range(10):
80+
with tracer.trace("child") as child:
81+
child.set_link(**span_links_kwargs)
82+
tracer.shutdown()
83+
log.warning.assert_not_called()
84+
log.error.assert_not_called()

tests/tracer/test_encoders.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_encode_traces_json(self):
162162
# test encoding for JSON format
163163
traces = [
164164
[
165-
Span(name="client.testing"),
165+
Span(name="client.testing", links=[SpanLink(trace_id=12345, span_id=678990)]),
166166
Span(name="client.testing"),
167167
],
168168
[
@@ -184,6 +184,7 @@ def test_encode_traces_json(self):
184184
assert isinstance(spans, str)
185185
assert len(items) == 3
186186
assert len(items[0]) == 2
187+
assert len(items[0][0]["span_links"]) == 1
187188
assert len(items[1]) == 2
188189
assert len(items[2]) == 2
189190
for i in range(3):
@@ -194,7 +195,7 @@ def test_encode_traces_json_v2(self):
194195
# test encoding for JSON format
195196
traces = [
196197
[
197-
Span(name="client.testing", span_id=0xAAAAAA),
198+
Span(name="client.testing", span_id=0xAAAAAA, links=[SpanLink(trace_id=12345, span_id=67890)]),
198199
Span(name="client.testing", span_id=0xAAAAAA),
199200
],
200201
[
@@ -215,6 +216,7 @@ def test_encode_traces_json_v2(self):
215216
assert isinstance(spans, str)
216217
assert len(items) == 3
217218
assert len(items[0]) == 2
219+
assert len(items[0][0]["span_links"]) == 1
218220
assert len(items[1]) == 2
219221
assert len(items[2]) == 2
220222
for i in range(3):

0 commit comments

Comments
 (0)