Skip to content

Commit 574437b

Browse files
committed
fix: capture request in event processor
1 parent 53c4557 commit 574437b

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
except ImportError:
1111
current_user = None
1212

13-
from flask import current_app, request
13+
from flask import _request_ctx_stack, _app_ctx_stack
1414
from flask.signals import (
1515
appcontext_pushed,
1616
appcontext_tearing_down,
@@ -32,7 +32,7 @@ def install(self, client):
3232

3333
def _push_appctx(*args, **kwargs):
3434
get_current_hub().push_scope()
35-
get_current_hub().add_event_processor(lambda: _event_processor)
35+
get_current_hub().add_event_processor(_make_event_processor)
3636

3737

3838
def _pop_appctx(*args, **kwargs):
@@ -43,24 +43,30 @@ def _capture_exception(sender, exception, **kwargs):
4343
capture_exception(exception)
4444

4545

46-
def _event_processor(event):
47-
if request:
48-
if "transaction" not in event:
46+
def _make_event_processor():
47+
request = getattr(_request_ctx_stack.top, 'request', None)
48+
app = getattr(_app_ctx_stack.top, 'app', None)
49+
50+
def event_processor(event):
51+
if request:
52+
if "transaction" not in event:
53+
with _internal_exceptions():
54+
event["transaction"] = request.url_rule.endpoint
55+
4956
with _internal_exceptions():
50-
event["transaction"] = request.url_rule.endpoint
57+
FlaskRequestExtractor(request).extract_into_event(event)
5158

52-
with _internal_exceptions():
53-
FlaskRequestExtractor(request).extract_into_event(event)
59+
if _should_send_default_pii():
60+
with _internal_exceptions():
61+
_set_user_info(request, event)
5462

55-
if _should_send_default_pii():
5663
with _internal_exceptions():
57-
_set_user_info(event)
64+
_process_frames(app, event)
5865

59-
with _internal_exceptions():
60-
_process_frames(event)
66+
return event_processor
6167

6268

63-
def _process_frames(event):
69+
def _process_frames(app, event):
6470
for frame in event.iter_frames():
6571
if "in_app" in frame:
6672
continue
@@ -70,9 +76,8 @@ def _process_frames(event):
7076

7177
if module == "flask" or module.startswith("flask."):
7278
frame["in_app"] = False
73-
elif current_app and (
74-
module.startswith("%s." % current_app.import_name)
75-
or module == current_app.import_name
79+
elif app and (
80+
module.startswith("%s." % app.import_name) or module == app.import_name
7681
):
7782
frame["in_app"] = True
7883

@@ -100,13 +105,13 @@ def form(self):
100105

101106
@property
102107
def files(self):
103-
return request.files
108+
return self.request.files
104109

105110
def size_of_file(self, file):
106111
return file.content_length
107112

108113

109-
def _set_user_info(event):
114+
def _set_user_info(request, event):
110115
if "user" in event:
111116
return
112117

0 commit comments

Comments
 (0)