Skip to content

Commit

Permalink
feat: use 'with_locals' setting from current sentry client
Browse files Browse the repository at this point in the history
  • Loading branch information
alxwrd authored and paveldedik committed Aug 25, 2022
1 parent c157b24 commit 56175f6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
7 changes: 6 additions & 1 deletion structlog_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ 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:
event, hint = event_from_exception(exc_info)
event, hint = event_from_exception(
exc_info,
client_options={
"with_locals": self._get_hub().client.options.get("with_locals")
},
)
else:
event, hint = {}, {}

Expand Down
50 changes: 49 additions & 1 deletion test/test_sentry_processor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass

import pytest
import sentry_sdk
Expand All @@ -10,15 +11,36 @@
]


@dataclass
class ClientParams:
with_locals: bool = True

@classmethod
def from_request(cls, request):
if not hasattr(request, "param"):
return cls()

if isinstance(request.param, dict):
return cls(**request.param)

if isinstance(request.param, cls):
return request.param

return cls()


@pytest.fixture
def sentry_events():
def sentry_events(request):
params = ClientParams.from_request(request)

events = []
with sentry_sdk.Hub() as hub:
hub.bind_client(
sentry_sdk.Client(
transport=events.append,
integrations=INTEGRATIONS,
auto_enabling_integrations=False,
with_locals=params.with_locals,
)
)
yield events
Expand Down Expand Up @@ -236,3 +258,29 @@ def test_sentry_ignore_logger(sentry_events, level):
assert_event_dict(event_data, sentry_events)
assert blacklisted_logger_event_dict.get("sentry") == "ignored"
assert whitelisted_logger_event_dict.get("sentry") != "ignored"


@pytest.mark.parametrize("sentry_events", [{"with_locals": False}], indirect=True)
def test_sentry_json_respects_global_with_locals_option_no_locals(sentry_events):
processor = SentryProcessor()
try:
1 / 0
except ZeroDivisionError:
processor(None, None, {"level": "error", "exc_info": True})

for event in sentry_events:
for frame in event["exception"]["values"][0]["stacktrace"]["frames"]:
assert "vars" not in frame # No local variables were captured


@pytest.mark.parametrize("sentry_events", [{"with_locals": True}], indirect=True)
def test_sentry_json_respects_global_with_locals_option_with_locals(sentry_events):
processor = SentryProcessor()
try:
1 / 0
except ZeroDivisionError:
processor(None, None, {"level": "error", "exc_info": True})

for event in sentry_events:
for frame in event["exception"]["values"][0]["stacktrace"]["frames"]:
assert "vars" in frame # Local variables were captured

0 comments on commit 56175f6

Please sign in to comment.