10
10
except ImportError :
11
11
current_user = None
12
12
13
- from flask import current_app , request
13
+ from flask import _request_ctx_stack , _app_ctx_stack
14
14
from flask .signals import (
15
15
appcontext_pushed ,
16
16
appcontext_tearing_down ,
@@ -32,7 +32,7 @@ def install(self, client):
32
32
33
33
def _push_appctx (* args , ** kwargs ):
34
34
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 )
36
36
37
37
38
38
def _pop_appctx (* args , ** kwargs ):
@@ -43,24 +43,30 @@ def _capture_exception(sender, exception, **kwargs):
43
43
capture_exception (exception )
44
44
45
45
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
+
49
56
with _internal_exceptions ():
50
- event [ "transaction" ] = request . url_rule . endpoint
57
+ FlaskRequestExtractor ( request ). extract_into_event ( event )
51
58
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 )
54
62
55
- if _should_send_default_pii ():
56
63
with _internal_exceptions ():
57
- _set_user_info ( event )
64
+ _process_frames ( app , event )
58
65
59
- with _internal_exceptions ():
60
- _process_frames (event )
66
+ return event_processor
61
67
62
68
63
- def _process_frames (event ):
69
+ def _process_frames (app , event ):
64
70
for frame in event .iter_frames ():
65
71
if "in_app" in frame :
66
72
continue
@@ -70,9 +76,8 @@ def _process_frames(event):
70
76
71
77
if module == "flask" or module .startswith ("flask." ):
72
78
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
76
81
):
77
82
frame ["in_app" ] = True
78
83
@@ -100,13 +105,13 @@ def form(self):
100
105
101
106
@property
102
107
def files (self ):
103
- return request .files
108
+ return self . request .files
104
109
105
110
def size_of_file (self , file ):
106
111
return file .content_length
107
112
108
113
109
- def _set_user_info (event ):
114
+ def _set_user_info (request , event ):
110
115
if "user" in event :
111
116
return
112
117
0 commit comments