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: Honor in_app in callstack #348

Merged
merged 2 commits into from
Apr 25, 2019
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
18 changes: 11 additions & 7 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ def _prepare_event(
and "threads" not in event
):
with capture_internal_exceptions():
event["threads"] = [
{
"stacktrace": current_stacktrace(self.options["with_locals"]),
"crashed": False,
"current": True,
}
]
event["threads"] = {
"values": [
{
"stacktrace": current_stacktrace(
self.options["with_locals"]
),
"crashed": False,
"current": True,
}
]
}

for key in "release", "environment", "server_name", "dist":
if event.get(key) is None and self.options[key] is not None: # type: ignore
Expand Down
18 changes: 11 additions & 7 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,17 @@ def _emit(self, record):
event = {}
hint = None
with capture_internal_exceptions():
event["threads"] = [
{
"stacktrace": current_stacktrace(client_options["with_locals"]),
"crashed": False,
"current": True,
}
]
event["threads"] = {
"values": [
{
"stacktrace": current_stacktrace(
client_options["with_locals"]
),
"crashed": False,
"current": True,
}
]
}
else:
event = {}

Expand Down
4 changes: 4 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ def iter_event_stacktraces(event):
# type: (Dict[str, Any]) -> Iterator[Dict[str, Any]]
if "stacktrace" in event:
yield event["stacktrace"]
if "threads" in event:
for thread in event["threads"].get("values") or ():
if "stacktrace" in thread:
yield thread["stacktrace"]
if "exception" in event:
for exception in event["exception"].get("values") or ():
if "stacktrace" in exception:
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/logging/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_logging_stack(sentry_init, capture_events):
event_with, event_without, = events

assert event_with["level"] == "error"
assert event_with["threads"][0]["stacktrace"]["frames"]
assert event_with["threads"]["values"][0]["stacktrace"]["frames"]

assert event_without["level"] == "error"
assert "threads" not in event_without
19 changes: 17 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def bar():
foo()

event, = events
thread, = event["threads"]
thread, = event["threads"]["values"]
functions = [x["function"] for x in thread["stacktrace"]["frames"]]
assert functions[-2:] == ["foo", "bar"]

Expand All @@ -167,11 +167,26 @@ def bar():
foo()

event, = events
thread, = event["threads"]
thread, = event["threads"]["values"]
local_vars = [x.get("vars") for x in thread["stacktrace"]["frames"]]
assert local_vars[-2:] == [None, None]


def test_attach_stacktrace_in_app(sentry_init, capture_events):
sentry_init(attach_stacktrace=True, in_app_exclude=["_pytest"])
events = capture_events()

capture_message("hi")

event, = events
thread, = event["threads"]["values"]
frames = thread["stacktrace"]["frames"]
pytest_frames = [f for f in frames if f["module"].startswith("_pytest")]
assert pytest_frames
assert all(f["in_app"] is False for f in pytest_frames)
assert any(f["in_app"] for f in frames)


def test_attach_stacktrace_disabled():
events = []
hub = Hub(Client(attach_stacktrace=False, transport=events.append))
Expand Down
13 changes: 13 additions & 0 deletions tests/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
strip_string,
filename_for_module,
handle_in_app_impl,
iter_event_stacktraces,
)
from sentry_sdk._compat import text_type

Expand Down Expand Up @@ -167,3 +168,15 @@ def test_in_app(empty):
in_app_include=empty,
in_app_exclude=["foo"],
) == [{"module": "foo", "in_app": False}, {"module": "bar", "in_app": True}]


def test_iter_stacktraces():
assert set(
iter_event_stacktraces(
{
"threads": {"values": [{"stacktrace": 1}]},
"stacktrace": 2,
"exception": {"values": [{"stacktrace": 3}]},
}
)
) == {1, 2, 3}