Skip to content

Commit 99607da

Browse files
authored
ref(bottle): Use new scopes API (#2872)
1 parent 530d32a commit 99607da

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

sentry_sdk/integrations/bottle.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from sentry_sdk.hub import Hub
1+
import sentry_sdk
22
from sentry_sdk.tracing import SOURCE_FOR_STYLE
33
from sentry_sdk.utils import (
44
capture_internal_exceptions,
5+
ensure_integration_enabled,
56
event_from_exception,
67
parse_version,
78
transaction_from_function,
89
)
910
from sentry_sdk.integrations import Integration, DidNotEnable
1011
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1112
from sentry_sdk.integrations._wsgi_common import RequestExtractor
12-
13+
from sentry_sdk.scope import Scope
1314
from sentry_sdk._types import TYPE_CHECKING
1415

1516
if TYPE_CHECKING:
@@ -55,7 +56,6 @@ def __init__(self, transaction_style="endpoint"):
5556
@staticmethod
5657
def setup_once():
5758
# type: () -> None
58-
5959
version = parse_version(BOTTLE_VERSION)
6060

6161
if version is None:
@@ -64,64 +64,46 @@ def setup_once():
6464
if version < (0, 12):
6565
raise DidNotEnable("Bottle 0.12 or newer required.")
6666

67-
# monkey patch method Bottle.__call__
6867
old_app = Bottle.__call__
6968

69+
@ensure_integration_enabled(BottleIntegration, old_app)
7070
def sentry_patched_wsgi_app(self, environ, start_response):
7171
# 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-
7872
return SentryWsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))(
7973
environ, start_response
8074
)
8175

8276
Bottle.__call__ = sentry_patched_wsgi_app
8377

84-
# monkey patch method Bottle._handle
8578
old_handle = Bottle._handle
8679

8780
def _patched_handle(self, environ):
8881
# 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)
9183
if integration is None:
9284
return old_handle(self, environ)
9385

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)
10592

106-
# scope cleanup
10793
return res
10894

10995
Bottle._handle = _patched_handle
11096

111-
# monkey patch method Route._make_callback
11297
old_make_callback = Route._make_callback
11398

11499
def patched_make_callback(self, *args, **kwargs):
115100
# 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)
118103
prepared_callback = old_make_callback(self, *args, **kwargs)
119104
if integration is None:
120105
return prepared_callback
121106

122-
# If an integration is there, a client has to be there.
123-
client = hub.client # type: Any
124-
125107
def wrapped_callback(*args, **kwargs):
126108
# type: (*object, **object) -> Any
127109

@@ -135,7 +117,7 @@ def wrapped_callback(*args, **kwargs):
135117
client_options=client.options,
136118
mechanism={"type": "bottle", "handled": False},
137119
)
138-
hub.capture_event(event, hint=hint)
120+
sentry_sdk.capture_event(event, hint=hint)
139121
raise exception
140122

141123
return res

0 commit comments

Comments
 (0)