8000 fix: in_app_exclude does not work solo (#322) · pythonthings/sentry-python@0ae2c1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ae2c1c

Browse files
authored
fix: in_app_exclude does not work solo (getsentry#322)
* fix: in_app_exclude does not work solo * fix: Lint
1 parent ca2d527 commit 0ae2c1c

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

sentry_sdk/utils.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -579,24 +579,41 @@ def to_string(value):
579579
return repr(value)[1:-1]
580580

581581

582-
def iter_event_frames(event):
582+
def iter_event_stacktraces(event):
583583
# type: (Dict[str, Any]) -> Iterator[Dict[str, Any]]
584-
stacktraces = []
585584
if "stacktrace" in event:
586-
stacktraces.append(event["stacktrace"])
585+
yield event["stacktrace"]
587586
if "exception" in event:
588587
for exception in event["exception"].get("values") or ():
589588
if "stacktrace" in exception:
590-
stacktraces.append(exception["stacktrace"])
591-
for stacktrace in stacktraces:
589+
yield exception["stacktrace"]
590+
591+
592+
def iter_event_frames(event):
593+
# type: (Dict[str, Any]) -> Iterator[Dict[str, Any]]
594+
for stacktrace in iter_event_stacktraces(event):
592595
for frame in stacktrace.get("frames") or ():
593596
yield frame
594597

595598

596599
def handle_in_app(event, in_app_exclude=None, in_app_include=None):
597600
# type: (Dict[str, Any], List, List) -> Dict[str, Any]
601+
for stacktrace in iter_event_stacktraces(event):
602+
handle_in_app_impl(
603+
stacktrace.get("frames"),
604+
in_app_exclude=in_app_exclude,
605+
in_app_include=in_app_include,
606+
)
607+
608+
return event
609+
610+
611+
def handle_in_app_impl(frames, in_app_exclude, in_app_include):
612+
if not frames:
613+
return
614+
598615
any_in_app = False
599-
for frame in iter_event_frames(event):
616+
for frame in frames:
600617
in_app = frame.get("in_app")
601618
if in_app is not None:
602619
if in_app:
@@ -606,18 +623,18 @@ def handle_in_app(event, in_app_exclude=None, in_app_include=None):
606623
module = frame.get("module")
607624
if not module:
608625
continue
609-
610-
if _module_in_set(module, in_app_exclude):
611-
frame["in_app"] = False
612-
if _module_in_set(module, in_app_include):
626+
elif _module_in_set(module, in_app_include):
613627
frame["in_app"] = True
614628
any_in_app = True
629+
elif _module_in_set(module, in_app_exclude):
630+
frame["in_app"] = False
615631

616632
if not any_in_app:
617-
for frame in iter_event_frames(event):
618-
frame["in_app"] = True
633+
for frame in frames:
634+
if frame.get("in_app") is None:
635+
frame["in_app"] = True
619636

620-
return event
637+
return frames
621638

622639

623640
def exc_info_from_error(error):

tests/utils/test_general.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
format_and_strip,
1616
strip_string,
1717
filename_for_module,
18+
handle_in_app_impl,
1819
)
1920
from sentry_sdk._compat import text_type
2021

@@ -145,3 +146,24 @@ def test_parse_dsn_paths(given, expected):
145146
def test_parse_invalid_dsn(dsn):
146147
with pytest.raises(BadDsn):
147148
dsn = Dsn(dsn)
149+
150+
151+
@pytest.mark.parametrize("empty", [None, []])
152+
def test_in_app(empty):
153+
assert handle_in_app_impl(
154+
[{"module": "foo"}, {"module": "bar"}],
155+
in_app_include=["foo"],
156+
in_app_exclude=empty,
157+
) == [{"module": "foo", "in_app": True}, {"module": "bar"}]
158+
159+
assert handle_in_app_impl(
160+
[{"module": "foo"}, {"module": "bar"}],
161+
in_app_include=["foo"],
162+
in_app_exclude=["foo"],
163+
) == [{"module": "foo", "in_app": True}, {"module": "bar"}]
164+
165+
assert handle_in_app_impl(
166+
[{"module": "foo"}, {"module": "bar"}],
167+
in_app_include=empty,
168+
in_app_exclude=["foo"],
169+
) == [{"module": "foo", "in_app": False}, {"module": "bar", "in_app": True}]

0 commit comments

Comments
 (0)
0