Skip to content

Commit 6b68fd1

Browse files
committed
fix(tracing): Fix add_query_source with modules outside of project root
Fix: #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: #1894 (comment). Note that the `_module_in_list` function returns `False` if `name` is `None` or `items` are falsy, hence extra check before function call can be omitted to simplify code.
1 parent 113d12d commit 6b68fd1

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 8 additions & 14 deletions
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,21 +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 is not None
233-
and abs_path.startswith(project_root)
234-
and should_be_included
235-
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)
236230
):
237231
break
238232

sentry_sdk/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def event_from_exception(
10471047

10481048

10491049
def _module_in_list(name, items):
1050-
# type: (str, Optional[List[str]]) -> bool
1050+
# type: (Optional[str], Optional[List[str]]) -> bool
10511051
if name is None:
10521052
return False
10531053

@@ -1074,8 +1074,8 @@ def _is_external_source(abs_path):
10741074

10751075

10761076
def _is_in_project_root(abs_path, project_root):
1077-
# type: (str, Optional[str]) -> bool
1078-
if project_root is None:
1077+
# type: (Optional[str], Optional[str]) -> bool
1078+
if abs_path is None or project_root is None:
10791079
return False
10801080

10811081
# check if path is in the project root

0 commit comments

Comments
 (0)