Skip to content

Commit 93e9219

Browse files
committed
Make code a bit nicer
1 parent 660be15 commit 93e9219

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

sentry_sdk/integrations/bottle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sentry_sdk
44
from sentry_sdk.tracing import SOURCE_FOR_STYLE
5+
from sentry_sdk.tracing_utils import finish_running_transaction
56
from sentry_sdk.utils import (
67
capture_internal_exceptions,
78
ensure_integration_enabled,
@@ -139,7 +140,7 @@ def wrapped_callback(*args, **kwargs):
139140
_capture_exception(res, handled=True)
140141

141142
# Manually close the transaction because Bottle does not call `close()` on the WSGI response
142-
sentry_sdk.get_current_scope().transaction.__exit__(None, None, None)
143+
finish_running_transaction()
143144

144145
return res
145146

sentry_sdk/integrations/flask.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
88
from sentry_sdk.scope import should_send_default_pii
99
from sentry_sdk.tracing import SOURCE_FOR_STYLE
10+
from sentry_sdk.tracing_utils import finish_running_transaction
1011
from sentry_sdk.utils import (
1112
capture_internal_exceptions,
1213
ensure_integration_enabled,
@@ -156,7 +157,7 @@ def _request_started(app, **kwargs):
156157

157158
def _request_finished(sender, response, **kwargs):
158159
# Manually close the transaction because Bottle does not call `close()` on the WSGI response
159-
sentry_sdk.get_current_scope().transaction.__exit__(None, None, None)
160+
finish_running_transaction()
160161

161162

162163
class FlaskRequestExtractor(RequestExtractor):

sentry_sdk/integrations/wsgi.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from sentry_sdk.integrations._wsgi_common import (
1010
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1111
_filter_headers,
12-
nullcontext,
1312
)
1413
from sentry_sdk.sessions import track_session
1514
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE
15+
from sentry_sdk.tracing_utils import finish_running_transaction
1616
from sentry_sdk.utils import (
1717
ContextVar,
1818
capture_internal_exceptions,
@@ -262,18 +262,16 @@ def __iter__(self):
262262
try:
263263
chunk = next(iterator)
264264
except StopIteration:
265+
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
266+
# This is done here to make sure the Transaction stays
267+
# open until all streaming responses are done.
268+
finish_running_transaction()
265269
break
266270
except BaseException:
267271
reraise(*_capture_exception())
268272

269273
yield chunk
270274

271-
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
272-
# This is done here to make sure the Transaction stays
273-
# open until all streaming responses are done.
274-
if self._transaction is not None and hasattr(self._transaction, "_context_manager_state"):
275-
self._transaction.__exit__(None, None, None)
276-
277275
def close(self):
278276
# type: () -> None
279277
with use_isolation_scope(self._isolation_scope):
@@ -284,8 +282,7 @@ def close(self):
284282
# Close the Sentry transaction
285283
# This is done here to make sure the Transaction stays
286284
# open until all streaming responses are done.
287-
if self._transaction is not None and hasattr(self._transaction, "_context_manager_state"):
288-
self._transaction.__exit__(None, None, None)
285+
finish_running_transaction()
289286
except AttributeError:
290287
pass
291288
except BaseException:

sentry_sdk/tracing_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,10 @@ def get_current_span(scope=None):
724724

725725
if TYPE_CHECKING:
726726
from sentry_sdk.tracing import Span
727+
728+
729+
def finish_running_transaction():
730+
# type: () -> None
731+
current_scope = sentry_sdk.get_current_scope()
732+
if current_scope._transaction is not None and hasattr(current_scope._transaction, "_context_manager_state"):
733+
current_scope._transaction.__exit__(None, None, None)

0 commit comments

Comments
 (0)