Skip to content

Commit 056286b

Browse files
pgjonesantonpirker
andauthored
Update Flask and Quart integrations (#1520)
Flask and Quart are deprecating and removing the ``_xxx_ctx_stack``s and adopting a more direct usage of ContextVars. The previous code will therefore break for the latest version of Quart and start to warn for Flask and then break. This fix should work with any version of Flask or Quart, and hence is a more robust version. There is an extra indirection, however I don't think this is on any hot path. Co-authored-by: Anton Pirker <[email protected]>
1 parent c910d06 commit 056286b

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
try:
2929
from flask import Flask, Markup, Request # type: ignore
3030
from flask import __version__ as FLASK_VERSION
31-
from flask import _app_ctx_stack, _request_ctx_stack
31+
from flask import request as flask_request
3232
from flask.signals import (
3333
before_render_template,
3434
got_request_exception,
@@ -124,19 +124,17 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
124124
pass
125125

126126

127-
def _request_started(sender, **kwargs):
127+
def _request_started(app, **kwargs):
128128
# type: (Flask, **Any) -> None
129129
hub = Hub.current
130130
integration = hub.get_integration(FlaskIntegration)
131131
if integration is None:
132132
return
133133

134-
app = _app_ctx_stack.top.app
135134
with hub.configure_scope() as scope:
136-
request = _request_ctx_stack.top.request
137-
138135
# Set the transaction name and source here,
139136
# but rely on WSGI middleware to actually start the transaction
137+
request = flask_request._get_current_object()
140138
_set_transaction_name_and_source(scope, integration.transaction_style, request)
141139
evt_processor = _make_request_event_processor(app, request, integration)
142140
scope.add_event_processor(evt_processor)

sentry_sdk/integrations/quart.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727

2828
try:
2929
from quart import ( # type: ignore
30+
has_request_context,
31+
has_websocket_context,
3032
Request,
3133
Quart,
32-
_request_ctx_stack,
33-
_websocket_ctx_stack,
34-
_app_ctx_stack,
34+
request,
35+
websocket,
3536
)
3637
from quart.signals import ( # type: ignore
3738
got_background_exception,
@@ -100,19 +101,18 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
100101
pass
101102

102103

103-
def _request_websocket_started(sender, **kwargs):
104+
def _request_websocket_started(app, **kwargs):
104105
# type: (Quart, **Any) -> None
105106
hub = Hub.current
106107
integration = hub.get_integration(QuartIntegration)
107108
if integration is None:
108109
return
109110

110-
app = _app_ctx_stack.top.app
111111
with hub.configure_scope() as scope:
112-
if _request_ctx_stack.top is not None:
113-
request_websocket = _request_ctx_stack.top.request
114-
if _websocket_ctx_stack.top is not None:
115-
request_websocket = _websocket_ctx_stack.top.websocket
112+
if has_request_context():
113+
request_websocket = request._get_current_object()
114+
if has_websocket_context():
115+
request_websocket = websocket._get_current_object()
116116

117117
# Set the transaction name here, but rely on ASGI middleware
118118
# to actually start the transaction

0 commit comments

Comments
 (0)