-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_spans.py
More file actions
578 lines (500 loc) · 18.3 KB
/
test_spans.py
File metadata and controls
578 lines (500 loc) · 18.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Any
from typing import Dict
from typing import Optional
from unittest import mock
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import LlmAgent
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.telemetry.tracing import ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS
from google.adk.telemetry.tracing import trace_agent_invocation
from google.adk.telemetry.tracing import trace_call_llm
from google.adk.telemetry.tracing import trace_merged_tool_calls
from google.adk.telemetry.tracing import trace_send_data
from google.adk.telemetry.tracing import trace_tool_call
from google.adk.tools.base_tool import BaseTool
from google.genai import types
import pytest
class Event:
def __init__(self, event_id: str, event_content: Any):
self.id = event_id
self.content = event_content
def model_dumps_json(self, exclude_none: bool = False) -> str:
# This is just a stub for the spec. The mock will provide behavior.
return ''
@pytest.fixture
def mock_span_fixture():
return mock.MagicMock()
@pytest.fixture
def mock_tool_fixture():
tool = mock.Mock(spec=BaseTool)
tool.name = 'sample_tool'
tool.description = 'A sample tool for testing.'
return tool
@pytest.fixture
def mock_event_fixture():
event_mock = mock.create_autospec(Event, instance=True)
event_mock.model_dumps_json.return_value = (
'{"default_event_key": "default_event_value"}'
)
return event_mock
async def _create_invocation_context(
agent: LlmAgent, state: Optional[dict[str, Any]] = None
) -> InvocationContext:
session_service = InMemorySessionService()
session = await session_service.create_session(
app_name='test_app', user_id='test_user', state=state
)
invocation_context = InvocationContext(
invocation_id='test_id',
agent=agent,
session=session,
session_service=session_service,
)
return invocation_context
def _assert_span_attribute_set_to_empty_json(mock_span, attribute_name: str):
"""Helper to assert span attribute is set to empty JSON string '{}'."""
calls = [
call
for call in mock_span.set_attribute.call_args_list
if call.args[0] == attribute_name
]
assert len(calls) == 1, f"Expected '{attribute_name}' to be set exactly once"
assert calls[0].args[1] == '{}', (
f"Expected JSON string '{{}}' for {attribute_name} when content capture"
f' is disabled, got {calls[0].args[1]!r}'
)
@pytest.mark.asyncio
async def test_trace_agent_invocation(mock_span_fixture):
"""Test trace_agent_invocation sets span attributes correctly."""
agent = LlmAgent(name='test_llm_agent', model='gemini-pro')
agent.description = 'Test agent description'
invocation_context = await _create_invocation_context(agent)
trace_agent_invocation(mock_span_fixture, agent, invocation_context)
expected_calls = [
mock.call('gen_ai.operation.name', 'invoke_agent'),
mock.call('gen_ai.agent.description', agent.description),
mock.call('gen_ai.agent.name', agent.name),
mock.call(
'gen_ai.conversation.id',
invocation_context.session.id,
),
]
mock_span_fixture.set_attribute.assert_has_calls(
expected_calls, any_order=True
)
assert mock_span_fixture.set_attribute.call_count == len(expected_calls)
@pytest.mark.asyncio
async def test_trace_call_llm(monkeypatch, mock_span_fixture):
"""Test trace_call_llm sets all telemetry attributes correctly with normal content."""
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
agent = LlmAgent(name='test_agent')
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest(
model='gemini-pro',
contents=[
types.Content(
role='user',
parts=[types.Part(text='Hello, how are you?')],
),
],
config=types.GenerateContentConfig(
top_p=0.95,
max_output_tokens=1024,
),
)
llm_response = LlmResponse(
turn_complete=True,
finish_reason=types.FinishReason.STOP,
usage_metadata=types.GenerateContentResponseUsageMetadata(
total_token_count=100,
prompt_token_count=50,
candidates_token_count=50,
),
)
trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)
expected_calls = [
mock.call('gen_ai.system', 'gcp.vertex.agent'),
mock.call('gen_ai.request.top_p', 0.95),
mock.call('gen_ai.request.max_tokens', 1024),
mock.call('gcp.vertex.agent.llm_response', mock.ANY),
mock.call('gen_ai.usage.input_tokens', 50),
mock.call('gen_ai.usage.output_tokens', 50),
mock.call('gen_ai.response.finish_reasons', ['stop']),
]
assert mock_span_fixture.set_attribute.call_count == 12
mock_span_fixture.set_attribute.assert_has_calls(
expected_calls, any_order=True
)
@pytest.mark.asyncio
async def test_trace_call_llm_with_binary_content(
monkeypatch, mock_span_fixture
):
"""Test trace_call_llm handles binary content serialization correctly."""
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
agent = LlmAgent(name='test_agent')
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest(
model='gemini-pro',
contents=[
types.Content(
role='user',
parts=[
types.Part.from_function_response(
name='test_function_1',
response={
'result': b'test_data',
},
),
],
),
types.Content(
role='user',
parts=[
types.Part.from_function_response(
name='test_function_2',
response={
'result': types.Part.from_bytes(
data=b'test_data',
mime_type='application/octet-stream',
),
},
),
],
),
],
config=types.GenerateContentConfig(),
)
llm_response = LlmResponse(turn_complete=True)
trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)
# Verify basic telemetry attributes are set
expected_calls = [
mock.call('gen_ai.system', 'gcp.vertex.agent'),
]
assert mock_span_fixture.set_attribute.call_count == 7
mock_span_fixture.set_attribute.assert_has_calls(expected_calls)
# Verify binary values are properly serialized as base64
llm_request_json_str = None
for call_obj in mock_span_fixture.set_attribute.call_args_list:
arg_name, arg_value = call_obj.args
if arg_name == 'gcp.vertex.agent.llm_request':
llm_request_json_str = arg_value
break
assert llm_request_json_str is not None
# Verify bytes are base64 encoded (b'test_data' -> 'dGVzdF9kYXRh')
assert 'dGVzdF9kYXRh' in llm_request_json_str
# Verify no serialization failures
assert '<not serializable>' not in llm_request_json_str
@pytest.mark.asyncio
async def test_trace_call_llm_with_thought_signature(
monkeypatch, mock_span_fixture
):
"""Test trace_call_llm handles thought_signature bytes correctly.
This test verifies that thought_signature bytes from Gemini 3.0 models
are properly serialized as base64 in telemetry traces.
"""
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
agent = LlmAgent(name='test_agent')
invocation_context = await _create_invocation_context(agent)
# multi-turn conversation where the model's response contains
# thought_signature bytes
thought_signature_bytes = b'thought_signature'
llm_request = LlmRequest(
model='gemini-3-pro-preview',
contents=[
types.Content(
role='user',
parts=[types.Part(text='Hello')],
),
types.Content(
role='model',
parts=[
types.Part(
thought=True,
thought_signature=thought_signature_bytes,
)
],
),
types.Content(
role='user',
parts=[types.Part(text='Follow up question')],
),
],
config=types.GenerateContentConfig(),
)
llm_response = LlmResponse(turn_complete=True)
# should not raise TypeError for bytes serialization
trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)
llm_request_json_str = None
for call_obj in mock_span_fixture.set_attribute.call_args_list:
arg_name, arg_value = call_obj.args
if arg_name == 'gcp.vertex.agent.llm_request':
llm_request_json_str = arg_value
break
assert (
llm_request_json_str is not None
), "Attribute 'gcp.vertex.agent.llm_request' was not set on the span."
# no serialization failures
assert '<not serializable>' not in llm_request_json_str
# llm request is valid JSON
parsed = json.loads(llm_request_json_str)
assert parsed['model'] == 'gemini-3-pro-preview'
assert len(parsed['contents']) == 3
def test_trace_tool_call_with_scalar_response(
monkeypatch, mock_span_fixture, mock_tool_fixture, mock_event_fixture
):
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
test_args: Dict[str, Any] = {'param_a': 'value_a', 'param_b': 100}
test_tool_call_id: str = 'tool_call_id_001'
test_event_id: str = 'event_id_001'
scalar_function_response: Any = 'Scalar result'
expected_processed_response = {'result': scalar_function_response}
mock_event_fixture.id = test_event_id
mock_event_fixture.content = types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
id=test_tool_call_id,
name='test_function_1',
response={'result': scalar_function_response},
)
),
],
)
# Act
trace_tool_call(
tool=mock_tool_fixture,
args=test_args,
function_response_event=mock_event_fixture,
)
# Assert
expected_calls = [
mock.call('gen_ai.operation.name', 'execute_tool'),
mock.call('gen_ai.tool.name', mock_tool_fixture.name),
mock.call('gen_ai.tool.description', mock_tool_fixture.description),
mock.call('gen_ai.tool.type', 'BaseTool'),
mock.call('gen_ai.tool.call.id', test_tool_call_id),
mock.call('gcp.vertex.agent.tool_call_args', json.dumps(test_args)),
mock.call('gcp.vertex.agent.event_id', test_event_id),
mock.call(
'gcp.vertex.agent.tool_response',
json.dumps(expected_processed_response),
),
mock.call('gcp.vertex.agent.llm_request', '{}'),
mock.call('gcp.vertex.agent.llm_response', '{}'),
]
assert mock_span_fixture.set_attribute.call_count == len(expected_calls)
mock_span_fixture.set_attribute.assert_has_calls(
expected_calls, any_order=True
)
def test_trace_tool_call_with_dict_response(
monkeypatch, mock_span_fixture, mock_tool_fixture, mock_event_fixture
):
# Arrange
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
test_args: Dict[str, Any] = {'query': 'details', 'id_list': [1, 2, 3]}
test_tool_call_id: str = 'tool_call_id_002'
test_event_id: str = 'event_id_dict_002'
dict_function_response: Dict[str, Any] = {
'data': 'structured_data',
'count': 5,
}
mock_event_fixture.id = test_event_id
mock_event_fixture.content = types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
id=test_tool_call_id,
name='test_function_1',
response=dict_function_response,
)
),
],
)
# Act
trace_tool_call(
tool=mock_tool_fixture,
args=test_args,
function_response_event=mock_event_fixture,
)
# Assert
expected_calls = [
mock.call('gen_ai.operation.name', 'execute_tool'),
mock.call('gen_ai.tool.name', mock_tool_fixture.name),
mock.call('gen_ai.tool.description', mock_tool_fixture.description),
mock.call('gen_ai.tool.type', 'BaseTool'),
mock.call('gen_ai.tool.call.id', test_tool_call_id),
mock.call('gcp.vertex.agent.tool_call_args', json.dumps(test_args)),
mock.call('gcp.vertex.agent.event_id', test_event_id),
mock.call(
'gcp.vertex.agent.tool_response', json.dumps(dict_function_response)
),
mock.call('gcp.vertex.agent.llm_request', '{}'),
mock.call('gcp.vertex.agent.llm_response', '{}'),
]
assert mock_span_fixture.set_attribute.call_count == len(expected_calls)
mock_span_fixture.set_attribute.assert_has_calls(
expected_calls, any_order=True
)
def test_trace_merged_tool_calls_sets_correct_attributes(
monkeypatch, mock_span_fixture, mock_event_fixture
):
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
test_response_event_id = 'merged_evt_id_001'
custom_event_json_output = (
'{"custom_event_payload": true, "details": "merged_details"}'
)
mock_event_fixture.model_dumps_json.return_value = custom_event_json_output
trace_merged_tool_calls(
response_event_id=test_response_event_id,
function_response_event=mock_event_fixture,
)
expected_calls = [
mock.call('gen_ai.operation.name', 'execute_tool'),
mock.call('gen_ai.tool.name', '(merged tools)'),
mock.call('gen_ai.tool.description', '(merged tools)'),
mock.call('gen_ai.tool.call.id', test_response_event_id),
mock.call('gcp.vertex.agent.tool_call_args', 'N/A'),
mock.call('gcp.vertex.agent.event_id', test_response_event_id),
mock.call('gcp.vertex.agent.tool_response', custom_event_json_output),
mock.call('gcp.vertex.agent.llm_request', '{}'),
mock.call('gcp.vertex.agent.llm_response', '{}'),
]
assert mock_span_fixture.set_attribute.call_count == len(expected_calls)
mock_span_fixture.set_attribute.assert_has_calls(
expected_calls, any_order=True
)
mock_event_fixture.model_dumps_json.assert_called_once_with(exclude_none=True)
@pytest.mark.asyncio
async def test_call_llm_disabling_request_response_content(
monkeypatch, mock_span_fixture
):
"""Test trace_call_llm sets placeholders when capture is disabled."""
# Arrange
monkeypatch.setenv(ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS, 'false')
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
agent = LlmAgent(name='test_agent')
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest(
model='gemini-pro',
contents=[
types.Content(
role='user',
parts=[types.Part(text='Hello, how are you?')],
),
],
)
llm_response = LlmResponse(
turn_complete=True,
finish_reason=types.FinishReason.STOP,
)
# Act
trace_call_llm(invocation_context, 'test_event_id', llm_request, llm_response)
# Assert - Check attributes are set to JSON string '{}' not dict {}
_assert_span_attribute_set_to_empty_json(
mock_span_fixture, 'gcp.vertex.agent.llm_request'
)
_assert_span_attribute_set_to_empty_json(
mock_span_fixture, 'gcp.vertex.agent.llm_response'
)
def test_trace_tool_call_disabling_request_response_content(
monkeypatch,
mock_span_fixture,
mock_tool_fixture,
mock_event_fixture,
):
"""Test trace_tool_call sets placeholders when capture is disabled."""
# Arrange
monkeypatch.setenv(ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS, 'false')
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
test_args: Dict[str, Any] = {'query': 'details', 'id_list': [1, 2, 3]}
test_tool_call_id: str = 'tool_call_id_002'
test_event_id: str = 'event_id_dict_002'
dict_function_response: Dict[str, Any] = {
'data': 'structured_data',
'count': 5,
}
mock_event_fixture.id = test_event_id
mock_event_fixture.content = types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
id=test_tool_call_id,
name='test_function_1',
response=dict_function_response,
)
),
],
)
# Act
trace_tool_call(
tool=mock_tool_fixture,
args=test_args,
function_response_event=mock_event_fixture,
)
# Assert - Check attributes are set to JSON string '{}' not dict {}
_assert_span_attribute_set_to_empty_json(
mock_span_fixture, 'gcp.vertex.agent.tool_call_args'
)
_assert_span_attribute_set_to_empty_json(
mock_span_fixture, 'gcp.vertex.agent.tool_response'
)
def test_trace_merged_tool_disabling_request_response_content(
monkeypatch,
mock_span_fixture,
mock_event_fixture,
):
"""Test trace_merged_tool_calls sets placeholders when capture is disabled."""
# Arrange
monkeypatch.setenv(ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS, 'false')
monkeypatch.setattr(
'opentelemetry.trace.get_current_span', lambda: mock_span_fixture
)
test_response_event_id = 'merged_evt_id_001'
custom_event_json_output = (
'{"custom_event_payload": true, "details": "merged_details"}'
)
mock_event_fixture.model_dumps_json.return_value = custom_event_json_output
# Act
trace_merged_tool_calls(
response_event_id=test_response_event_id,
function_response_event=mock_event_fixture,
)
# Assert - Check attribute is set to JSON string '{}' not dict {}
_assert_span_attribute_set_to_empty_json(
mock_span_fixture, 'gcp.vertex.agent.tool_response'
)