1
- from sentry_sdk . hub import Hub
1
+ import sentry_sdk
2
2
from sentry_sdk .tracing import SOURCE_FOR_STYLE
3
3
from sentry_sdk .utils import (
4
4
capture_internal_exceptions ,
5
+ ensure_integration_enabled ,
5
6
event_from_exception ,
6
7
parse_version ,
7
8
transaction_from_function ,
8
9
)
9
10
from sentry_sdk .integrations import Integration , DidNotEnable
10
11
from sentry_sdk .integrations .wsgi import SentryWsgiMiddleware
11
12
from sentry_sdk .integrations ._wsgi_common import RequestExtractor
12
-
13
+ from sentry_sdk . scope import Scope
13
14
from sentry_sdk ._types import TYPE_CHECKING
14
15
15
16
if TYPE_CHECKING :
@@ -55,7 +56,6 @@ def __init__(self, transaction_style="endpoint"):
55
56
@staticmethod
56
57
def setup_once ():
57
58
# type: () -> None
58
-
59
59
version = parse_version (BOTTLE_VERSION )
60
60
61
61
if version is None :
@@ -64,64 +64,46 @@ def setup_once():
64
64
if version < (0 , 12 ):
65
65
raise DidNotEnable ("Bottle 0.12 or newer required." )
66
66
67
- # monkey patch method Bottle.__call__
68
67
old_app = Bottle .__call__
69
68
69
+ @ensure_integration_enabled (BottleIntegration , old_app )
70
70
def sentry_patched_wsgi_app (self , environ , start_response ):
71
71
# type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
72
-
73
- hub = Hub .current
74
- integration = hub .get_integration (BottleIntegration )
75
- if integration is None :
76
- return old_app (self , environ , start_response )
77
-
78
72
return SentryWsgiMiddleware (lambda * a , ** kw : old_app (self , * a , ** kw ))(
79
73
environ , start_response
80
74
)
81
75
82
76
Bottle .__call__ = sentry_patched_wsgi_app
83
77
84
- # monkey patch method Bottle._handle
85
78
old_handle = Bottle ._handle
86
79
87
80
def _patched_handle (self , environ ):
88
81
# type: (Bottle, Dict[str, Any]) -> Any
89
- hub = Hub .current
90
- integration = hub .get_integration (BottleIntegration )
82
+ integration = sentry_sdk .get_client ().get_integration (BottleIntegration )
91
83
if integration is None :
92
84
return old_handle (self , environ )
93
85
94
- # create new scope
95
- scope_manager = hub .push_scope ()
96
-
97
- with scope_manager :
98
- app = self
99
- with hub .configure_scope () as scope :
100
- scope ._name = "bottle"
101
- scope .add_event_processor (
102
- _make_request_event_processor (app , bottle_request , integration )
103
- )
104
- res = old_handle (self , environ )
86
+ scope = Scope .get_isolation_scope ()
87
+ scope ._name = "bottle"
88
+ scope .add_event_processor (
89
+ _make_request_event_processor (self , bottle_request , integration )
90
+ )
91
+ res = old_handle (self , environ )
105
92
106
- # scope cleanup
107
93
return res
108
94
109
95
Bottle ._handle = _patched_handle
110
96
111
- # monkey patch method Route._make_callback
112
97
old_make_callback = Route ._make_callback
113
98
114
99
def patched_make_callback (self , * args , ** kwargs ):
115
100
# type: (Route, *object, **object) -> Any
116
- hub = Hub . current
117
- integration = hub .get_integration (BottleIntegration )
101
+ client = sentry_sdk . get_client ()
102
+ integration = client .get_integration (BottleIntegration )
118
103
prepared_callback = old_make_callback (self , * args , ** kwargs )
119
104
if integration is None :
120
105
return prepared_callback
121
106
122
- # If an integration is there, a client has to be there.
123
- client = hub .client # type: Any
124
-
125
107
def wrapped_callback (* args , ** kwargs ):
126
108
# type: (*object, **object) -> Any
127
109
@@ -135,7 +117,7 @@ def wrapped_callback(*args, **kwargs):
135
117
client_options = client .options ,
136
118
mechanism = {"type" : "bottle" , "handled" : False },
137
119
)
138
- hub .capture_event (event , hint = hint )
120
+ sentry_sdk .capture_event (event , hint = hint )
139
121
raise exception
140
122
141
123
return res
0 commit comments