25
25
class FlaskIntegration (Integration ):
26
26
identifier = "flask"
27
27
28
+ transaction_style = None
29
+
30
+ def __init__ (self , transaction_style = "endpoint" ):
31
+ TRANSACTION_STYLE_VALUES = ("endpoint" , "url" )
32
+ if transaction_style not in TRANSACTION_STYLE_VALUES :
33
+ raise ValueError (
34
+ "Invalid value for transaction_style: %s (must be in %s)"
35
+ % (transaction_style , TRANSACTION_STYLE_VALUES )
36
+ )
37
+ self .transaction_style = transaction_style
38
+
28
39
def install (self ):
29
- appcontext_pushed .connect (_push_appctx )
30
- appcontext_tearing_down .connect (_pop_appctx )
31
- request_started .connect (_request_started )
40
+ appcontext_pushed .connect (self . _push_appctx )
41
+ appcontext_tearing_down .connect (self . _pop_appctx )
42
+ request_started .connect (self . _request_started )
32
43
got_request_exception .connect (_capture_exception )
33
44
34
45
old_app = Flask .__call__
@@ -40,23 +51,52 @@ def sentry_patched_wsgi_app(self, environ, start_response):
40
51
41
52
Flask .__call__ = sentry_patched_wsgi_app
42
53
54
+ def _push_appctx (self , * args , ** kwargs ):
55
+ # always want to push scope regardless of whether WSGI app might already
56
+ # have (not the case for CLI for example)
57
+ hub = Hub .current
58
+ hub .push_scope ()
59
+
60
+ def _pop_appctx (self , * args , ** kwargs ):
61
+ Hub .current .pop_scope_unsafe ()
62
+
63
+ def _request_started (self , sender , ** kwargs ):
64
+ weak_request = weakref .ref (_request_ctx_stack .top .request )
65
+ app = _app_ctx_stack .top .app
66
+ with configure_scope () as scope :
67
+ scope .add_event_processor (
68
+ self ._make_request_event_processor (app , weak_request )
69
+ )
43
70
44
- def _push_appctx (* args , ** kwargs ):
45
- # always want to push scope regardless of whether WSGI app might already
46
- # have (not the case for CLI for example)
47
- hub = Hub .current
48
- hub .push_scope ()
71
+ def _make_request_event_processor (self , app , weak_request ):
72
+ def inner (event , hint ):
73
+ request = weak_request ()
74
+
75
+ # if the request is gone we are fine not logging the data from
76
+ # it. This might happen if the processor is pushed away to
77
+ # another thread.
78
+ if request is None :
79
+ return event
80
+
81
+ if "transaction" not in event :
82
+ try :
83
+ if self .transaction_style == "endpoint" :
84
+ event ["transaction" ] = request .url_rule .endpoint
85
+ elif self .transaction_style == "url" :
86
+ event ["transaction" ] = request .url_rule .rule
87
+ except Exception :
88
+ pass
49
89
90
+ with capture_internal_exceptions ():
91
+ FlaskRequestExtractor (request ).extract_into_event (event )
50
92
51
- def _pop_appctx (* args , ** kwargs ):
52
- Hub .current .pop_scope_unsafe ()
93
+ if _should_send_default_pii ():
94
+ with capture_internal_exceptions ():
95
+ _add_user_to_event (event )
53
96
97
+ return event
54
98
55
- def _request_started (sender , ** kwargs ):
56
- weak_request = weakref .ref (_request_ctx_stack .top .request )
57
- app = _app_ctx_stack .top .app
58
- with configure_scope () as scope :
59
- scope .add_event_processor (_make_request_event_processor (app , weak_request ))
99
+ return inner
60
100
61
101
62
102
class FlaskRequestExtractor (RequestExtractor ):
@@ -93,34 +133,6 @@ def _capture_exception(sender, exception, **kwargs):
93
133
hub .capture_event (event , hint = hint )
94
134
95
135
96
- def _make_request_event_processor (app , weak_request ):
97
- def inner (event , hint ):
98
- request = weak_request ()
99
-
100
- # if the request is gone we are fine not logging the data from
101
- # it. This might happen if the processor is pushed away to
102
- # another thread.
103
- if request is None :
104
- return event
105
-
106
- if "transaction" not in event :
107
- try :
108
- event ["transaction" ] = request .url_rule .endpoint
109
- except Exception :
110
- pass
111
-
112
- with capture_internal_exceptions ():
113
- FlaskRequestExtractor (request ).extract_into_event (event )
114
-
115
- if _should_send_default_pii ():
116
- with capture_internal_exceptions ():
117
- _add_user_to_event (event )
118
-
119
- return event
120
-
121
- return inner
122
-
123
-
124
136
def _add_user_to_event (event ):
125
137
if flask_login is None :
126
138
return
0 commit comments