Skip to content

Commit

Permalink
chore: [SVLS-5265] add span links to encoded spans (#10850)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
apiarian-datadog authored Oct 10, 2024
1 parent b98138c commit 15a44de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ddtrace/internal/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _span_to_dict(span):
if span.span_type:
d["type"] = span.span_type

if span._links:
d["span_links"] = [link.to_dict() for link in span._links]

return d


Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,22 @@ def test_trace_with_metrics_accepted_by_agent(self, metrics):
tracer.shutdown()
log.warning.assert_not_called()
log.error.assert_not_called()

@pytest.mark.parametrize(
"span_links_kwargs",
[
{"trace_id": 12345, "span_id": 67890},
],
)
def test_trace_with_links_accepted_by_agent(self, span_links_kwargs):
"""Links should not break things."""
tracer = Tracer()
with mock.patch("ddtrace.internal.writer.writer.log") as log:
with tracer.trace("root", service="test_encoding", resource="test_resource") as root:
root.set_link(**span_links_kwargs)
for _ in range(10):
with tracer.trace("child") as child:
child.set_link(**span_links_kwargs)
tracer.shutdown()
log.warning.assert_not_called()
log.error.assert_not_called()
6 changes: 4 additions & 2 deletions tests/tracer/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_encode_traces_json(self):
# test encoding for JSON format
traces = [
[
Span(name="client.testing"),
Span(name="client.testing", links=[SpanLink(trace_id=12345, span_id=678990)]),
Span(name="client.testing"),
],
[
Expand All @@ -184,6 +184,7 @@ def test_encode_traces_json(self):
assert isinstance(spans, str)
assert len(items) == 3
assert len(items[0]) == 2
assert len(items[0][0]["span_links"]) == 1
assert len(items[1]) == 2
assert len(items[2]) == 2
for i in range(3):
Expand All @@ -194,7 +195,7 @@ def test_encode_traces_json_v2(self):
# test encoding for JSON format
traces = [
[
Span(name="client.testing", span_id=0xAAAAAA),
Span(name="client.testing", span_id=0xAAAAAA, links=[SpanLink(trace_id=12345, span_id=67890)]),
Span(name="client.testing", span_id=0xAAAAAA),
],
[
Expand All @@ -215,6 +216,7 @@ def test_encode_traces_json_v2(self):
assert isinstance(spans, str)
assert len(items) == 3
assert len(items[0]) == 2
assert len(items[0][0]["span_links"]) == 1
assert len(items[1]) == 2
assert len(items[2]) == 2
for i in range(3):
Expand Down

0 comments on commit 15a44de

Please sign in to comment.