Skip to content

Commit 6afd366

Browse files
committed
Correct semantics to use isolation scope and not current scope
Previously this library was based on hubs which maps to the isolation scope now.
1 parent 9871391 commit 6afd366

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ extreme lenghts to keep its own SDK setup separate from the SDK setup of the
153153
tested code.
154154

155155
`pytest-sentry` exposes the `sentry_test_scope` fixture whose return value is
156-
the `Scope` being used to send events to Sentry. Use `with use_scope(entry_test_scope):`
156+
the isolation `Scope` being used to send events to Sentry. Use `with use_isolation_scope(sentry_test_scope)`:
157157
to temporarily switch context. You can use this to set custom tags like so::
158158

159159
```python
160160
def test_foo(sentry_test_scope):
161-
with use_scope(sentry_test_scope):
161+
with use_isolation_scope(sentry_test_scope):
162162
sentry_sdk.set_tag("pull_request", os.environ['EXAMPLE_CI_PULL_REQUEST'])
163163
```
164164

pytest_sentry/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@pytest.fixture
77
def sentry_test_scope(request):
88
"""
9-
Gives back the current scope.
9+
Gives back the isolation Scope.
1010
"""
1111

1212
item = request.node

pytest_sentry/helpers.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import sentry_sdk
2+
from sentry_sdk.scope import ScopeType
23

34
from pytest_sentry.client import Client
45

56

6-
DEFAULT_SCOPE = sentry_sdk.Scope(client=Client())
7+
DEFAULT_ISOLATION_SCOPE = sentry_sdk.Scope(ty=ScopeType.ISOLATION)
8+
DEFAULT_ISOLATION_SCOPE.set_client(Client())
79

8-
_scope_cache = {}
10+
_isolation_scope_cache = {}
911

1012

