|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import asyncio |
15 | 16 | import gc |
16 | 17 | import sys |
17 | | -from unittest import mock |
18 | 18 |
|
19 | 19 | from google.adk.agents import base_agent |
20 | 20 | from google.adk.agents.llm_agent import Agent |
21 | 21 | from google.adk.models.base_llm import BaseLlm |
| 22 | +from google.adk.models.llm_response import LlmResponse |
22 | 23 | from google.adk.telemetry import tracing |
23 | 24 | from google.adk.tools import FunctionTool |
24 | 25 | from google.adk.utils.context_utils import Aclosing |
| 26 | +from google.genai.types import Content |
25 | 27 | from google.genai.types import Part |
26 | | -from opentelemetry.version import __version__ |
| 28 | +from opentelemetry.sdk.trace import TracerProvider |
| 29 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
| 30 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
27 | 31 | import pytest |
28 | 32 |
|
29 | 33 | from ..testing_utils import MockModel |
@@ -63,27 +67,27 @@ async def test_runner(test_agent: Agent) -> TestInMemoryRunner: |
63 | 67 |
|
64 | 68 |
|
65 | 69 | @pytest.fixture |
66 | | -def mock_start_as_current_span(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: |
67 | | - mock_context_manager = mock.MagicMock() |
68 | | - mock_context_manager.__enter__.return_value = mock.Mock() |
69 | | - mock_start_as_current_span = mock.Mock() |
70 | | - mock_start_as_current_span.return_value = mock_context_manager |
| 70 | +def span_exporter(monkeypatch: pytest.MonkeyPatch) -> InMemorySpanExporter: |
| 71 | + tracer_provider = TracerProvider() |
| 72 | + span_exporter = InMemorySpanExporter() |
| 73 | + tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter)) |
| 74 | + real_tracer = tracer_provider.get_tracer(__name__) |
71 | 75 |
|
72 | 76 | def do_replace(tracer): |
73 | 77 | monkeypatch.setattr( |
74 | | - tracer, 'start_as_current_span', mock_start_as_current_span |
| 78 | + tracer, 'start_as_current_span', real_tracer.start_as_current_span |
75 | 79 | ) |
76 | 80 |
|
77 | 81 | do_replace(tracing.tracer) |
78 | 82 | do_replace(base_agent.tracer) |
79 | 83 |
|
80 | | - return mock_start_as_current_span |
| 84 | + return span_exporter |
81 | 85 |
|
82 | 86 |
|
83 | 87 | @pytest.mark.asyncio |
84 | 88 | async def test_tracer_start_as_current_span( |
85 | 89 | test_runner: TestInMemoryRunner, |
86 | | - mock_start_as_current_span: mock.Mock, |
| 90 | + span_exporter: InMemorySpanExporter, |
87 | 91 | ): |
88 | 92 | """Test creation of multiple spans in an E2E runner invocation. |
89 | 93 |
|
@@ -112,18 +116,49 @@ def wrapped_firstiter(coro): |
112 | 116 | pass |
113 | 117 |
|
114 | 118 | # Assert |
115 | | - expected_start_as_current_span_calls = [ |
116 | | - mock.call('invocation'), |
117 | | - mock.call('execute_tool some_tool'), |
118 | | - mock.call('invoke_agent some_root_agent'), |
119 | | - mock.call('call_llm'), |
120 | | - mock.call('call_llm'), |
| 119 | + spans = span_exporter.get_finished_spans() |
| 120 | + assert list(sorted(span.name for span in spans)) == [ |
| 121 | + 'call_llm', |
| 122 | + 'call_llm', |
| 123 | + 'execute_tool some_tool', |
| 124 | + 'invocation', |
| 125 | + 'invoke_agent some_root_agent', |
121 | 126 | ] |
122 | 127 |
|
123 | | - mock_start_as_current_span.assert_has_calls( |
124 | | - expected_start_as_current_span_calls, |
125 | | - any_order=True, |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_exception_preserves_attributes( |
| 131 | + test_model: BaseLlm, span_exporter: InMemorySpanExporter |
| 132 | +): |
| 133 | + """Test when an exception occurs during tool execution, span attributes are still present on spans where they are expected.""" |
| 134 | + |
| 135 | + # Arrange |
| 136 | + async def some_tool(): |
| 137 | + raise ValueError('This tool always fails') |
| 138 | + |
| 139 | + test_agent = Agent( |
| 140 | + name='some_root_agent', |
| 141 | + model=test_model, |
| 142 | + tools=[ |
| 143 | + FunctionTool(some_tool), |
| 144 | + ], |
126 | 145 | ) |
127 | | - assert mock_start_as_current_span.call_count == len( |
128 | | - expected_start_as_current_span_calls |
| 146 | + |
| 147 | + test_runner = TestInMemoryRunner(test_agent) |
| 148 | + |
| 149 | + # Act |
| 150 | + with pytest.raises(ValueError, match='This tool always fails'): |
| 151 | + async with Aclosing( |
| 152 | + test_runner.run_async_with_new_session_agen('') |
| 153 | + ) as agen: |
| 154 | + async for _ in agen: |
| 155 | + pass |
| 156 | + |
| 157 | + # Assert |
| 158 | + spans = span_exporter.get_finished_spans() |
| 159 | + assert len(spans) > 1 |
| 160 | + assert all( |
| 161 | + span.attributes is not None and len(span.attributes) > 0 |
| 162 | + for span in spans |
| 163 | + if span.name != 'invocation' # not expected to have attributes |
129 | 164 | ) |
0 commit comments