Skip to content

Commit

Permalink
fix(tracing): Fix add_query_source with modules outside of project …
Browse files Browse the repository at this point in the history
…root

Fix: getsentry#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:
getsentry#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.
  • Loading branch information
rominf committed Jul 19, 2024
1 parent b1b3eab commit 2d74969
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
is_sentry_url,
_is_external_source,
_module_in_list,
_is_in_project_root,
)
from sentry_sdk._types import TYPE_CHECKING

Expand Down Expand Up @@ -218,20 +219,14 @@ def add_query_source(span):
is_sentry_sdk_frame = namespace is not None and namespace.startswith(
"sentry_sdk."
)
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
)

should_be_included = not _is_external_source(abs_path)
if namespace is not None:
if in_app_exclude and _module_in_list(namespace, in_app_exclude):
should_be_included = False
if in_app_include and _module_in_list(namespace, in_app_include):
# in_app_include takes precedence over in_app_exclude, so doing it
# at the end
should_be_included = True

if (
abs_path.startswith(project_root)
and should_be_included
and not is_sentry_sdk_frame
if not is_sentry_sdk_frame and (
should_be_included
or (_is_in_project_root(abs_path, project_root) and not should_be_excluded)
):
break

Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ def event_from_exception(


def _module_in_list(name, items):
# type: (str, Optional[List[str]]) -> bool
# type: (Optional[str], Optional[List[str]]) -> bool
if name is None:
return False

Expand Down

0 comments on commit 2d74969

Please sign in to comment.