|
18 | 18 |
|
19 | 19 | import asyncio |
20 | 20 | import importlib.metadata |
| 21 | +import json |
21 | 22 | import os |
22 | 23 | from dataclasses import asdict |
23 | 24 | from types import SimpleNamespace |
|
41 | 42 | from agentscope.model import ChatResponse, DashScopeChatModel # noqa: E402 |
42 | 43 | from agentscope.tool import ToolResponse # noqa: E402 |
43 | 44 |
|
| 45 | +from opentelemetry import trace as trace_api # noqa: E402 |
44 | 46 | from opentelemetry.instrumentation.agentscope._v2_middleware import ( # noqa: E402 |
45 | 47 | AgentScopeV2Middleware, |
46 | 48 | _message_to_input, |
47 | 49 | ) |
48 | 50 | from opentelemetry.instrumentation.agentscope.package import ( # noqa: E402 |
49 | 51 | get_installed_instrumentation_dependencies, |
50 | 52 | ) |
| 53 | +from opentelemetry.semconv._incubating.attributes import ( # noqa: E402 |
| 54 | + gen_ai_attributes as GenAI, |
| 55 | +) |
51 | 56 | from opentelemetry.trace.status import StatusCode # noqa: E402 |
52 | 57 | from opentelemetry.util.genai.utils import gen_ai_json_dumps # noqa: E402 |
53 | 58 |
|
@@ -209,6 +214,114 @@ async def stream_handler(**kwargs): |
209 | 214 | assert span.attributes["error.type"] == "RuntimeError" |
210 | 215 |
|
211 | 216 |
|
| 217 | +async def test_v2_streaming_model_call_starts_llm_span_before_model_handler( |
| 218 | + instrument, |
| 219 | + span_exporter, |
| 220 | +): |
| 221 | + agent = Agent( |
| 222 | + name="stream_suppression_agent", |
| 223 | + system_prompt="Reply briefly.", |
| 224 | + model=_make_model(stream=True), |
| 225 | + ) |
| 226 | + middleware = _middleware(agent._model_call_middlewares) |
| 227 | + observed_current_span_ids = [] |
| 228 | + consumer_current_span_ids = [] |
| 229 | + |
| 230 | + async def stream_handler(**kwargs): |
| 231 | + del kwargs |
| 232 | + observed_current_span_ids.append( |
| 233 | + trace_api.get_current_span().get_span_context().span_id |
| 234 | + ) |
| 235 | + |
| 236 | + async def stream(): |
| 237 | + observed_current_span_ids.append( |
| 238 | + trace_api.get_current_span().get_span_context().span_id |
| 239 | + ) |
| 240 | + yield ChatResponse( |
| 241 | + content=[TextBlock(text="partial")], |
| 242 | + is_last=False, |
| 243 | + ) |
| 244 | + observed_current_span_ids.append( |
| 245 | + trace_api.get_current_span().get_span_context().span_id |
| 246 | + ) |
| 247 | + yield ChatResponse( |
| 248 | + content=[TextBlock(text="done")], |
| 249 | + is_last=True, |
| 250 | + ) |
| 251 | + |
| 252 | + return stream() |
| 253 | + |
| 254 | + stream = await middleware.on_model_call( |
| 255 | + agent, |
| 256 | + { |
| 257 | + "current_model": agent.model, |
| 258 | + "messages": [UserMsg(name="user", content="hello")], |
| 259 | + }, |
| 260 | + stream_handler, |
| 261 | + ) |
| 262 | + |
| 263 | + async for _ in stream: |
| 264 | + consumer_current_span_ids.append( |
| 265 | + trace_api.get_current_span().get_span_context().span_id |
| 266 | + ) |
| 267 | + |
| 268 | + spans = _spans_by_operation(span_exporter.get_finished_spans(), "chat") |
| 269 | + assert len(spans) == 1 |
| 270 | + llm_span_id = spans[0].context.span_id |
| 271 | + assert observed_current_span_ids == [llm_span_id, llm_span_id, llm_span_id] |
| 272 | + assert consumer_current_span_ids == [llm_span_id, llm_span_id] |
| 273 | + assert ( |
| 274 | + trace_api.get_current_span().get_span_context().span_id != llm_span_id |
| 275 | + ) |
| 276 | + |
| 277 | + |
| 278 | +async def test_v2_streaming_model_call_captures_input_and_output_content( |
| 279 | + instrument_with_content, |
| 280 | + span_exporter, |
| 281 | +): |
| 282 | + agent = Agent( |
| 283 | + name="stream_content_agent", |
| 284 | + system_prompt="Reply briefly.", |
| 285 | + model=_make_model(stream=True), |
| 286 | + ) |
| 287 | + middleware = _middleware(agent._model_call_middlewares) |
| 288 | + |
| 289 | + async def stream_handler(**kwargs): |
| 290 | + del kwargs |
| 291 | + |
| 292 | + async def stream(): |
| 293 | + yield ChatResponse( |
| 294 | + content=[TextBlock(text="partial")], |
| 295 | + is_last=False, |
| 296 | + ) |
| 297 | + yield ChatResponse( |
| 298 | + content=[TextBlock(text="done")], |
| 299 | + is_last=True, |
| 300 | + ) |
| 301 | + |
| 302 | + return stream() |
| 303 | + |
| 304 | + stream = await middleware.on_model_call( |
| 305 | + agent, |
| 306 | + { |
| 307 | + "current_model": agent.model, |
| 308 | + "messages": [UserMsg(name="user", content="hello")], |
| 309 | + }, |
| 310 | + stream_handler, |
| 311 | + ) |
| 312 | + |
| 313 | + async for _ in stream: |
| 314 | + pass |
| 315 | + |
| 316 | + span = _spans_by_operation(span_exporter.get_finished_spans(), "chat")[0] |
| 317 | + input_messages = json.loads(span.attributes[GenAI.GEN_AI_INPUT_MESSAGES]) |
| 318 | + output_messages = json.loads(span.attributes[GenAI.GEN_AI_OUTPUT_MESSAGES]) |
| 319 | + assert input_messages[0]["role"] == "user" |
| 320 | + assert input_messages[0]["parts"][0]["content"] == "hello" |
| 321 | + assert output_messages[0]["role"] == "assistant" |
| 322 | + assert output_messages[0]["parts"][0]["content"] == "done" |
| 323 | + |
| 324 | + |
212 | 325 | async def test_v2_tool_acting_hook(instrument, span_exporter): |
213 | 326 | agent = Agent( |
214 | 327 | name="tool_agent", |
|
0 commit comments