Skip to content

Commit 2568b9c

Browse files
committed
fix(tracing): fix logic for setting in_app flag
Previously, in case packages added in `in_app_include` were installed into a location outside of the project root directory, stack frames from those packages were not marked as `in_app`. Cases include running Python from virtualenv, created outside of the project root directory, or Python packages installed into the system using package managers. This resulted in inconsistency: traces from the same project would have different `in_app` flags depending on the deployment method. Steps to reproduce (virtualenv outside of project root): ``` $ docker run --replace --rm --name sentry-postgres -e POSTGRES_USER=sentry -e POSTGRES_PASSWORD=sentry -d -p 5432:5432 postgres $ distrobox create -i ubuntu:24.04 -n sentry-test-in_app_include-venv $ distrobox enter sentry-test-in_app_include-venv $ python3 -m venv /tmp/.venv-test-in_app_include $ source /tmp/.venv-test-in_app_include/bin/activate $ pytest tests/integrations/django/test_db_query_data.py::test_query_source_with_in_app_include # FAIL ``` Steps to reproduce (system packages): ``` $ docker run --replace --rm --name sentry-postgres -e POSTGRES_USER=sentry -e POSTGRES_PASSWORD=sentry -d -p 5432:5432 postgres $ distrobox create -i ubuntu:24.04 -n sentry-test-in_app_include-os $ distrobox enter sentry-test-in_app_include-os $ sudo apt install python3-django python3-pytest python3-pytest-cov python3-pytest-django python3-jsonschema python3-urllib3 python3-certifi python3-werkzeug python3-psycopg2 $ pytest tests/integrations/django/test_db_query_data.py::test_query_source_with_in_app_include # FAIL ``` In this change, the logic was slightly changed to avoid these discrepancies and conform to the requirements, described in the PR with `in_app` flag introduction: getsentry#1894 (comment). Note that the `_module_in_list` function returns `False` if `name` is `None`, hence extra check before function call can be omitted to simplify code.
1 parent e0a9b5f commit 2568b9c

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

sentry_sdk/tracing_utils.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
is_sentry_url,
2323
_is_external_source,
2424
_module_in_list,
25+
_is_in_project_root,
2526
)
2627
from sentry_sdk._types import TYPE_CHECKING
2728

@@ -218,20 +219,14 @@ def add_query_source(span):
218219
is_sentry_sdk_frame = namespace is not None and namespace.startswith(
219220
"sentry_sdk."
220221
)
222+
should_be_included = _module_in_list(namespace, in_app_include)
223+
should_be_excluded = _is_external_source(abs_path) or _module_in_list(
224+
namespace, in_app_exclude
225+
)
221226

222-
should_be_included = not _is_external_source(abs_path)
223-
if namespace is not None:
224-
if in_app_exclude and _module_in_list(namespace, in_app_exclude):
225-
should_be_included = False
226-
if in_app_include and _module_in_list(namespace, in_app_include):
227-
# in_app_include takes precedence over in_app_exclude, so doing it
228-
# at the end
229-
should_be_included = True
230-
231-
if (
232-
abs_path.startswith(project_root)
233-
and should_be_included
234-
and not is_sentry_sdk_frame
227+
if not is_sentry_sdk_frame and (
228+
should_be_included
229+
or (_is_in_project_root(abs_path, project_root) and not should_be_excluded)
235230
):
236231
break
237232

sentry_sdk/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ def event_from_exception(
10431043

10441044

10451045
def _module_in_list(name, items):
1046-
# type: (str, Optional[List[str]]) -> bool
1046+
# type: (Optional[str], Optional[List[str]]) -> bool
10471047
if name is None:
10481048
return False
10491049

0 commit comments

Comments
 (0)