Skip to content

Commit a1b7ce5

Browse files
chore(tracing): Refactor tracing_utils.py (#3452)
* chore(tracing): Refactor `tracing_utils.py` Preparation for: #3313 Proposed in: #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. * ref: Further simplify `should_be_included` logic --------- Co-authored-by: Daniel Szoke <[email protected]>
1 parent 269d96d commit a1b7ce5

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
to_string,
2222
is_sentry_url,
2323
_is_external_source,
24+
_is_in_project_root,
2425
_module_in_list,
2526
)
2627
from sentry_sdk._types import TYPE_CHECKING
@@ -170,6 +171,14 @@ def maybe_create_breadcrumbs_from_span(scope, span):
170171
)
171172

172173

174+
def _get_frame_module_abs_path(frame):
175+
# type: (FrameType) -> Optional[str]
176+
try:
177+
return frame.f_code.co_filename
178+
except Exception:
179+
return None
180+
181+
173182
def add_query_source(span):
174183
# type: (sentry_sdk.tracing.Span) -> None
175184
"""
@@ -200,10 +209,7 @@ def add_query_source(span):
200209
# Find the correct frame
201210
frame = sys._getframe() # type: Union[FrameType, None]
202211
while frame is not None:
203-
try:
204-
abs_path = frame.f_code.co_filename
205-
except Exception:
206-
abs_path = ""
212+
abs_path = _get_frame_module_abs_path(frame)
207213

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

217-
should_be_included = not _is_external_source(abs_path)
218-
if namespace is not None:
219-
if in_app_exclude and _module_in_list(namespace, in_app_exclude):
220-
should_be_included = False
221-
if in_app_include and _module_in_list(namespace, in_app_include):
222-
# in_app_include takes precedence over in_app_exclude, so doing it
223-
# at the end
224-
should_be_included = True
223+
# in_app_include takes precedence over in_app_exclude
224+
should_be_included = (
225+
not (
226+
_is_external_source(abs_path)
227+
or _module_in_list(namespace, in_app_exclude)
228+
)
229+
) or _module_in_list(namespace, in_app_include)
225230

226231
if (
227-
abs_path.startswith(project_root)
232+
_is_in_project_root(abs_path, project_root)
228233
and should_be_included
229234
and not is_sentry_sdk_frame
230235
):
@@ -250,10 +255,7 @@ def add_query_source(span):
250255
if namespace is not None:
251256
span.set_data(SPANDATA.CODE_NAMESPACE, namespace)
252257

253-
try:
254-
filepath = frame.f_code.co_filename
255-
except Exception:
256-
filepath = None
258+
filepath = _get_frame_module_abs_path(frame)
257259
if filepath is not None:
258260
if namespace is not None:
259261
in_app_path = filename_for_module(namespace, filepath)

sentry_sdk/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ def event_from_exception(
10761076

10771077

10781078
def _module_in_list(name, items):
1079-
# type: (str, Optional[List[str]]) -> bool
1079+
# type: (Optional[str], Optional[List[str]]) -> bool
10801080
if name is None:
10811081
return False
10821082

@@ -1091,17 +1091,20 @@ def _module_in_list(name, items):
10911091

10921092

10931093
def _is_external_source(abs_path):
1094-
# type: (str) -> bool
1094+
# type: (Optional[str]) -> bool
10951095
# check if frame is in 'site-packages' or 'dist-packages'
1096+
if abs_path is None:
1097+
return False
1098+
10961099
external_source = (
10971100
re.search(r"[\\/](?:dist|site)-packages[\\/]", abs_path) is not None
10981101
)
10991102
return external_source
11001103

11011104

11021105
def _is_in_project_root(abs_path, project_root):
1103-
# type: (str, Optional[str]) -> bool
1104-
if project_root is None:
1106+
# type: (Optional[str], Optional[str]) -> bool
1107+
if abs_path is None or project_root is None:
11051108
return False
11061109

11071110
# check if path is in the project root

0 commit comments

Comments
 (0)