Skip to content

Commit af21b44

Browse files
committed
feat: Add reason for in_app to frames
Sentry enhances the in_app decision from the SDK, sometimes overriding it. In cases the decision to set in_app to True comes from the user themselves (by setting in_app_include), we don't want Sentry to override the decision. Currently there's no way for Sentry to know what led to the decision in the SDK. Changing that here.
1 parent 8d48961 commit af21b44

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,12 @@ def _should_be_included(
191191
# type: (...) -> bool
192192
# in_app_include takes precedence over in_app_exclude
193193
should_be_included = _module_in_list(namespace, in_app_include)
194-
should_be_excluded = _is_external_source(abs_path) or _module_in_list(
195-
namespace, in_app_exclude
194+
should_be_excluded = (
195+
_is_external_source(abs_path)
196+
or _module_in_list(namespace, in_app_exclude) is not None
196197
)
197198
return not is_sentry_sdk_frame and (
198-
should_be_included
199+
should_be_included is not None
199200
or (_is_in_project_root(abs_path, project_root) and not should_be_excluded)
200201
)
201202

sentry_sdk/utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,12 +1042,14 @@ def set_in_app_in_frames(frames, in_app_exclude, in_app_include, project_root=No
10421042
module = frame.get("module")
10431043

10441044
# check if module in frame is in the list of modules to include
1045-
if _module_in_list(module, in_app_include):
1045+
include_pattern_matched = _module_in_list(module, in_app_include)
1046+
if include_pattern_matched:
10461047
frame["in_app"] = True
1048+
frame["in_app_include_used"] = include_pattern_matched
10471049
continue
10481050

10491051
# check if module in frame is in the list of modules to exclude
1050-
if _module_in_list(module, in_app_exclude):
1052+
if _module_in_list(module, in_app_exclude) is not None:
10511053
frame["in_app"] = False
10521054
continue
10531055

@@ -1118,18 +1120,18 @@ def event_from_exception(
11181120

11191121

11201122
def _module_in_list(name, items):
1121-
# type: (Optional[str], Optional[List[str]]) -> bool
1123+
# type: (Optional[str], Optional[List[str]]) -> Optional[str]
11221124
if name is None:
1123-
return False
1125+
return None
11241126

11251127
if not items:
1126-
return False
1128+
return None
11271129

11281130
for item in items:
11291131
if item == name or name.startswith(item + "."):
1130-
return True
1132+
return item
11311133

1132-
return False
1134+
return None
11331135

11341136

11351137
def _is_external_source(abs_path):

tests/utils/test_general.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def test_parse_invalid_dsn(dsn):
239239
"module": "fastapi.routing",
240240
"abs_path": "/home/ubuntu/fastapi/.venv/lib/python3.10/site-packages/fastapi/routing.py",
241241
"in_app": True,
242+
"in_app_include_used": "fastapi",
242243
},
243244
],
244245
[
@@ -280,6 +281,7 @@ def test_parse_invalid_dsn(dsn):
280281
"module": "fastapi.routing",
281282
"abs_path": "/usr/lib/python2.7/dist-packages/fastapi/routing.py",
282283
"in_app": True,
284+
"in_app_include_used": "fastapi",
283285
},
284286
],
285287
[
@@ -420,6 +422,7 @@ def test_parse_invalid_dsn(dsn):
420422
{
421423
"module": "fastapi.routing",
422424
"in_app": True,
425+
"in_app_include_used": "fastapi",
423426
},
424427
],
425428
[
@@ -461,6 +464,7 @@ def test_parse_invalid_dsn(dsn):
461464
"module": "main",
462465
"abs_path": "/home/ubuntu/fastapi/main.py",
463466
"in_app": True,
467+
"in_app_include_used": "main",
464468
},
465469
],
466470
[

0 commit comments

Comments
 (0)