Skip to content

Commit f8e5d2f

Browse files
authored
Add span data to the transactions trace context (#3374)
Fixes #3372
1 parent 0f3e5db commit f8e5d2f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

sentry_sdk/tracing.py

+9
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,15 @@ def to_json(self):
10271027

10281028
return rv
10291029

1030+
def get_trace_context(self):
1031+
# type: () -> Any
1032+
trace_context = super().get_trace_context()
1033+
1034+
if self._data:
1035+
trace_context["data"] = self._data
1036+
1037+
return trace_context
1038+
10301039
def get_baggage(self):
10311040
# type: () -> Baggage
10321041
"""Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage`

tests/tracing/test_misc.py

+27
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ def test_transaction_naming(sentry_init, capture_events):
6060
assert events[2]["transaction"] == "a"
6161

6262

63+
def test_transaction_data(sentry_init, capture_events):
64+
sentry_init(traces_sample_rate=1.0)
65+
events = capture_events()
66+
67+
with start_transaction(name="test-transaction"):
68+
span_or_tx = sentry_sdk.get_current_span()
69+
span_or_tx.set_data("foo", "bar")
70+
with start_span(op="test-span") as span:
71+
span.set_data("spanfoo", "spanbar")
72+
73+
assert len(events) == 1
74+
75+
transaction = events[0]
76+
transaction_data = transaction["contexts"]["trace"]["data"]
77+
78+
assert "data" not in transaction.keys()
79+
assert transaction_data.items() >= {"foo": "bar"}.items()
80+
81+
assert len(transaction["spans"]) == 1
82+
83+
span = transaction["spans"][0]
84+
span_data = span["data"]
85+
86+
assert "contexts" not in span.keys()
87+
assert span_data.items() >= {"spanfoo": "spanbar"}.items()
88+
89+
6390
def test_start_transaction(sentry_init):
6491
sentry_init(traces_sample_rate=1.0)
6592

0 commit comments

Comments
 (0)