11from __future__ import absolute_import
22
3- from sentry_sdk import capture_exception , configure_scope , get_current_hub
3+ from sentry_sdk import capture_exception , configure_scope , get_current_hub , init
44from ._wsgi import RequestExtractor
5+ from . import Integration
56
67try :
78 from flask_login import current_user
1314
1415
1516class FlaskSentry (object ):
16- def __init__ (self , app = None , ** options ):
17+ def __init__ (self , app = None ):
1718 self .app = app
1819 if app is not None :
1920 self .init_app (app , ** options )
@@ -22,18 +23,33 @@ def init_app(self, app, setup_logger=True):
2223 if not hasattr (app , "extensions" ):
2324 app .extensions = {}
2425 elif app .extensions .get (__name__ , None ):
25- raise RuntimeError ("Sentry registration is already registered" )
26+ raise RuntimeError ("Sentry extension is already registered" )
2627 app .extensions [__name__ ] = True
2728
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 )
29+ client_options , integration_options = \
30+ FlaskIntegration .parse_environment (app .config )
31+ integration = FlaskIntegration (app , ** integration_options )
32+ client_options .setdefault ('integrations' , []).append (integration )
33+ init (** client_options )
3234
33- if setup_logger :
34- from .logging import HANDLER
3535
36- app .logger .addHandler (HANDLER )
36+ class FlaskIntegration (Integration ):
37+ identifier = "flask"
38+
39+ def __init__ (self , app , setup_logger = True ):
40+ self ._app = app
41+ self ._setup_logger = setup_logger
42+
43+ def install (self , client = None ):
44+ appcontext_pushed .connect (self ._push_appctx , sender = self ._app )
45+ self ._app .teardown_appcontext (self ._pop_appctx )
46+ got_request_exception .connect (self ._capture_exception ,
47+ sender = self ._app )
48+ self ._app .before_request (self ._before_request )
49+
50+ if self ._setup_logger :
51+ from .logging import HANDLER
52+ self ._app .logger .addHandler (HANDLER )
3753
3854 def _push_appctx (self , * args , ** kwargs ):
3955 get_current_hub ().push_scope ()
0 commit comments