Skip to content

Commit d8702c8

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 da164da commit d8702c8

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
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: 1 addition & 1 deletion
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)