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

fix(profiling): get_frame_name only look at arguments #1684

Merged
merged 2 commits into from
Oct 20, 2022
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
19 changes: 16 additions & 3 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,35 @@ def get_frame_name(frame):
# in 3.11+, there is a frame.f_code.co_qualname that
# we should consider using instead where possible

f_code = frame.f_code
# co_name only contains the frame name. If the frame was a method,
# the class name will NOT be included.
name = frame.f_code.co_name
name = f_code.co_name

# if it was a method, we can get the class name by inspecting
# the f_locals for the `self` argument
try:
if "self" in frame.f_locals:
if (
# the co_varnames start with the frame's positional arguments
# and we expect the first to be `self` if its an instance method
f_code.co_varnames
and f_code.co_varnames[0] == "self"
and "self" in frame.f_locals
):
return "{}.{}".format(frame.f_locals["self"].__class__.__name__, name)
except AttributeError:
pass

# if it was a class method, (decorated with `@classmethod`)
# we can get the class name by inspecting the f_locals for the `cls` argument
try:
if "cls" in frame.f_locals:
if (
# the co_varnames start with the frame's positional arguments
# and we expect the first to be `cls` if its a class method
f_code.co_varnames
and f_code.co_varnames[0] == "cls"
and "cls" in frame.f_locals
):
return "{}.{}".format(frame.f_locals["cls"].__name__, name)
except AttributeError:
pass
Expand Down
25 changes: 25 additions & 0 deletions tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,25 @@ class GetFrame:
def instance_method(self):
return inspect.currentframe()

def instance_method_wrapped(self):
def wrapped():
self
return inspect.currentframe()

return wrapped

@classmethod
def class_method(cls):
return inspect.currentframe()

@classmethod
def class_method_wrapped(cls):
def wrapped():
cls
return inspect.currentframe()

return wrapped

@staticmethod
def static_method():
return inspect.currentframe()
Expand All @@ -112,11 +127,21 @@ def static_method():
"GetFrame.instance_method",
id="instance_method",
),
pytest.param(
GetFrame().instance_method_wrapped()(),
"wrapped",
id="instance_method_wrapped",
),
pytest.param(
GetFrame().class_method(),
"GetFrame.class_method",
id="class_method",
),
pytest.param(
GetFrame().class_method_wrapped()(),
"wrapped",
id="class_method_wrapped",
),
pytest.param(
GetFrame().static_method(),
"GetFrame.static_method",
Expand Down