fix: Honor in_app in callstack (#348) · willcodefortea/sentry-python@d25173d · GitHub
Skip to content

Commit d25173d

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

6 files changed

Lines changed: 57 additions & 17 deletions

File tree

sentry_sdk/client.py

Lines changed: 11 additions & 7 deletions

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

0 commit comments

Comments
 (0)