Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/in-app-project-root-boundary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

`posthog.exception_utils`: only mark frames as `in_app` when their path is inside the project root directory, so sibling directories sharing the root's name as a prefix (e.g. `/work/app-shared` for root `/work/app`) are no longer treated as application code.
9 changes: 4 additions & 5 deletions posthog/exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,10 @@ def _is_in_project_root(abs_path, project_root):
if project_root is None:
return False

# check if path is in the project root
if abs_path.startswith(project_root):
return True

return False
# check if path is in the project root, matching whole directory names so
# that sibling directories sharing a prefix (e.g. /app-shared) don't match
root = project_root.rstrip(os.sep)
return abs_path == root or abs_path.startswith(root + os.sep)


def _truncate_by_bytes(string, max_bytes):
Expand Down
19 changes: 19 additions & 0 deletions posthog/test/test_exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ def test_frame_classification_preserves_overrides_and_module_boundaries(
assert frame.get("in_app") is expected


@pytest.mark.parametrize(
"project_root, abs_path, expected",
[
("/work/app", "/work/app/lib.py", True),
("/work/app", "/work/app/pkg/mod.py", True),
("/work/app/", "/work/app/lib.py", True),
("/work/app", "/work/app-shared/lib.py", None),
("/work/app", "/work/application/x.py", None),
("/work/app/", "/work/app-shared/lib.py", None),
],
)
def test_project_root_match_respects_directory_boundaries(
project_root, abs_path, expected
):
frame = {"abs_path": abs_path}
exception_utils.set_in_app_in_frames([frame], None, None, project_root)
assert frame.get("in_app") is expected


def test_frame_classification_accepts_missing_options_and_empty_frames():
frame = {"module": "app", "abs_path": "/project/app.py"}
exception_utils.set_in_app_in_frames([frame], None, None)
Expand Down
Loading