forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_telemetry.py
More file actions
184 lines (132 loc) · 4.59 KB
/
Copy pathtest_telemetry.py
File metadata and controls
184 lines (132 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import asyncio
from typing import NoReturn
from unittest import mock
import pytest
from a2a.utils.telemetry import trace_class, trace_function
@pytest.fixture
def mock_span():
return mock.MagicMock()
@pytest.fixture
def mock_tracer(mock_span):
tracer = mock.MagicMock()
tracer.start_as_current_span.return_value.__enter__.return_value = mock_span
tracer.start_as_current_span.return_value.__exit__.return_value = False
return tracer
@pytest.fixture(autouse=True)
def patch_trace_get_tracer(mock_tracer):
with mock.patch('opentelemetry.trace.get_tracer', return_value=mock_tracer):
yield
def test_trace_function_sync_success(mock_span):
@trace_function
def foo(x, y):
return x + y
result = foo(2, 3)
assert result == 5
mock_span.set_status.assert_called()
mock_span.set_status.assert_any_call(mock.ANY)
mock_span.record_exception.assert_not_called()
def test_trace_function_sync_exception(mock_span):
@trace_function
def bar() -> NoReturn:
raise ValueError('fail')
with pytest.raises(ValueError):
bar()
mock_span.record_exception.assert_called()
mock_span.set_status.assert_any_call(mock.ANY, description='fail')
def test_trace_function_sync_attribute_extractor_called(mock_span):
called = {}
def attr_extractor(span, args, kwargs, result, exception) -> None:
called['called'] = True
assert span is mock_span
assert exception is None
assert result == 42
@trace_function(attribute_extractor=attr_extractor)
def foo() -> int:
return 42
foo()
assert called['called']
def test_trace_function_sync_attribute_extractor_error_logged(mock_span):
with mock.patch('a2a.utils.telemetry.logger') as logger:
def attr_extractor(span, args, kwargs, result, exception) -> NoReturn:
raise RuntimeError('attr fail')
@trace_function(attribute_extractor=attr_extractor)
def foo() -> int:
return 1
foo()
logger.error.assert_any_call(mock.ANY)
@pytest.mark.asyncio
async def test_trace_function_async_success(mock_span):
@trace_function
async def foo(x):
await asyncio.sleep(0)
return x * 2
result = await foo(4)
assert result == 8
mock_span.set_status.assert_called()
mock_span.record_exception.assert_not_called()
@pytest.mark.asyncio
async def test_trace_function_async_exception(mock_span):
@trace_function
async def bar() -> NoReturn:
await asyncio.sleep(0)
raise RuntimeError('async fail')
with pytest.raises(RuntimeError):
await bar()
mock_span.record_exception.assert_called()
mock_span.set_status.assert_any_call(mock.ANY, description='async fail')
@pytest.mark.asyncio
async def test_trace_function_async_attribute_extractor_called(mock_span):
called = {}
def attr_extractor(span, args, kwargs, result, exception) -> None:
called['called'] = True
assert exception is None
assert result == 99
@trace_function(attribute_extractor=attr_extractor)
async def foo() -> int:
return 99
await foo()
assert called['called']
def test_trace_function_with_args_and_attributes(mock_span):
@trace_function(span_name='custom.span', attributes={'foo': 'bar'})
def foo() -> int:
return 1
foo()
mock_span.set_attribute.assert_any_call('foo', 'bar')
def test_trace_class_exclude_list(mock_span):
@trace_class(exclude_list=['skip_me'])
class MyClass:
def a(self) -> str:
return 'a'
def skip_me(self) -> str:
return 'skip'
def __str__(self):
return 'str'
obj = MyClass()
assert obj.a() == 'a'
assert obj.skip_me() == 'skip'
# Only 'a' is traced, not 'skip_me' or dunder
assert hasattr(obj.a, '__wrapped__')
assert not hasattr(obj.skip_me, '__wrapped__')
def test_trace_class_include_list(mock_span):
@trace_class(include_list=['only_this'])
class MyClass:
def only_this(self) -> str:
return 'yes'
def not_this(self) -> str:
return 'no'
obj = MyClass()
assert obj.only_this() == 'yes'
assert obj.not_this() == 'no'
assert hasattr(obj.only_this, '__wrapped__')
assert not hasattr(obj.not_this, '__wrapped__')
def test_trace_class_dunder_not_traced(mock_span):
@trace_class()
class MyClass:
def __init__(self):
self.x = 1
def foo(self) -> str:
return 'foo'
obj = MyClass()
assert obj.foo() == 'foo'
assert hasattr(obj.foo, '__wrapped__')
assert hasattr(obj, 'x')