Skip to content

Commit d43c175

Browse files
authored
ref(serverless): Use new scopes API (#2896)
1 parent 523ff93 commit d43c175

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

sentry_sdk/integrations/serverless.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from functools import wraps
33

4-
from sentry_sdk.hub import Hub
4+
import sentry_sdk
55
from sentry_sdk.utils import event_from_exception, reraise
66
from sentry_sdk._types import TYPE_CHECKING
77

@@ -42,17 +42,16 @@ def wrapper(f):
4242
@wraps(f)
4343
def inner(*args, **kwargs):
4444
# type: (*Any, **Any) -> Any
45-
with Hub(Hub.current) as hub:
46-
with hub.configure_scope() as scope:
47-
scope.clear_breadcrumbs()
45+
with sentry_sdk.isolation_scope() as scope:
46+
scope.clear_breadcrumbs()
4847

4948
try:
5049
return f(*args, **kwargs)
5150
except Exception:
5251
_capture_and_reraise()
5352
finally:
5453
if flush:
55-
_flush_client()
54+
sentry_sdk.flush()
5655

5756
return inner # type: ignore
5857

@@ -65,18 +64,13 @@ def inner(*args, **kwargs):
6564
def _capture_and_reraise():
6665
# type: () -> None
6766
exc_info = sys.exc_info()
68-
hub = Hub.current
69-
if hub.client is not None:
67+
client = sentry_sdk.get_client()
68+
if client.is_active():
7069
event, hint = event_from_exception(
7170
exc_info,
72-
client_options=hub.client.options,
71+
client_options=client.options,
7372
mechanism={"type": "serverless", "handled": False},
7473
)
75-
hub.capture_event(event, hint=hint)
74+
sentry_sdk.capture_event(event, hint=hint)
7675

7776
reraise(*exc_info)
78-
79-
80-
def _flush_client():
81-
# type: () -> None
82-
return Hub.current.flush()

tests/integrations/serverless/test_serverless.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ def test_basic(sentry_init, capture_exceptions, monkeypatch):
1111

1212
@serverless_function
1313
def foo():
14-
monkeypatch.setattr(
15-
"sentry_sdk.Hub.current.flush", lambda: flush_calls.append(1)
16-
)
14+
monkeypatch.setattr("sentry_sdk.flush", lambda: flush_calls.append(1))
1715
1 / 0
1816

1917
with pytest.raises(ZeroDivisionError):
@@ -31,7 +29,7 @@ def test_flush_disabled(sentry_init, capture_exceptions, monkeypatch):
3129

3230
flush_calls = []
3331

34-
monkeypatch.setattr("sentry_sdk.Hub.current.flush", lambda: flush_calls.append(1))
32+
monkeypatch.setattr("sentry_sdk.flush", lambda: flush_calls.append(1))
3533

3634
@serverless_function(flush=False)
3735
def foo():

0 commit comments

Comments
 (0)