Skip to content

Commit

Permalink
chore(tracing): Refactor tracing_utils.py
Browse files Browse the repository at this point in the history
Preparation for:
getsentry#3313
Proposed in:
getsentry#3313 (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 Aug 20, 2024
1 parent 269d96d commit db40dfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 18 additions & 16 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 @@ -215,16 +221,15 @@ def add_query_source(span):
)

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 _module_in_list(namespace, in_app_exclude):
should_be_included = False
if _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)
_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

0 comments on commit db40dfc

Please sign in to comment.