1113
def _resolve_scope_marker_value(marker_value):
12-
if id(marker_value) not in _scope_cache:
13-
_scope_cache[id(marker_value)] = rv = _resolve_scope_marker_value_uncached(
14+
if id(marker_value) not in _isolation_scope_cache:
15+
_isolation_scope_cache[id(marker_value)] = rv = _resolve_scope_marker_value_uncached(
1416
marker_value
1517
)
1618
return rv
1719

18-
return _scope_cache[id(marker_value)]
20+
return _isolation_scope_cache[id(marker_value)]
1921

2022

2123
def _resolve_scope_marker_value_uncached(marker_value):
@@ -37,25 +39,25 @@ def _resolve_scope_marker_value_uncached(marker_value):
3739

3840
if isinstance(marker_value, str):
3941
# If a DSN string is provided, create a new client and use that
40-
scope = sentry_sdk.get_current_scope()
42+
scope = sentry_sdk.get_isolation_scope()
4143
scope.set_client(Client(marker_value))
4244
return scope
4345

4446
if isinstance(marker_value, dict):
4547
# If a dict is provided, create a new client using the dict as Client options
46-
scope = sentry_sdk.get_current_scope()
48+
scope = sentry_sdk.get_isolation_scope()
4749
scope.set_client(Client(**marker_value))
4850
return scope
4951

5052
if isinstance(marker_value, Client):
5153
# If a Client instance is provided, use that
52-
scope = sentry_sdk.get_current_scope()
54+
scope = sentry_sdk.get_isolation_scope()
5355
scope.set_client(marker_value)
5456
return scope
5557

5658
if isinstance(marker_value, sentry_sdk.Scope):
5759
# If a Scope instance is provided, use the client from it
58-
scope = sentry_sdk.get_current_scope()
60+
scope = sentry_sdk.get_isolation_scope()
5961
scope.set_client(marker_value.client)
6062
return marker_value
6163

pytest_sentry/hooks.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ def hookwrapper(itemgetter, **kwargs):
2727
"""
2828

2929
@wrapt.decorator
30-
def _with_scope(wrapped, instance, args, kwargs):
30+
def _with_isolation_scope(wrapped, instance, args, kwargs):
3131
item = itemgetter(*args, **kwargs)
32-
scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
32+
isolation_scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
3333

34-
if scope.client.get_integration(PytestIntegration) is None:
34+
if isolation_scope.client.get_integration(PytestIntegration) is None:
3535
yield
3636
else:
37-
with sentry_sdk.use_scope(scope):
37+
with sentry_sdk.use_isolation_scope(isolation_scope):
3838
gen = wrapped(*args, **kwargs)
3939

4040
while True:
4141
try:
42-
with sentry_sdk.use_scope(scope):
42+
with sentry_sdk.use_isolation_scope(isolation_scope):
4343
chunk = next(gen)
4444

4545
y = yield chunk
4646

47-
with sentry_sdk.use_scope(scope):
47+
with sentry_sdk.use_isolation_scope(isolation_scope):
4848
gen.send(y)
4949

5050
except StopIteration:
5151
break
5252

5353
def inner(f):
54-
return pytest.hookimpl(hookwrapper=True, **kwargs)(_with_scope(f))
54+
return pytest.hookimpl(hookwrapper=True, **kwargs)(_with_isolation_scope(f))
5555

5656
return inner
5757

@@ -89,7 +89,7 @@ def pytest_runtest_call(item):
8989

9090
# We use the full name including parameters because then we can identify
9191
# how often a single test has run as part of the same GITHUB_RUN_ID.
92-
with sentry_sdk.continue_trace(dict(sentry_sdk.get_current_scope().iter_trace_propagation_headers())):
92+
with sentry_sdk.continue_trace(dict(sentry_sdk.get_isolation_scope().iter_trace_propagation_headers())):
9393
with sentry_sdk.start_span(op=op, name=name) as span:
9494
span.set_attribute("pytest-sentry.rerun", is_rerun)
9595
if is_rerun:
@@ -107,7 +107,7 @@ def pytest_fixture_setup(fixturedef, request):
107107
op = "pytest.fixture.setup"
108108
name = "{} {}".format(op, fixturedef.argname)
109109

110-
with sentry_sdk.continue_trace(dict(sentry_sdk.get_current_scope().iter_trace_propagation_headers())):
110+
with sentry_sdk.continue_trace(dict(sentry_sdk.get_isolation_scope().iter_trace_propagation_headers())):
111111
with sentry_sdk.start_span(op=op, name=name) as root_span:
112112
root_span.set_tag("pytest.fixture.scope", fixturedef.scope)
113113
yield
@@ -135,8 +135,8 @@ def pytest_runtest_makereport(item, call):
135135
call.excinfo
136136
]
137137

138-
scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
139-
integration = scope.client.get_integration(PytestIntegration)
138+
isolation_scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))
139+
integration = isolation_scope.client.get_integration(PytestIntegration)
140140

141141
if (cur_exc_chain and call.excinfo is None) or (integration is not None and integration.always_report):
142142
for exc_info in cur_exc_chain:

tests/test_envvars.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def capture_envelope(self, envelope):
3232

3333

3434
def test_basic(sentry_test_scope):
35-
with sentry_sdk.use_scope(sentry_test_scope):
36-
sentry_test_scope.capture_message("hi")
35+
isolation_scope = sentry_test_scope
36+
with sentry_sdk.use_isolation_scope(isolation_scope):
37+
isolation_scope.capture_message("hi")
3738

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

tests/test_scope.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
pytestmark = pytest.mark.sentry_client(GLOBAL_CLIENT)
1111

12-
_DEFAULT_GLOBAL_SCOPE = sentry_sdk.Scope.get_global_scope()
13-
_DEFAULT_ISOLATION_SCOPE = sentry_sdk.Scope.get_isolation_scope()
12+
_DEFAULT_GLOBAL_SCOPE = sentry_sdk.get_global_scope()
13+
_DEFAULT_ISOLATION_SCOPE = sentry_sdk.get_isolation_scope()
1414

1515

1616
def _assert_right_scopes():
17-
global_scope = sentry_sdk.Scope.get_global_scope()
17+
global_scope = sentry_sdk.get_global_scope()
1818
assert global_scope is _DEFAULT_GLOBAL_SCOPE
1919

20-
isolation_scope = sentry_sdk.Scope.get_isolation_scope()
20+
isolation_scope = sentry_sdk.get_isolation_scope()
2121
assert isolation_scope is _DEFAULT_ISOLATION_SCOPE
2222

2323

0 commit comments

Comments
 (0)