Skip to content

Commit 8839f99

Browse files
committed
feat: Access transaction in current scope
Specially when trying to add spans to automatically instrumented transactions, users need access to the current transaction. This gives direct access no matter how deep the code is in the transaction/span tree.
1 parent b539ecb commit 8839f99

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

sentry_sdk/scope.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,30 @@ def fingerprint(self, value):
134134
"""When set this overrides the default fingerprint."""
135135
self._fingerprint = value
136136

137-
@_attr_setter
137+
@property
138+
def transaction(self):
139+
# type: () -> Optional[Span]
140+
"""Return the transaction (root span) in the scope."""
141+
# XXX: update return type to Optional[Transaction]
142+
try:
143+
return self._span._span_recorder.spans[0]
144+
except (AttributeError, IndexError):
145+
return None
146+
147+
@transaction.setter
138148
def transaction(self, value):
139149
# type: (Optional[str]) -> None
140150
"""When set this forces a specific transaction name to be set."""
151+
# XXX: the docstring above is misleading. The implementation of
152+
# apply_to_event prefers an existing value of event.transaction over
153+
# anything set in the scope.
154+
# XXX: note that with the introduction of the Scope.transaction getter,
155+
# there is a semantic and type mismatch between getter and setter. The
156+
# getter returns a transaction, the setter sets a transaction name.
157+
# Without breaking version compatibility, we could make the setter set a
158+
# transaction name or transaction (self._span) depending on the type of
159+
# the value argument.
141160
self._transaction = value
142-
span = self._span
143-
if span:
144-
span.transaction = value
145161

146162
@_attr_setter
147163
def user(self, value):

tests/test_tracing.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from sentry_sdk import Hub, capture_message
6+
from sentry_sdk import Hub, capture_message, start_span
77
from sentry_sdk.tracing import Span
88

99

@@ -180,3 +180,17 @@ def before_send(event, hint):
180180
pass
181181

182182
assert len(events) == 1
183+
184+
185+
def test_get_transaction_from_scope(sentry_init, capture_events):
186+
sentry_init(traces_sample_rate=1.0)
187+
events = capture_events()
188+
189+
with start_span(transaction="/"):
190+
with start_span(op="child-span"):
191+
with start_span(op="child-child-span"):
192+
scope = Hub.current.scope
193+
assert scope.span.op == "child-child-span"
194+
assert scope.transaction.transaction == "/"
195+
196+
assert len(events) == 1

0 commit comments

Comments
 (0)