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(api): Fix tracing TypeError for static and class methods #2559

Merged
merged 18 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@
module_obj = import_module(module_name)
class_obj = getattr(module_obj, class_name)
function_obj = getattr(class_obj, function_name)
setattr(class_obj, function_name, trace(function_obj))
function_type = type(class_obj.__dict__[function_name])
traced_function = trace(function_obj)

if function_type in (staticmethod, classmethod):
traced_function = staticmethod(traced_function)

Check warning on line 205 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L205

Added line #L205 was not covered by tests

setattr(class_obj, function_name, traced_function)
setattr(module_obj, class_name, class_obj)
logger.debug("Enabled tracing for %s", function_qualname)

Expand Down
49 changes: 48 additions & 1 deletion tests/tracing/test_decorator_py3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import mock
from contextlib import contextmanager
import pytest
import sys

Expand All @@ -11,6 +12,16 @@
pytest.skip("Async decorator only works on Python 3.6+", allow_module_level=True)


class TestClass:
@staticmethod
def static(arg):
return arg

@classmethod
def class_(cls, arg):
return cls, arg


def my_example_function():
return "return_of_sync_function"

Expand All @@ -19,7 +30,8 @@ async def my_async_example_function():
return "return_of_async_function"


def test_trace_decorator_sync_py3():
@contextmanager
def patch_start_child():
fake_start_child = mock.MagicMock()
fake_transaction = mock.MagicMock()
fake_transaction.start_child = fake_start_child
Expand All @@ -28,6 +40,11 @@ def test_trace_decorator_sync_py3():
"sentry_sdk.tracing_utils_py3.get_current_span",
return_value=fake_transaction,
):
yield fake_start_child


def test_trace_decorator_sync_py3():
with patch_start_child() as fake_start_child:
result = my_example_function()
fake_start_child.assert_not_called()
assert result == "return_of_sync_function"
Expand Down Expand Up @@ -101,3 +118,33 @@ async def test_trace_decorator_async_py3_no_trx():
"test_decorator_py3.my_async_example_function",
)
assert result2 == "return_of_async_function"


def test_staticmethod_patching(sentry_init):
test_staticmethod_name = "test_decorator_py3.TestClass.static"
assert (
".".join([TestClass.static.__module__, TestClass.static.__qualname__])
== test_staticmethod_name
), "The test static method was moved or renamed. Please update the name accordingly"

sentry_init(functions_to_trace=[{"qualified_name": test_staticmethod_name}])

for instance_or_class in (TestClass, TestClass()):
with patch_start_child() as fake_start_child:
assert instance_or_class.static(1) == 1
fake_start_child.assert_called_once()


def test_classmethod_patching(sentry_init):
test_classmethod_name = "test_decorator_py3.TestClass.class_"
assert (
".".join([TestClass.class_.__module__, TestClass.class_.__qualname__])
== test_classmethod_name
), "The test class method was moved or renamed. Please update the name accordingly"

sentry_init(functions_to_trace=[{"qualified_name": test_classmethod_name}])

for instance_or_class in (TestClass, TestClass()):
with patch_start_child() as fake_start_child:
assert instance_or_class.class_(1) == (TestClass, 1)
fake_start_child.assert_called_once()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move these to test_basics since they are not specific to the test decorator and should also run on Python 2 (put them here originally when I thought I would fix the bug differently)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me either way