File tree 2 files changed +35
-5
lines changed
2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -134,14 +134,30 @@ def fingerprint(self, value):
134
134
"""When set this overrides the default fingerprint."""
135
135
self ._fingerprint = value
136
136
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
138
148
def transaction (self , value ):
139
149
# type: (Optional[str]) -> None
140
150
"""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.
141
160
self ._transaction = value
142
- span = self ._span
143
- if span :
144
- span .transaction = value
145
161
146
162
@_attr_setter
147
163
def user (self , value ):
Original file line number Diff line number Diff line change 3
3
4
4
import pytest
5
5
6
- from sentry_sdk import Hub , capture_message
6
+ from sentry_sdk import Hub , capture_message , start_span
7
7
from sentry_sdk .tracing import Span
8
8
9
9
@@ -180,3 +180,17 @@ def before_send(event, hint):
180
180
pass
181
181
182
182
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
You can’t perform that action at this time.
0 commit comments