Skip to content
Open
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
11 changes: 6 additions & 5 deletions src/a2a/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,12 @@ def decorator(cls: Any) -> Any:
continue

span_name = f'{cls.__module__}.{cls.__name__}.{name}'
setattr(
cls,
name,
trace_function(span_name=span_name, kind=kind)(method),
)
traced = trace_function(span_name=span_name, kind=kind)(method)
# `getmembers` unwraps staticmethods into plain functions. Setting
# the wrapper back as-is would turn them into instance methods.
if isinstance(inspect.getattr_static(cls, name), staticmethod):
traced = staticmethod(traced)
setattr(cls, name, traced)
return cls

return decorator
31 changes: 31 additions & 0 deletions tests/utils/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,37 @@ def foo(self) -> str:
assert hasattr(obj, 'x')


def test_trace_class_keeps_staticmethod_static(
mock_tracer: mock.MagicMock,
) -> None:
class Base:
@staticmethod
def inherited(x: int) -> int:
return x + 1

@trace_class()
class MyClass(Base):
@staticmethod
def double(x: int) -> int:
return x * 2

def run(self) -> int:
return self.double(21)

obj = MyClass()
assert obj.run() == 42
assert obj.double(2) == 4
assert MyClass.double(3) == 6
assert obj.inherited(1) == 2
assert isinstance(MyClass.__dict__['double'], staticmethod)
assert isinstance(MyClass.__dict__['inherited'], staticmethod)
span_names = [
c.args[0] for c in mock_tracer.start_as_current_span.call_args_list
]
assert span_names.count(f'{__name__}.MyClass.double') == 3
assert span_names.count(f'{__name__}.MyClass.inherited') == 1


@pytest.mark.xdist_group(name='telemetry_isolation')
@pytest.mark.parametrize(
'env_value,expected_tracing',
Expand Down
Loading