Skip to content

Commit

Permalink
fix(profiler): Add function name to profiler frame cache (#2164)
Browse files Browse the repository at this point in the history
Wrapper functions can take on the same name as the wrapped function. This means
that if a decorator is used to wrap different functions, even though the
filename and line number will be the same for all instances of the frame, the
function name can vary. Add the function name to the cache to avoid these cache
collisions.
  • Loading branch information
Zylphrex authored Jun 13, 2023
1 parent d991be7 commit a4378de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 9 additions & 8 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
FrameId = Tuple[
str, # abs_path
int, # lineno
str, # function
]
FrameIds = Tuple[FrameId, ...]

Expand Down Expand Up @@ -278,7 +279,7 @@ def extract_stack(
for i, fid in enumerate(frame_ids):
frame = cache.get(fid)
if frame is None:
frame = extract_frame(raw_frames[i], cwd)
frame = extract_frame(fid, raw_frames[i], cwd)
cache.set(fid, frame)
frames.append(frame)

Expand All @@ -300,15 +301,15 @@ def extract_stack(

def frame_id(raw_frame):
# type: (FrameType) -> FrameId
return (raw_frame.f_code.co_filename, raw_frame.f_lineno)
return (raw_frame.f_code.co_filename, raw_frame.f_lineno, get_frame_name(raw_frame))


def extract_frame(frame, cwd):
# type: (FrameType, str) -> ProcessedFrame
abs_path = frame.f_code.co_filename
def extract_frame(fid, raw_frame, cwd):
# type: (FrameId, FrameType, str) -> ProcessedFrame
abs_path = raw_frame.f_code.co_filename

try:
module = frame.f_globals["__name__"]
module = raw_frame.f_globals["__name__"]
except Exception:
module = None

Expand All @@ -327,8 +328,8 @@ def extract_frame(frame, cwd):
"abs_path": os.path.join(cwd, abs_path),
"module": module,
"filename": filename_for_module(module, abs_path) or None,
"function": get_frame_name(frame),
"lineno": frame.f_lineno,
"function": fid[2],
"lineno": raw_frame.f_lineno,
}


Expand Down
3 changes: 2 additions & 1 deletion tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ThreadScheduler,
extract_frame,
extract_stack,
frame_id,
get_current_thread_id,
get_frame_name,
setup_profiler,
Expand Down Expand Up @@ -444,7 +445,7 @@ def test_get_frame_name(frame, frame_name):
def test_extract_frame(get_frame, function):
cwd = os.getcwd()
frame = get_frame()
extracted_frame = extract_frame(frame, cwd)
extracted_frame = extract_frame(frame_id(frame), frame, cwd)

# the abs_path should be equal toe the normalized path of the co_filename
assert extracted_frame["abs_path"] == os.path.normpath(frame.f_code.co_filename)
Expand Down

0 comments on commit a4378de

Please sign in to comment.