Skip to content

Commit 73b75f7

Browse files
Revert "feat: SDK 2.0 Support (#31)" (#32)
This reverts commit 2ea3687.
1 parent 64cffef commit 73b75f7

File tree

8 files changed

+93
-110
lines changed

8 files changed

+93
-110
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
11+
python: ['3.7', '3.8', '3.9', '3.10', '3.11']
1212
steps:
1313
- uses: actions/checkout@v3
1414
- uses: actions/setup-python@v4

README.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ Advanced Options
8484
================
8585

8686
``pytest-sentry`` supports marking your tests to use a different DSN, client or
87-
scope per-test. You can use this to provide custom options to the ``Client``
87+
hub per-test. You can use this to provide custom options to the ``Client``
8888
object from the `Sentry SDK for Python
8989
<https://github.com/getsentry/sentry-python>`_::
9090

9191
import random
9292
import pytest
9393

94-
from sentry_sdk import Scope
94+
from sentry_sdk import Hub
9595
from pytest_sentry import Client
9696

9797
@pytest.mark.sentry_client(None)
@@ -108,7 +108,7 @@ object from the `Sentry SDK for Python
108108

109109
@pytest.mark.sentry_client(Client("CUSTOM DSN"))
110110
@pytest.mark.sentry_client(lambda: Client("CUSTOM DSN"))
111-
@pytest.mark.sentry_client(Scope(Client("CUSTOM DSN")))
111+
@pytest.mark.sentry_client(Hub(Client("CUSTOM DSN")))
112112
@pytest.mark.sentry_client({"dsn": ..., "debug": True})
113113

114114

@@ -125,12 +125,12 @@ you configured this plugin with. That's because ``pytest-sentry`` goes to
125125
extreme lenghts to keep its own SDK setup separate from the SDK setup of the
126126
tested code.
127127

128-
``pytest-sentry`` exposes the ``sentry_test_scope`` fixture whose return value is
129-
the ``Scope`` being used to send events to Sentry. Use ``with use_scope(entry_test_scope):``
128+
``pytest-sentry`` exposes the ``sentry_test_hub`` fixture whose return value is
129+
the ``Hub`` being used to send events to Sentry. Use ``with sentry_test_hub:``
130130
to temporarily switch context. You can use this to set custom tags like so::
131131

132-
def test_foo(sentry_test_scope):
133-
with use_scope(sentry_test_scope):
132+
def test_foo(sentry_test_hub):
133+
with sentry_test_hub:
134134
sentry_sdk.set_tag("pull_request", os.environ['EXAMPLE_CI_PULL_REQUEST'])
135135

136136

pytest_sentry.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import sentry_sdk
88
from sentry_sdk.integrations import Integration
99

10-
from sentry_sdk import Scope, capture_exception
10+
from sentry_sdk import Hub, capture_exception
1111
from sentry_sdk.tracing import Transaction
12-
from sentry_sdk.scope import add_global_event_processor, use_scope
12+
from sentry_sdk.scope import add_global_event_processor
1313

1414
_ENVVARS_AS_TAGS = frozenset(
1515
[
@@ -59,7 +59,7 @@ def __init__(self, always_report=None):
5959
def setup_once():
6060
@add_global_event_processor
6161
def procesor(event, hint):
62-
if Scope.get_client().get_integration(PytestIntegration) is None:
62+
if Hub.current.get_integration(PytestIntegration) is None:
6363
return event
6464

6565
for key in _ENVVARS_AS_TAGS:
@@ -82,10 +82,7 @@ def procesor(event, hint):
8282
class Client(sentry_sdk.Client):
8383
def __init__(self, *args, **kwargs):
8484
kwargs.setdefault("dsn", os.environ.get("PYTEST_SENTRY_DSN", None))
85-
kwargs.setdefault(
86-
"traces_sample_rate",
87-
float(os.environ.get("PYTEST_SENTRY_TRACES_SAMPLE_RATE", 1.0)),
88-
)
85+
kwargs.setdefault("traces_sample_rate", float(os.environ.get("PYTEST_SENTRY_TRACES_SAMPLE_RATE", 1.0)))
8986
kwargs.setdefault("_experiments", {}).setdefault(
9087
"auto_enabling_integrations", True
9188
)
@@ -97,52 +94,52 @@ def __init__(self, *args, **kwargs):
9794

9895
def hookwrapper(itemgetter, **kwargs):
9996
"""
100-
A version of pytest.hookimpl that sets the current scope to the correct one
97+
A version of pytest.hookimpl that sets the current hub to the correct one
10198
and skips the hook if the integration is disabled.
10299
103100
Assumes the function is a hookwrapper, ie yields once
104101
"""
105102

106103
@wrapt.decorator
107-
def _with_scope(wrapped, instance, args, kwargs):
104+
def _with_hub(wrapped, instance, args, kwargs):
108105
item = itemgetter(*args, **kwargs)
109-
scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
106+
hub = _resolve_hub_marker_value(item.get_closest_marker("sentry_client"))
110107

111-
if scope.client.get_integration(PytestIntegration) is None:
108+
if hub.get_integration(PytestIntegration) is None:
112109
yield
113110
else:
114-
with use_scope(scope):
111+
with hub:
115112
gen = wrapped(*args, **kwargs)
116113

117114
while True:
118115
try:
119-
with use_scope(scope):
116+
with hub:
120117
chunk = next(gen)
121118

122119
y = yield chunk
123120

124-
with use_scope(scope):
121+
with hub:
125122
gen.send(y)
126123

127124
except StopIteration:
128125
break
129126

130127
def inner(f):
131-
return pytest.hookimpl(hookwrapper=True, **kwargs)(_with_scope(f))
128+
return pytest.hookimpl(hookwrapper=True, **kwargs)(_with_hub(f))
132129

133130
return inner
134131

135132

136133
def pytest_load_initial_conftests(early_config, parser, args):
137134
early_config.addinivalue_line(
138135
"markers",
139-
"sentry_client(client=None): Use this client instance for reporting tests. You can also pass a DSN string directly, or a `Scope` if you need it.",
136+
"sentry_client(client=None): Use this client instance for reporting tests. You can also pass a DSN string directly, or a `Hub` if you need it.",
140137
)
141138

142139

143140
def _start_transaction(**kwargs):
144141
transaction = Transaction.continue_from_headers(
145-
dict(Scope.get_current_scope().iter_trace_propagation_headers()), **kwargs
142+
dict(Hub.current.iter_trace_propagation_headers()), **kwargs
146143
)
147144
transaction.same_process_as_parent = True
148145
return sentry_sdk.start_transaction(transaction)
@@ -157,7 +154,7 @@ def pytest_runtest_protocol(item):
157154
# We use the full name including parameters because then we can identify
158155
# how often a single test has run as part of the same GITHUB_RUN_ID.
159156

160-
with _start_transaction(op=op, name="{} {}".format(op, name)) as tx:
157+
with _start_transaction(op=op, name=u"{} {}".format(op, name)) as tx:
161158
yield
162159

163160
# Purposefully drop transaction to spare quota. We only created it to
@@ -174,16 +171,14 @@ def pytest_runtest_call(item):
174171
# We use the full name including parameters because then we can identify
175172
# how often a single test has run as part of the same GITHUB_RUN_ID.
176173

177-
with _start_transaction(op=op, name="{} {}".format(op, name)):
174+
with _start_transaction(op=op, name=u"{} {}".format(op, name)):
178175
yield
179176

180177

181178
@hookwrapper(itemgetter=lambda fixturedef, request: request._pyfuncitem)
182179
def pytest_fixture_setup(fixturedef, request):
183180
op = "pytest.fixture.setup"
184-
with _start_transaction(
185-
op=op, name="{} {}".format(op, fixturedef.argname)
186-
) as transaction:
181+
with _start_transaction(op=op, name=u"{} {}".format(op, fixturedef.argname)) as transaction:
187182
transaction.set_tag("pytest.fixture.scope", fixturedef.scope)
188183
yield
189184

@@ -203,31 +198,31 @@ def pytest_runtest_makereport(item, call):
203198
call.excinfo
204199
]
205200

206-
integration = Scope.get_client().get_integration(PytestIntegration)
201+
integration = Hub.current.get_integration(PytestIntegration)
207202

208203
if (cur_exc_chain and call.excinfo is None) or integration.always_report:
209204
for exc_info in cur_exc_chain:
210205
capture_exception((exc_info.type, exc_info.value, exc_info.tb))
211206

212207

213-
DEFAULT_SCOPE = Scope(client=Client())
208+
DEFAULT_HUB = Hub(Client())
214209

215-
_scope_cache = {}
210+
_hub_cache = {}
216211

217212

218-
def _resolve_scope_marker_value(marker_value):
219-
if id(marker_value) not in _scope_cache:
220-
_scope_cache[id(marker_value)] = rv = _resolve_scope_marker_value_uncached(
213+
def _resolve_hub_marker_value(marker_value):
214+
if id(marker_value) not in _hub_cache:
215+
_hub_cache[id(marker_value)] = rv = _resolve_hub_marker_value_uncached(
221216
marker_value
222217
)
223218
return rv
224219

225-
return _scope_cache[id(marker_value)]
220+
return _hub_cache[id(marker_value)]
226221

227222

228-
def _resolve_scope_marker_value_uncached(marker_value):
223+
def _resolve_hub_marker_value_uncached(marker_value):
229224
if marker_value is None:
230-
marker_value = DEFAULT_SCOPE
225+
marker_value = DEFAULT_HUB
231226
else:
232227
marker_value = marker_value.args[0]
233228

@@ -236,35 +231,35 @@ def _resolve_scope_marker_value_uncached(marker_value):
236231

237232
if marker_value is None:
238233
# user explicitly disabled reporting
239-
return Scope()
234+
return Hub()
240235

241236
if isinstance(marker_value, str):
242-
return Scope(client=Client(marker_value))
237+
return Hub(Client(marker_value))
243238

244239
if isinstance(marker_value, dict):
245-
return Scope(client=Client(**marker_value))
240+
return Hub(Client(**marker_value))
246241

247242
if isinstance(marker_value, Client):
248-
return Scope(client=marker_value)
243+
return Hub(marker_value)
249244

250-
if isinstance(marker_value, Scope):
245+
if isinstance(marker_value, Hub):
251246
return marker_value
252247

253248
raise RuntimeError(
254-
"The `sentry_client` value must be a client, scope or string, not {}".format(
249+
"The `sentry_client` value must be a client, hub or string, not {}".format(
255250
repr(type(marker_value))
256251
)
257252
)
258253

259254

260255
@pytest.fixture
261-
def sentry_test_scope(request):
256+
def sentry_test_hub(request):
262257
"""
263-
Gives back the current scope.
258+
Gives back the current hub.
264259
"""
265260

266261
item = request.node
267-
return _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
262+
return _resolve_hub_marker_value(item.get_closest_marker("sentry_client"))
268263

269264

270265
def _process_stacktrace(stacktrace):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ classifiers =
1616
py_modules = pytest_sentry
1717
install_requires =
1818
pytest
19-
sentry-sdk==2.0.0rc4
19+
sentry-sdk
2020
wrapt
2121

2222
[options.entry_points]

tests/test_envvars.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22
import sentry_sdk
3-
from sentry_sdk.scope import use_scope
43
import pytest_sentry
54

65
events = []
@@ -27,9 +26,9 @@ def clear_events(monkeypatch):
2726
pytestmark = pytest.mark.sentry_client(pytest_sentry.Client(transport=MyTransport()))
2827

2928

30-
def test_basic(sentry_test_scope):
31-
with use_scope(sentry_test_scope):
32-
sentry_test_scope.capture_message("hi")
29+
def test_basic(sentry_test_hub):
30+
with sentry_test_hub:
31+
sentry_test_hub.capture_message("hi")
3332

3433
(event,) = events
3534
assert event["tags"]["pytest_environ.GITHUB_RUN_ID"] == "123abc"

tests/test_fixture.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
pytestmark = pytest.mark.sentry_client(GLOBAL_CLIENT)
88

99

10-
def test_basic(sentry_test_scope):
11-
assert sentry_test_scope.client is GLOBAL_CLIENT
10+
def test_basic(sentry_test_hub):
11+
assert sentry_test_hub.client is GLOBAL_CLIENT
1212

1313

1414
@pytest.mark.sentry_client(None)
15-
def test_func(sentry_test_scope):
16-
assert not sentry_test_scope.client.is_active()
15+
def test_func(sentry_test_hub):
16+
assert sentry_test_hub.client is None

tests/test_hub.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from __future__ import absolute_import
2+
3+
import pytest
4+
import unittest
5+
import sentry_sdk
6+
import pytest_sentry
7+
8+
pytestmark = pytest.mark.sentry_client(pytest_sentry.Client())
9+
10+
_DEFAULT_HUB = sentry_sdk.Hub.current
11+
12+
13+
def _assert_right_hubs():
14+
for hub in sentry_sdk.Hub.current, sentry_sdk.Hub.main:
15+
assert hub.client is None
16+
assert hub is _DEFAULT_HUB
17+
18+
19+
def test_basic():
20+
_assert_right_hubs()
21+
22+
23+
class TestSimpleClass(object):
24+
def setup_method(self):
25+
_assert_right_hubs()
26+
27+
def test_basic(self):
28+
_assert_right_hubs()
29+
30+
def teardown_method(self):
31+
_assert_right_hubs()
32+
33+
34+
class TestUnittestClass(unittest.TestCase):
35+
def setUp(self):
36+
_assert_right_hubs()
37+
38+
def test_basic(self):
39+
_assert_right_hubs()
40+
41+
def tearDown(self):
42+
_assert_right_hubs()

0 commit comments

Comments
 (0)