diff --git a/ddtrace/internal/encoding.py b/ddtrace/internal/encoding.py index 13084cea1fc..0cf31f9f7ed 100644 --- a/ddtrace/internal/encoding.py +++ b/ddtrace/internal/encoding.py @@ -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 diff --git a/tests/integration/test_encoding.py b/tests/integration/test_encoding.py index f5efc03d163..43c47ac4840 100644 --- a/tests/integration/test_encoding.py +++ b/tests/integration/test_encoding.py @@ -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() diff --git a/tests/tracer/test_encoders.py b/tests/tracer/test_encoders.py index cd14c439e89..213027a7103 100644 --- a/tests/tracer/test_encoders.py +++ b/tests/tracer/test_encoders.py @@ -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"), ], [ @@ -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): @@ -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), ], [ @@ -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):