Skip to content

Commit

Permalink
fix: use isolation scope instead of Hub
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdarcet committed Oct 19, 2024
1 parent 0a6d19a commit 4a1c4ea
Showing 1 changed file with 9 additions and 9 deletions.
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

0 comments on commit 4a1c4ea

Please sign in to comment.