Skip to content

Commit

Permalink
fix: compatibility with newer Sentry SDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
jyggen authored and paveldedik committed Apr 18, 2023
1 parent 52c08c0 commit 2025df9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

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

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

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

Expand Down
13 changes: 11 additions & 2 deletions structlog_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
from fnmatch import fnmatch
from typing import Any, Iterable, Optional

from sentry_sdk import Hub
from packaging import version

from sentry_sdk import VERSION, Hub
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

_IS_OLD_VERSION = version.parse(VERSION) < version.parse("1.17.0")


def _figure_out_exc_info(v: Any) -> ExcInfo:
"""
Expand Down Expand Up @@ -107,10 +111,15 @@ 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:
options = self._get_hub().client.options
include_local_variables = options.get(
"include_local_variables", options.get("with_locals")
)
key = "with_locals" if _IS_OLD_VERSION else "include_local_variables"
event, hint = event_from_exception(
exc_info,
client_options={
"with_locals": self._get_hub().client.options.get("with_locals")
key: include_local_variables,
},
)
else:
Expand Down
12 changes: 8 additions & 4 deletions test/test_sentry_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@dataclass
class ClientParams:
with_locals: bool = True
include_local_variables: bool = True

@classmethod
def from_request(cls, request):
Expand All @@ -40,7 +40,7 @@ def sentry_events(request):
transport=events.append,
integrations=INTEGRATIONS,
auto_enabling_integrations=False,
with_locals=params.with_locals,
include_local_variables=params.include_local_variables,
)
)
yield events
Expand Down Expand Up @@ -260,7 +260,9 @@ def test_sentry_ignore_logger(sentry_events, level):
assert whitelisted_logger_event_dict.get("sentry") != "ignored"


@pytest.mark.parametrize("sentry_events", [{"with_locals": False}], indirect=True)
@pytest.mark.parametrize(
"sentry_events", [{"include_local_variables": False}], indirect=True
)
def test_sentry_json_respects_global_with_locals_option_no_locals(sentry_events):
processor = SentryProcessor()
try:
Expand All @@ -273,7 +275,9 @@ def test_sentry_json_respects_global_with_locals_option_no_locals(sentry_events)
assert "vars" not in frame # No local variables were captured


@pytest.mark.parametrize("sentry_events", [{"with_locals": True}], indirect=True)
@pytest.mark.parametrize(
"sentry_events", [{"include_local_variables": True}], indirect=True
)
def test_sentry_json_respects_global_with_locals_option_with_locals(sentry_events):
processor = SentryProcessor()
try:
Expand Down

0 comments on commit 2025df9

Please sign in to comment.