|
10 | 10 | ) |
11 | 11 |
|
12 | 12 | from strands.telemetry.tracer import JSONEncoder, Tracer, get_tracer, serialize |
| 13 | +from strands.types.content import ContentBlock |
13 | 14 | from strands.types.streaming import StopReason, Usage |
14 | 15 |
|
15 | 16 |
|
@@ -198,7 +199,116 @@ def test_start_tool_call_span(mock_tracer): |
198 | 199 | span = tracer.start_tool_call_span(tool) |
199 | 200 |
|
200 | 201 | mock_tracer.start_span.assert_called_once() |
201 | | - assert mock_tracer.start_span.call_args[1]["name"] == "Tool: test-tool" |
| 202 | + assert mock_tracer.start_span.call_args[1]["name"] == "execute_tool test-tool" |
| 203 | + mock_span.set_attribute.assert_any_call("gen_ai.tool.name", "test-tool") |
| 204 | + mock_span.set_attribute.assert_any_call("gen_ai.system", "strands-agents") |
| 205 | + mock_span.set_attribute.assert_any_call("gen_ai.operation.name", "execute_tool") |
| 206 | + mock_span.set_attribute.assert_any_call("gen_ai.tool.call.id", "123") |
| 207 | + mock_span.add_event.assert_any_call( |
| 208 | + "gen_ai.tool.message", attributes={"role": "tool", "content": json.dumps({"param": "value"}), "id": "123"} |
| 209 | + ) |
| 210 | + assert span is not None |
| 211 | + |
| 212 | + |
| 213 | +""" |
| 214 | +def start_swarm_span( |
| 215 | + self, |
| 216 | + task: str | list[ContentBlock], |
| 217 | + ) -> Optional[Span]: |
| 218 | + attributes: Dict[str, AttributeValue] = { |
| 219 | + "gen_ai.system": "strands-agents", |
| 220 | + "gen_ai.agent.name": "swarm", |
| 221 | + "gen_ai.operation.name": "invoke_swarm", |
| 222 | + } |
| 223 | +
|
| 224 | + span = self._start_span("invoke_swarm", attributes=attributes, span_kind=trace_api.SpanKind.CLIENT) |
| 225 | +
|
| 226 | + self._add_event( |
| 227 | + span, |
| 228 | + "gen_ai.user.message", |
| 229 | + event_attributes={ |
| 230 | + "content": serialize(task), |
| 231 | + }, |
| 232 | + ) |
| 233 | +
|
| 234 | + return span |
| 235 | +""" |
| 236 | + |
| 237 | + |
| 238 | +def test_start_swarm_call_span_with_string_task(mock_tracer): |
| 239 | + """Test starting a swarm call span with task as string.""" |
| 240 | + with mock.patch("strands.telemetry.tracer.trace_api.get_tracer", return_value=mock_tracer): |
| 241 | + tracer = Tracer() |
| 242 | + tracer.tracer = mock_tracer |
| 243 | + |
| 244 | + mock_span = mock.MagicMock() |
| 245 | + mock_tracer.start_span.return_value = mock_span |
| 246 | + |
| 247 | + task = "Design foo bar" |
| 248 | + |
| 249 | + span = tracer.start_swarm_span(task) |
| 250 | + |
| 251 | + mock_tracer.start_span.assert_called_once() |
| 252 | + assert mock_tracer.start_span.call_args[1]["name"] == "invoke_swarm" |
| 253 | + mock_span.set_attribute.assert_any_call("gen_ai.system", "strands-agents") |
| 254 | + mock_span.set_attribute.assert_any_call("gen_ai.agent.name", "swarm") |
| 255 | + mock_span.set_attribute.assert_any_call("gen_ai.operation.name", "invoke_swarm") |
| 256 | + mock_span.add_event.assert_any_call("gen_ai.user.message", attributes={"content": "Design foo bar"}) |
| 257 | + assert span is not None |
| 258 | + |
| 259 | + |
| 260 | +def test_start_swarm_span_with_contentblock_task(mock_tracer): |
| 261 | + """Test starting a swarm call span with task as list of contentBlock.""" |
| 262 | + with mock.patch("strands.telemetry.tracer.trace_api.get_tracer", return_value=mock_tracer): |
| 263 | + tracer = Tracer() |
| 264 | + tracer.tracer = mock_tracer |
| 265 | + |
| 266 | + mock_span = mock.MagicMock() |
| 267 | + mock_tracer.start_span.return_value = mock_span |
| 268 | + |
| 269 | + task = [ContentBlock(text="Original Task: foo bar")] |
| 270 | + |
| 271 | + span = tracer.start_swarm_span(task) |
| 272 | + |
| 273 | + mock_tracer.start_span.assert_called_once() |
| 274 | + assert mock_tracer.start_span.call_args[1]["name"] == "invoke_swarm" |
| 275 | + mock_span.set_attribute.assert_any_call("gen_ai.system", "strands-agents") |
| 276 | + mock_span.set_attribute.assert_any_call("gen_ai.agent.name", "swarm") |
| 277 | + mock_span.set_attribute.assert_any_call("gen_ai.operation.name", "invoke_swarm") |
| 278 | + mock_span.add_event.assert_any_call( |
| 279 | + "gen_ai.user.message", attributes={"content": '[{"text": "Original Task: foo bar"}]'} |
| 280 | + ) |
| 281 | + assert span is not None |
| 282 | + |
| 283 | + |
| 284 | +def test_end_swarm_span(mock_span): |
| 285 | + """Test ending a tool call span.""" |
| 286 | + tracer = Tracer() |
| 287 | + swarm_final_reuslt = "foo bar bar" |
| 288 | + |
| 289 | + tracer.end_swarm_span(mock_span, swarm_final_reuslt) |
| 290 | + |
| 291 | + mock_span.add_event.assert_called_with( |
| 292 | + "gen_ai.choice", |
| 293 | + attributes={"message": "foo bar bar"}, |
| 294 | + ) |
| 295 | + |
| 296 | + |
| 297 | +def test_start_graph_call_span(mock_tracer): |
| 298 | + """Test starting a graph call span.""" |
| 299 | + with mock.patch("strands.telemetry.tracer.trace_api.get_tracer", return_value=mock_tracer): |
| 300 | + tracer = Tracer() |
| 301 | + tracer.tracer = mock_tracer |
| 302 | + |
| 303 | + mock_span = mock.MagicMock() |
| 304 | + mock_tracer.start_span.return_value = mock_span |
| 305 | + |
| 306 | + tool = {"name": "test-tool", "toolUseId": "123", "input": {"param": "value"}} |
| 307 | + |
| 308 | + span = tracer.start_tool_call_span(tool) |
| 309 | + |
| 310 | + mock_tracer.start_span.assert_called_once() |
| 311 | + assert mock_tracer.start_span.call_args[1]["name"] == "execute_tool test-tool" |
202 | 312 | mock_span.set_attribute.assert_any_call("gen_ai.tool.name", "test-tool") |
203 | 313 | mock_span.set_attribute.assert_any_call("gen_ai.system", "strands-agents") |
204 | 314 | mock_span.set_attribute.assert_any_call("gen_ai.operation.name", "execute_tool") |
|
0 commit comments