Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #161, use the sentry isolation scope instead of the deprecated hub #162

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include = ["*.md", "*.toml", "*.txt", "*.yml", "*.yaml", ".coveragerc", "tox.ini

[tool.poetry.dependencies]
python = "^3.7"
sentry-sdk = "*"
sentry-sdk = "^2.0"
structlog = "*"

[tool.poetry.dev-dependencies]
Expand Down
18 changes: 9 additions & 9 deletions structlog_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, Optional
from collections.abc import MutableMapping, Iterable

from sentry_sdk import Hub
from sentry_sdk import Scope, get_isolation_scope
from sentry_sdk.integrations.logging import _IGNORED_LOGGERS
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from structlog.types import EventDict, ExcInfo, WrappedLogger
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(
tag_keys: list[str] | str | None = None,
ignore_loggers: Iterable[str] | None = None,
verbose: bool = False,
hub: Hub | None = None,
scope: Scope | None = None,
) -> None:
"""
:param level: Events of this or higher levels will be reported as
Expand All @@ -66,15 +66,15 @@ def __init__(
:param ignore_loggers: A list of logger names to ignore any events from.
:param verbose: Report the action taken by the logger in the `event_dict`.
Default is :obj:`False`.
:param hub: Optionally specify :obj:`sentry_sdk.Hub`.
:param scope: Optionally specify :obj:`sentry_sdk.Scope`.
"""
self.event_level = event_level
self.level = level
self.active = active
self.tag_keys = tag_keys
self.verbose = verbose

self._hub = hub
self._scope = scope
self._as_context = as_context
self._original_event_dict: dict = {}
self.ignore_breadcrumb_data = ignore_breadcrumb_data
Expand Down Expand Up @@ -107,8 +107,8 @@ def _get_logger_name(

return logger_name

def _get_hub(self) -> Hub:
return self._hub or Hub.current
def _get_scope(self) -> Scope:
return self._scope or get_isolation_scope()

def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
"""Create a sentry event and hint from structlog `event_dict` and sys.exc_info.
Expand All @@ -119,7 +119,7 @@ def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
has_exc_info = exc_info and exc_info != (None, None, None)

if has_exc_info:
client = self._get_hub().client
client = self._get_scope().client
options: dict[str, Any] = client.options if client else {}
event, hint = event_from_exception(
exc_info,
Expand Down Expand Up @@ -172,7 +172,7 @@ def _can_record(self, logger: WrappedLogger, event_dict: EventDict) -> bool:
def _handle_event(self, event_dict: EventDict) -> None:
with capture_internal_exceptions():
event, hint = self._get_event_and_hint(event_dict)
sid = self._get_hub().capture_event(event, hint=hint)
sid = self._get_scope().capture_event(event, hint=hint)
if sid:
event_dict["sentry_id"] = sid
if self.verbose:
Expand All @@ -181,7 +181,7 @@ def _handle_event(self, event_dict: EventDict) -> None:
def _handle_breadcrumb(self, event_dict: EventDict) -> None:
with capture_internal_exceptions():
event, hint = self._get_breadcrumb_and_hint(event_dict)
self._get_hub().add_breadcrumb(event, hint=hint)
self._get_scope().add_breadcrumb(event, hint=hint)

@staticmethod
def _get_level_value(level_name: str) -> int:
Expand Down
32 changes: 21 additions & 11 deletions test/test_sentry_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,31 @@ def from_request(cls, request):
return cls()


class CaptureTransport(sentry_sdk.Transport):
def __init__(self):
super().__init__()
self.events = []

def capture_envelope(self, envelope):
event = envelope.get_event()
if event is not None:
self.events.append(event)


@pytest.fixture
def sentry_events(request):
params = ClientParams.from_request(request)
transport = CaptureTransport()
client = sentry_sdk.Client(
transport=transport,
integrations=INTEGRATIONS,
auto_enabling_integrations=False,
include_local_variables=params.include_local_variables,
)

events = []
with sentry_sdk.Hub() as hub:
hub.bind_client(
sentry_sdk.Client(
transport=events.append,
integrations=INTEGRATIONS,
auto_enabling_integrations=False,
include_local_variables=params.include_local_variables,
)
)
yield events
with sentry_sdk.isolation_scope() as scope:
scope.set_client(client)
yield transport.events


def assert_event_dict(event_data, sentry_events, number_of_events=1, error=None):
Expand Down
Loading