From e7479b9234a9be035add8b5689e6ff79a39c8457 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 30 Jul 2024 12:02:35 +0200 Subject: [PATCH 1/3] Add span data to the transactions trace context --- sentry_sdk/tracing.py | 9 +++++++++ tests/tracing/test_misc.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index dbfa4d896b..b451fcfe0b 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1027,6 +1027,15 @@ def to_json(self): return rv + def get_trace_context(self): + # type: () -> Any + trace_context = super().get_trace_context() + + if self._data: + trace_context["data"] = self._data + + return trace_context + def get_baggage(self): # type: () -> Baggage """Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage` diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index de25acd7d2..4501d59f2b 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -11,6 +11,7 @@ from sentry_sdk.tracing import Span, Transaction from sentry_sdk.tracing_utils import should_propagate_trace from sentry_sdk.utils import Dsn +from tests.conftest import ApproxDict def test_span_trimming(sentry_init, capture_events): @@ -60,6 +61,35 @@ def test_transaction_naming(sentry_init, capture_events): assert events[2]["transaction"] == "a" +def test_transaction_data(sentry_init, capture_events): + sentry_init(traces_sample_rate=1.0) + events = capture_events() + + with start_transaction(name="test-transaction"): + span_or_tx = sentry_sdk.get_current_span() + span_or_tx.set_data("foo", "bar") + with start_span(op="test-span") as span: + span.set_data("spanfoo", "spanbar") + + (transaction,) = events + span = transaction["spans"][0] + + transaction_data = transaction["contexts"]["trace"]["data"] + span_data = span["data"] + + assert transaction_data == ApproxDict( + { + "foo": "bar", + } + ) + + assert span_data == ApproxDict( + { + "spanfoo": "spanbar", + } + ) + + def test_start_transaction(sentry_init): sentry_init(traces_sample_rate=1.0) From 31b46c4d30b54a2f1a6f82ece739e04b84a19f59 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 30 Jul 2024 12:39:17 +0200 Subject: [PATCH 2/3] Nicer test --- tests/tracing/test_misc.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 4501d59f2b..79c5282693 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -71,18 +71,20 @@ def test_transaction_data(sentry_init, capture_events): with start_span(op="test-span") as span: span.set_data("spanfoo", "spanbar") - (transaction,) = events - span = transaction["spans"][0] - + transaction = events[0] transaction_data = transaction["contexts"]["trace"]["data"] - span_data = span["data"] + assert "data" not in transaction.keys() assert transaction_data == ApproxDict( { "foo": "bar", } ) + span = transaction["spans"][0] + span_data = span["data"] + + assert "contexts" not in span.keys() assert span_data == ApproxDict( { "spanfoo": "spanbar", From 6fb1b24a35081c5af710fef53124ec20cc5f55d1 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 30 Jul 2024 16:56:54 +0200 Subject: [PATCH 3/3] Review comments --- tests/tracing/test_misc.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 79c5282693..02966642fd 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -11,7 +11,6 @@ from sentry_sdk.tracing import Span, Transaction from sentry_sdk.tracing_utils import should_propagate_trace from sentry_sdk.utils import Dsn -from tests.conftest import ApproxDict def test_span_trimming(sentry_init, capture_events): @@ -71,25 +70,21 @@ def test_transaction_data(sentry_init, capture_events): with start_span(op="test-span") as span: span.set_data("spanfoo", "spanbar") + assert len(events) == 1 + transaction = events[0] transaction_data = transaction["contexts"]["trace"]["data"] assert "data" not in transaction.keys() - assert transaction_data == ApproxDict( - { - "foo": "bar", - } - ) + assert transaction_data.items() >= {"foo": "bar"}.items() + + assert len(transaction["spans"]) == 1 span = transaction["spans"][0] span_data = span["data"] assert "contexts" not in span.keys() - assert span_data == ApproxDict( - { - "spanfoo": "spanbar", - } - ) + assert span_data.items() >= {"spanfoo": "spanbar"}.items() def test_start_transaction(sentry_init):