@@ -25,46 +25,48 @@ def init_app(self, app):
2525 raise RuntimeError ("Sentry registration is already registered" )
2626 app .extensions [__name__ ] = True
2727
28- appcontext_pushed .connect (_push_appctx , sender = app )
29- app .teardown_appcontext (_pop_appctx )
30- got_request_exception .connect (_capture_exception , sender = app )
31- app .before_request (_before_request )
28+ appcontext_pushed .connect (self . _push_appctx , sender = app )
29+ app .teardown_appcontext (self . _pop_appctx )
30+ got_request_exception .connect (self . _capture_exception , sender = app )
31+ app .before_request (self . _before_request )
3232
33+ def _push_appctx (self , * args , ** kwargs ):
34+ get_current_hub ().push_scope ()
35+ _app_ctx_stack .top ._sentry_app_scope_pushed = True
3336
34- def _push_appctx (* args , ** kwargs ):
35- get_current_hub ().push_scope ()
36- _app_ctx_stack .top ._sentry_app_scope_pushed = True
37+ def _pop_appctx (self , exception ):
38+ get_current_hub ().pop_scope_unsafe ()
3739
40+ def _capture_exception (self , sender , exception , ** kwargs ):
41+ capture_exception (exception )
3842
39- def _pop_appctx (exception ):
40- get_current_hub ().pop_scope_unsafe ()
43+ def _before_request (self , * args , ** kwargs ):
44+ try :
45+ assert getattr (
46+ _app_ctx_stack .top , "_sentry_app_scope_pushed" , None
47+ ), "scope push failed"
4148
49+ with configure_scope () as scope :
50+ if request .url_rule :
51+ scope .transaction = request .url_rule .endpoint
4252
43- def _capture_exception (sender , exception , ** kwargs ):
44- capture_exception (exception )
45-
46-
47- def _before_request (* args , ** kwargs ):
48- try :
49- assert getattr (
50- _app_ctx_stack .top , "_sentry_app_scope_pushed" , None
51- ), "scope push failed"
52-
53- with configure_scope () as scope :
54- if request .url_rule :
55- scope .transaction = request .url_rule .endpoint
53+ get_current_hub ().add_event_processor (self .make_event_processor )
54+ except Exception :
55+ get_current_hub ().capture_internal_exception ()
5656
57+ def make_event_processor (self ):
58+ def processor (event ):
5759 try :
58- FlaskRequestExtractor (request ).extract_into_scope ( scope )
60+ FlaskRequestExtractor (request ).extract_into_event ( event )
5961 except Exception :
6062 get_current_hub ().capture_internal_exception ()
6163
6264 try :
63- _set_user_info (scope )
65+ _set_user_info (event )
6466 except Exception :
6567 get_current_hub ().capture_internal_exception ()
66- except Exception :
67- get_current_hub (). capture_internal_exception ()
68+
69+ return processor
6870
6971
7072class FlaskRequestExtractor (RequestExtractor ):
@@ -96,7 +98,10 @@ def size_of_file(self, file):
9698 return file .content_length
9799
98100
99- def _set_user_info (scope ):
101+ def _set_user_info (event ):
102+ if "user" in event :
103+ return
104+
100105 try :
101106 ip_address = request .access_route [0 ]
102107 except IndexError :
@@ -114,4 +119,4 @@ def _set_user_info(scope):
114119 # - no user is logged in
115120 pass
116121
117- scope . user = user_info
122+ event [ " user" ] = user_info
0 commit comments