Skip to content

Commit 1c886e6

Browse files
authored
fix(profiling): Resolve inherited method class names (#1756)
Methods may be inherited from a parent class. If multiple classes inherit from the same class and uses the inherited method, we'd want it to report the parent class's name instead of the individual child classes since they'd have the same filename and lineno of the parent class and not the children.
1 parent ec43e1d commit 1c886e6

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

sentry_sdk/profiler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def get_frame_name(frame):
211211
and f_code.co_varnames[0] == "self"
212212
and "self" in frame.f_locals
213213
):
214-
return "{}.{}".format(frame.f_locals["self"].__class__.__name__, name)
214+
for cls in frame.f_locals["self"].__class__.__mro__:
215+
if name in cls.__dict__:
216+
return "{}.{}".format(cls.__name__, name)
215217
except AttributeError:
216218
pass
217219

@@ -225,7 +227,9 @@ def get_frame_name(frame):
225227
and f_code.co_varnames[0] == "cls"
226228
and "cls" in frame.f_locals
227229
):
228-
return "{}.{}".format(frame.f_locals["cls"].__name__, name)
230+
for cls in frame.f_locals["cls"].__mro__:
231+
if name in cls.__dict__:
232+
return "{}.{}".format(cls.__name__, name)
229233
except AttributeError:
230234
pass
231235

tests/test_profiler.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,35 @@ def get_frame(depth=1):
8282
return inspect.currentframe()
8383

8484

85-
class GetFrame:
85+
class GetFrameBase:
86+
def inherited_instance_method(self):
87+
return inspect.currentframe()
88+
89+
def inherited_instance_method_wrapped(self):
90+
def wrapped():
91+
self
92+
return inspect.currentframe()
93+
94+
return wrapped
95+
96+
@classmethod
97+
def inherited_class_method(cls):
98+
return inspect.currentframe()
99+
100+
@classmethod
101+
def inherited_class_method_wrapped(cls):
102+
def wrapped():
103+
cls
104+
return inspect.currentframe()
105+
106+
return wrapped
107+
108+
@staticmethod
109+
def inherited_static_method():
110+
return inspect.currentframe()
111+
112+
113+
class GetFrame(GetFrameBase):
86114
def instance_method(self):
87115
return inspect.currentframe()
88116

@@ -149,6 +177,32 @@ def static_method():
149177
id="static_method",
150178
marks=pytest.mark.skip(reason="unsupported"),
151179
),
180+
pytest.param(
181+
GetFrame().inherited_instance_method(),
182+
"GetFrameBase.inherited_instance_method",
183+
id="inherited_instance_method",
184+
),
185+
pytest.param(
186+
GetFrame().inherited_instance_method_wrapped()(),
187+
"wrapped",
188+
id="instance_method_wrapped",
189+
),
190+
pytest.param(
191+
GetFrame().inherited_class_method(),
192+
"GetFrameBase.inherited_class_method",
193+
id="inherited_class_method",
194+
),
195+
pytest.param(
196+
GetFrame().inherited_class_method_wrapped()(),
197+
"wrapped",
198+
id="inherited_class_method_wrapped",
199+
),
200+
pytest.param(
201+
GetFrame().inherited_static_method(),
202+
"GetFrameBase.static_method",
203+
id="inherited_static_method",
204+
marks=pytest.mark.skip(reason="unsupported"),
205+
),
152206
],
153207
)
154208
def test_get_frame_name(frame, frame_name):

0 commit comments

Comments
 (0)