From 6fa39dbfffaac892bf42e17fd5473cff1d61857f Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Thu, 18 Jul 2024 16:57:21 +0300 Subject: [PATCH] fix(tracing): Fix `add_query_source` with modules outside of project root Fix: https://github.com/getsentry/sentry-python/issues/3312 Previously, when packages added in `in_app_include` were installed to a location outside of the project root directory, span from those packages were not extended with OTel compatible source code information. 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 an inconsistency: spans from the same project would be different, depending on the deployment method. In this change, the logic was slightly changed to avoid these discrepancies and conform to the requirements, described in the PR with better setting of in-app in stack frames: https://github.com/getsentry/sentry-python/pull/1894#issue-1579192436. --- sentry_sdk/tracing_utils.py | 16 +++++++--------- tests/test_tracing_utils.py | 5 +---- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 27f11ff226..562b1a961d 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -188,15 +188,13 @@ def _should_be_included( project_root: Optional[str], ) -> bool: # in_app_include takes precedence over in_app_exclude - should_be_included = ( - not ( - _is_external_source(abs_path) or _module_in_list(namespace, in_app_exclude) - ) - ) or _module_in_list(namespace, in_app_include) - return ( - _is_in_project_root(abs_path, project_root) - and should_be_included - and not is_sentry_sdk_frame + should_be_included = _module_in_list(namespace, in_app_include) + should_be_excluded = _is_external_source(abs_path) or _module_in_list( + namespace, in_app_exclude + ) + return not is_sentry_sdk_frame and ( + should_be_included + or (_is_in_project_root(abs_path, project_root) and not should_be_excluded) ) diff --git a/tests/test_tracing_utils.py b/tests/test_tracing_utils.py index c31d4dcf5f..6e15a4db66 100644 --- a/tests/test_tracing_utils.py +++ b/tests/test_tracing_utils.py @@ -73,7 +73,7 @@ class ShouldBeIncludedTestCase: ), False, ), - pytest.param( + ( ShouldBeIncludedTestCase( id="Frame from system-wide installed Django with `django` in `in_app_include`", is_sentry_sdk_frame=False, @@ -83,9 +83,6 @@ class ShouldBeIncludedTestCase: in_app_include=["django"], ), True, - marks=pytest.mark.xfail( - reason="Bug, see https://github.com/getsentry/sentry-python/issues/3312" - ), ), ], ids=id_function,