Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 9215192

Browse files
committed
Don't use singleton
1 parent 96153fe commit 9215192

File tree

4 files changed

+56
-45
lines changed

4 files changed

+56
-45
lines changed

seamapi/seam.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ def __init__(
7373
self.should_report_exceptions = should_report_exceptions
7474

7575
if self.should_report_exceptions:
76-
sentry_sdk.init(
76+
self.sentry_client = sentry_sdk.Hub(sentry_sdk.Client(
7777
dsn=get_sentry_dsn(),
78-
default_integrations=False,
79-
)
80-
sentry_sdk.set_context("sdk_info", {
78+
))
79+
self.sentry_client.scope.set_context("sdk_info", {
8180
"repository": "https://github.com/seamapi/python",
8281
"version": pkg_resources.get_distribution("seamapi").version,
8382
"endpoint": self.api_url,
@@ -107,7 +106,7 @@ def make_request(self, method: str, path: str, **kwargs):
107106

108107
if self.should_report_exceptions and response.status_code:
109108
# Add breadcrumb
110-
sentry_sdk.add_breadcrumb(
109+
self.sentry_client.add_breadcrumb(
111110
category="http",
112111
level="info",
113112
data={

seamapi/utils/report_error.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from seamapi.types import SeamAPIException
2-
import sentry_sdk
32

43
"""
54
A decorator for model methods that will report errors to Sentry (if enabled).
@@ -11,7 +10,7 @@ def wrapper(self, *args, **kwargs):
1110
return f(self, *args, **kwargs)
1211
except Exception as error:
1312
if self.seam.should_report_exceptions and type(error) is not SeamAPIException:
14-
sentry_sdk.capture_exception(error)
13+
self.seam.sentry_client.capture_exception(error)
1514

1615
raise error
1716
return wrapper

tests/conftest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,41 @@ def seam(seam_backend: Any, dotenv_fixture: Any):
6868
seam = Seam(api_url=seam_backend.url, api_key=seam_backend.sandbox_api_key)
6969
seam.make_request("POST", "/workspaces/reset_sandbox")
7070
yield seam
71+
72+
@pytest.fixture
73+
def fake_sentry(monkeypatch):
74+
sentry_dsn = "https://[email protected]/123"
75+
76+
monkeypatch.setenv("SENTRY_DSN", sentry_dsn)
77+
78+
sentry_init_args = {}
79+
sentry_capture_exception_calls = []
80+
sentry_add_breadcrumb_calls = []
81+
class TestSentryClient(object):
82+
def __init__(self, *args, **kwargs):
83+
sentry_init_args.update(kwargs)
84+
def set_context(self, *args, **kwargs):
85+
pass
86+
87+
monkeypatch.setattr("sentry_sdk.Client", TestSentryClient)
88+
89+
class TestSentryScope(object):
90+
def set_context(self, *args, **kwargs):
91+
pass
92+
93+
class TestSentryHub(object):
94+
def __init__(self, *args, **kwargs):
95+
self.scope = TestSentryScope()
96+
def capture_exception(self, *args, **kwargs):
97+
sentry_capture_exception_calls.append((args, kwargs))
98+
def add_breadcrumb(self, *args, **kwargs):
99+
sentry_add_breadcrumb_calls.append((args, kwargs))
100+
101+
monkeypatch.setattr("sentry_sdk.Hub", TestSentryHub)
102+
103+
yield {
104+
"sentry_init_args": sentry_init_args,
105+
"sentry_capture_exception_calls": sentry_capture_exception_calls,
106+
"sentry_add_breadcrumb_calls": sentry_add_breadcrumb_calls,
107+
"sentry_dsn": sentry_dsn,
108+
}

tests/test_sentry_errors.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
@responses.activate
6-
def test_sends_error_to_sentry(seam: Seam, monkeypatch):
6+
def test_sends_error_to_sentry(seam: Seam, fake_sentry):
77
rsp = responses.Response(
88
method="GET",
99
url=seam.api_url + "/devices/list",
@@ -13,42 +13,31 @@ def test_sends_error_to_sentry(seam: Seam, monkeypatch):
1313
)
1414
responses.add(rsp)
1515

16-
sentry_init_args = {}
17-
sentry_dsn = "https://sentry.io/123"
18-
monkeypatch.setenv("SENTRY_DSN", sentry_dsn)
19-
monkeypatch.setattr("sentry_sdk.init", lambda *a, **kw: sentry_init_args.update(kw))
20-
2116
client_with_sentry = Seam(
2217
api_key=seam.api_key,
2318
api_url=seam.api_url,
2419
should_report_exceptions=True,
2520
)
2621

27-
assert sentry_init_args["dsn"] == sentry_dsn
28-
assert sentry_init_args["default_integrations"] is False
29-
30-
sentry_capture_exception_calls = []
31-
monkeypatch.setattr("sentry_sdk.capture_exception", lambda *a, **kw: sentry_capture_exception_calls.append((a, kw)))
22+
assert fake_sentry["sentry_init_args"]["dsn"] == fake_sentry["sentry_dsn"]
3223

33-
sentry_add_breadcrumb_calls = []
34-
monkeypatch.setattr("sentry_sdk.add_breadcrumb", lambda *a, **kw: sentry_add_breadcrumb_calls.append((a, kw)))
3524
try:
3625
client_with_sentry.devices.list()
3726
assert False
38-
except:
27+
except Exception as error:
3928
pass
4029

4130
assert rsp.call_count == 1
4231

43-
assert len(sentry_capture_exception_calls) == 1
44-
assert type(sentry_capture_exception_calls[0][0][0]) is KeyError
32+
assert len(fake_sentry["sentry_capture_exception_calls"]) == 1
33+
assert type(fake_sentry["sentry_capture_exception_calls"][0][0][0]) is KeyError
4534

46-
assert len(sentry_add_breadcrumb_calls) == 1
47-
assert sentry_add_breadcrumb_calls[0][1]['category'] == "http"
48-
assert sentry_add_breadcrumb_calls[0][1]["data"]["request_id"] == "1234"
35+
assert len(fake_sentry["sentry_add_breadcrumb_calls"]) == 1
36+
assert fake_sentry["sentry_add_breadcrumb_calls"][0][1]['category'] == "http"
37+
assert fake_sentry["sentry_add_breadcrumb_calls"][0][1]["data"]["request_id"] == "1234"
4938

5039
@responses.activate
51-
def test_skips_sentry_reporting(seam: Seam, monkeypatch):
40+
def test_skips_sentry_reporting(seam: Seam, fake_sentry):
5241
rsp = responses.Response(
5342
method="GET",
5443
url=seam.api_url + "/devices/list",
@@ -57,17 +46,11 @@ def test_skips_sentry_reporting(seam: Seam, monkeypatch):
5746
)
5847
responses.add(rsp)
5948

60-
monkeypatch.setenv("SENTRY_DSN", "https://sentry.io/123")
61-
monkeypatch.setattr("sentry_sdk.init", lambda *a, **kw: None)
62-
6349
client_without_sentry = Seam(
6450
api_key=seam.api_key,
6551
api_url=seam.api_url,
6652
)
6753

68-
sentry_capture_exception_calls = []
69-
monkeypatch.setattr("sentry_sdk.capture_exception", lambda *a, **kw: sentry_capture_exception_calls.append((a, kw)))
70-
7154
try:
7255
client_without_sentry.devices.list()
7356
assert False
@@ -76,29 +59,21 @@ def test_skips_sentry_reporting(seam: Seam, monkeypatch):
7659

7760
assert rsp.call_count == 1
7861

79-
assert len(sentry_capture_exception_calls) == 0
80-
81-
def test_skips_report_for_known_error(seam: Seam, monkeypatch):
82-
sentry_init_args = {}
83-
sentry_dsn = "https://sentry.io/123"
84-
monkeypatch.setenv("SENTRY_DSN", sentry_dsn)
85-
monkeypatch.setattr("sentry_sdk.init", lambda *a, **kw: sentry_init_args.update(kw))
62+
assert len(fake_sentry["sentry_capture_exception_calls"]) == 0
8663

64+
def test_skips_report_for_known_error(seam: Seam, fake_sentry):
8765
client_without_sentry = Seam(
8866
api_key=seam.api_key,
8967
api_url=seam.api_url,
9068
should_report_exceptions=True
9169
)
9270

93-
assert sentry_init_args["dsn"] == sentry_dsn
94-
95-
sentry_capture_exception_calls = []
96-
monkeypatch.setattr("sentry_sdk.capture_exception", lambda *a, **kw: sentry_capture_exception_calls.append((a, kw)))
71+
assert fake_sentry["sentry_init_args"]["dsn"] == fake_sentry["sentry_dsn"]
9772

9873
try:
9974
client_without_sentry.devices.get("123")
10075
assert False
10176
except:
10277
pass
10378

104-
assert len(sentry_capture_exception_calls) == 0
79+
assert len(fake_sentry["sentry_capture_exception_calls"]) == 0

0 commit comments

Comments
 (0)