Skip to content

Commit d25173d

Browse files
authored
fix: Honor in_app in callstack (getsentry#348)
1 parent 36eeba2 commit d25173d

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

sentry_sdk/client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ def _prepare_event(
116116
and "threads" not in event
117117
):
118118
with capture_internal_exceptions():
119-
event["threads"] = [
120-
{
121-
"stacktrace": current_stacktrace(self.options["with_locals"]),
122-
"crashed": False,
123-
"current": True,
124-
}
125-
]
119+
event["threads"] = {
120+
"values": [
121+
{
122+
"stacktrace": current_stacktrace(
123+
self.options["with_locals"]
124+
),
125+
"crashed": False,
126+
"current": True,
127+
}
128+
]
129+
}
126130

127131
for key in "release", "environment", "server_name", "dist":
128132
if event.get(key) is None and self.options[key] is not None: # type: ignore

sentry_sdk/integrations/logging.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,17 @@ def _emit(self, record):
170170
event = {}
171171
hint = None
172172
with capture_internal_exceptions():
173-
event["threads"] = [
174-
{
175-
"stacktrace": current_stacktrace(client_options["with_locals"]),
176-
"crashed": False,
177-
"current": True,
178-
}
179-
]
173+
event["threads"] = {
174+
"values": [
175+
{
176+
"stacktrace": current_stacktrace(
177+
client_options["with_locals"]
178+
),
179+
"crashed": False,
180+
"current": True,
181+
}
182+
]
183+
}
180184
else:
181185
event = {}
182186

sentry_sdk/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ def iter_event_stacktraces(event):
587587
# type: (Dict[str, Any]) -> Iterator[Dict[str, Any]]
588588
if "stacktrace" in event:
589589
yield event["stacktrace"]
590+
if "threads" in event:
591+
for thread in event["threads"].get("values") or ():
592+
if "stacktrace" in thread:
593+
yield thread["stacktrace"]
590594
if "exception" in event:
591595
for exception in event["exception"].get("values") or ():
592596
if "stacktrace" in exception:

tests/integrations/logging/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_logging_stack(sentry_init, capture_events):
6969
event_with, event_without, = events
7070

7171
assert event_with["level"] == "error"
72-
assert event_with["threads"][0]["stacktrace"]["frames"]
72+
assert event_with["threads"]["values"][0]["stacktrace"]["frames"]
7373

7474
assert event_without["level"] == "error"
7575
assert "threads" not in event_without

tests/test_client.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def bar():
147147
foo()
148148

149149
event, = events
150-
thread, = event["threads"]
150+
thread, = event["threads"]["values"]
151151
functions = [x["function"] for x in thread["stacktrace"]["frames"]]
152152
assert functions[-2:] == ["foo", "bar"]
153153

@@ -167,11 +167,26 @@ def bar():
167167
foo()
168168

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

174174

175+
def test_attach_stacktrace_in_app(sentry_init, capture_events):
176+
sentry_init(attach_stacktrace=True, in_app_exclude=["_pytest"])
177+
events = capture_events()
178+
179+
capture_message("hi")
180+
181+
event, = events
182+
thread, = event["threads"]["values"]
183+
frames = thread["stacktrace"]["frames"]
184+
pytest_frames = [f for f in frames if f["module"].startswith("_pytest")]
185+
assert pytest_frames
186+
assert all(f["in_app"] is False for f in pytest_frames)
187+
assert any(f["in_app"] for f in frames)
188+
189+
175190
def test_attach_stacktrace_disabled():
176191
events = []
177192
hub = Hub(Client(attach_stacktrace=False, transport=events.append))

tests/utils/test_general.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
strip_string,
1717
filename_for_module,
1818
handle_in_app_impl,
19+
iter_event_stacktraces,
1920
)
2021
from sentry_sdk._compat import text_type
2122

@@ -167,3 +168,15 @@ def test_in_app(empty):
167168
in_app_include=empty,
168169
in_app_exclude=["foo"],
169170
) == [{"module": "foo", "in_app": False}, {"module": "bar", "in_app": True}]
171+
172+
173+
def test_iter_stacktraces():
174+
assert set(
175+
iter_event_stacktraces(
176+
{
177+
"threads": {"values": [{"stacktrace": 1}]},
178+
"stacktrace": 2,
179+
"exception": {"values": [{"stacktrace": 3}]},
180+
}
181+
)
182+
) == {1, 2, 3}

0 commit comments

Comments
 (0)