Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tracing): Refactor tracing_utils.py #3452

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
to_string,
is_sentry_url,
_is_external_source,
_is_in_project_root,
_module_in_list,
)
from sentry_sdk._types import TYPE_CHECKING
Expand Down Expand Up @@ -170,6 +171,14 @@ def maybe_create_breadcrumbs_from_span(scope, span):
)


def _get_frame_module_abs_path(frame):
# type: (FrameType) -> Optional[str]
try:
return frame.f_code.co_filename
except Exception:
return None


def add_query_source(span):
# type: (sentry_sdk.tracing.Span) -> None
"""
Expand Down Expand Up @@ -200,10 +209,7 @@ def add_query_source(span):
# Find the correct frame
frame = sys._getframe() # type: Union[FrameType, None]
while frame is not None:
try:
abs_path = frame.f_code.co_filename
except Exception:
abs_path = ""
abs_path = _get_frame_module_abs_path(frame)

try:
namespace = frame.f_globals.get("__name__") # type: Optional[str]
Expand All @@ -214,17 +220,16 @@ def add_query_source(span):
"sentry_sdk."
)

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
# 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)

if (
abs_path.startswith(project_root)
_is_in_project_root(abs_path, project_root)
and should_be_included
and not is_sentry_sdk_frame
):
Expand All @@ -250,10 +255,7 @@ def add_query_source(span):
if namespace is not None:
span.set_data(SPANDATA.CODE_NAMESPACE, namespace)

try:
filepath = frame.f_code.co_filename
except Exception:
filepath = None
filepath = _get_frame_module_abs_path(frame)
if filepath is not None:
if namespace is not None:
in_app_path = filename_for_module(namespace, filepath)
Expand Down
11 changes: 7 additions & 4 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,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 All @@ -1091,17 +1091,20 @@ def _module_in_list(name, items):


def _is_external_source(abs_path):
# type: (str) -> bool
# type: (Optional[str]) -> bool
# check if frame is in 'site-packages' or 'dist-packages'
if abs_path is None:
return False

external_source = (
re.search(r"[\\/](?:dist|site)-packages[\\/]", abs_path) is not None
)
return external_source


def _is_in_project_root(abs_path, project_root):
# type: (str, Optional[str]) -> bool
if project_root is None:
# type: (Optional[str], Optional[str]) -> bool
if abs_path is None or project_root is None:
return False

# check if path is in the project root
Expand Down
Loading