From b233b0eaf11a747fc2701cf66ee04fc0db8ccb4d Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Wed, 24 Apr 2019 20:00:00 +0200 Subject: [PATCH] fix: Honor in_app in callstack --- sentry_sdk/client.py | 18 +++++++++++------- sentry_sdk/integrations/logging.py | 18 +++++++++++------- sentry_sdk/utils.py | 4 ++++ tests/integrations/logging/test_logging.py | 2 +- tests/test_client.py | 19 +++++++++++++++++-- tests/utils/test_general.py | 13 +++++++++++++ 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index 8ecea608b3..15c869a55f 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -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 diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 60fba0dc74..bcbe86304b 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -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 = {} diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 94e14b200b..912dbdb4ab 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -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: diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 73e0f4823f..d368b447b1 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -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 diff --git a/tests/test_client.py b/tests/test_client.py index ec4b77e8ff..0e67fd5347 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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"] @@ -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)) diff --git a/tests/utils/test_general.py b/tests/utils/test_general.py index d3ed5c5a46..a21a755343 100644 --- a/tests/utils/test_general.py +++ b/tests/utils/test_general.py @@ -16,6 +16,7 @@ strip_string, filename_for_module, handle_in_app_impl, + iter_event_stacktraces, ) from sentry_sdk._compat import text_type @@ -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}