Skip to content

fix(profiler): Add function name to profiler frame cache #2164

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

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