Skip to content

Commit

Permalink
No more hubs!
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Mar 7, 2024
1 parent 31a678a commit 448db6d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ Advanced Options
================

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

import random
import pytest

from sentry_sdk import Hub
from sentry_sdk import Scope
from pytest_sentry import Client

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

@pytest.mark.sentry_client(Client("CUSTOM DSN"))
@pytest.mark.sentry_client(lambda: Client("CUSTOM DSN"))
@pytest.mark.sentry_client(Hub(Client("CUSTOM DSN")))
@pytest.mark.sentry_client(Scope(Client("CUSTOM DSN")))
@pytest.mark.sentry_client({"dsn": ..., "debug": True})


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

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

def test_foo(sentry_test_hub):
with sentry_test_hub:
def test_foo(sentry_test_scope):
with sentry_test_scope:
sentry_sdk.set_tag("pull_request", os.environ['EXAMPLE_CI_PULL_REQUEST'])


Expand Down
22 changes: 11 additions & 11 deletions pytest_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import sentry_sdk
from sentry_sdk.integrations import Integration

from sentry_sdk import Hub, Scope, capture_exception
from sentry_sdk import Scope, capture_exception
from sentry_sdk.tracing import Transaction
from sentry_sdk.scope import add_global_event_processor
from sentry_sdk.scope import add_global_event_processor, use_isolation_scope

_ENVVARS_AS_TAGS = frozenset(
[
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self, always_report=None):
def setup_once():
@add_global_event_processor
def procesor(event, hint):
if Hub.current.get_integration(PytestIntegration) is None:
if Scope.get_client().get_integration(PytestIntegration) is None:
return event

for key in _ENVVARS_AS_TAGS:
Expand Down Expand Up @@ -108,20 +108,20 @@ def _with_scope(wrapped, instance, args, kwargs):
item = itemgetter(*args, **kwargs)
scope = _resolve_scope_marker_value(item.get_closest_marker("sentry_client"))

if scope.get_client().get_integration(PytestIntegration) is None:
if scope.client.get_integration(PytestIntegration) is None:
yield
else:
with sentry_sdk.push_scope(): # TODO: What do we do with scope here?
with use_isolation_scope(scope): # TODO: What do we do with scope here?
gen = wrapped(*args, **kwargs)

while True:
try:
with sentry_sdk.push_scope():
with use_isolation_scope(scope):
chunk = next(gen)

y = yield chunk

with sentry_sdk.push_scope():
with use_isolation_scope(scope):
gen.send(y)

except StopIteration:
Expand All @@ -142,7 +142,7 @@ def pytest_load_initial_conftests(early_config, parser, args):

def _start_transaction(**kwargs):
transaction = Transaction.continue_from_headers(
dict(Hub.current.iter_trace_propagation_headers()), **kwargs
dict(Scope.get_current_scope().iter_trace_propagation_headers()), **kwargs
)
transaction.same_process_as_parent = True
return sentry_sdk.start_transaction(transaction)
Expand Down Expand Up @@ -203,7 +203,7 @@ def pytest_runtest_makereport(item, call):
call.excinfo
]

integration = Hub.current.get_integration(PytestIntegration)
integration = Scope.get_client().get_integration(PytestIntegration)

if (cur_exc_chain and call.excinfo is None) or integration.always_report:
for exc_info in cur_exc_chain:
Expand Down Expand Up @@ -258,9 +258,9 @@ def _resolve_scope_marker_value_uncached(marker_value):


@pytest.fixture
def sentry_test_hub(request):
def sentry_test_scope(request):
"""
Gives back the current hub.
Gives back the current scope.
"""

item = request.node
Expand Down
7 changes: 4 additions & 3 deletions tests/test_envvars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import sentry_sdk
from sentry_sdk.scope import use_scope
import pytest_sentry

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


def test_basic(sentry_test_hub):
with sentry_test_hub:
sentry_test_hub.capture_message("hi")
def test_basic(sentry_test_scope):
with use_scope(sentry_test_scope):
sentry_test_scope.capture_message("hi")

(event,) = events
assert event["tags"]["pytest_environ.GITHUB_RUN_ID"] == "123abc"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
pytestmark = pytest.mark.sentry_client(GLOBAL_CLIENT)


def test_basic(sentry_test_hub):
assert sentry_test_hub.client is GLOBAL_CLIENT
def test_basic(sentry_test_scope):
assert sentry_test_scope.client is GLOBAL_CLIENT


@pytest.mark.sentry_client(None)
def test_func(sentry_test_hub):
assert sentry_test_hub.client is None
def test_func(sentry_test_scope):
assert not sentry_test_scope.client.is_active()
17 changes: 10 additions & 7 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

pytestmark = pytest.mark.sentry_client(pytest_sentry.Client())

_DEFAULT_SCOPE = sentry_sdk.Scope.get_isolation_scope()
_DEFAULT_GLOBAL_SCOPE = sentry_sdk.Scope.get_global_scope()
_DEFAULT_ISOLATION_SCOPE = sentry_sdk.Scope.get_isolation_scope()


def _assert_right_scopes():
for scope in (
sentry_sdk.Scope.get_global_scope(),
sentry_sdk.Scope.get_isolation_scope(),
):
assert not scope.get_client().is_active()
assert scope is _DEFAULT_SCOPE
global_scope = sentry_sdk.Scope.get_global_scope()
isolation_scope = sentry_sdk.Scope.get_isolation_scope()

assert not global_scope.get_client().is_active()
assert global_scope is _DEFAULT_GLOBAL_SCOPE

assert not isolation_scope.get_client().is_active()
assert isolation_scope is _DEFAULT_ISOLATION_SCOPE


def test_basic():
Expand Down

0 comments on commit 448db6d

Please sign in to comment.