Skip to content

Respect with_locals setting #74

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

Merged
merged 1 commit into from
Aug 25, 2022
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
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