Skip to content

Commit 7a6064a

Browse files
poshinchendbschmigelski
authored andcommitted
fix: include agent trace into tool for agent as tools (#526)
1 parent 41f3bd3 commit 7a6064a

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/strands/telemetry/tracer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def end_model_invoke_span(
273273

274274
self._end_span(span, attributes, error)
275275

276-
def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None, **kwargs: Any) -> Optional[Span]:
276+
def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None, **kwargs: Any) -> Span:
277277
"""Start a new span for a tool call.
278278
279279
Args:

src/strands/tools/executor.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from typing import Any, Optional, cast
77

8-
from opentelemetry import trace
8+
from opentelemetry import trace as trace_api
99

1010
from ..telemetry.metrics import EventLoopMetrics, Trace
1111
from ..telemetry.tracer import get_tracer
@@ -23,7 +23,7 @@ async def run_tools(
2323
invalid_tool_use_ids: list[str],
2424
tool_results: list[ToolResult],
2525
cycle_trace: Trace,
26-
parent_span: Optional[trace.Span] = None,
26+
parent_span: Optional[trace_api.Span] = None,
2727
) -> ToolGenerator:
2828
"""Execute tools concurrently.
2929
@@ -53,24 +53,23 @@ async def work(
5353
tool_name = tool_use["name"]
5454
tool_trace = Trace(f"Tool: {tool_name}", parent_id=cycle_trace.id, raw_name=tool_name)
5555
tool_start_time = time.time()
56+
with trace_api.use_span(tool_call_span):
57+
try:
58+
async for event in handler(tool_use):
59+
worker_queue.put_nowait((worker_id, event))
60+
await worker_event.wait()
61+
worker_event.clear()
62+
63+
result = cast(ToolResult, event)
64+
finally:
65+
worker_queue.put_nowait((worker_id, stop_event))
66+
67+
tool_success = result.get("status") == "success"
68+
tool_duration = time.time() - tool_start_time
69+
message = Message(role="user", content=[{"toolResult": result}])
70+
event_loop_metrics.add_tool_usage(tool_use, tool_duration, tool_trace, tool_success, message)
71+
cycle_trace.add_child(tool_trace)
5672

57-
try:
58-
async for event in handler(tool_use):
59-
worker_queue.put_nowait((worker_id, event))
60-
await worker_event.wait()
61-
worker_event.clear()
62-
63-
result = cast(ToolResult, event)
64-
finally:
65-
worker_queue.put_nowait((worker_id, stop_event))
66-
67-
tool_success = result.get("status") == "success"
68-
tool_duration = time.time() - tool_start_time
69-
message = Message(role="user", content=[{"toolResult": result}])
70-
event_loop_metrics.add_tool_usage(tool_use, tool_duration, tool_trace, tool_success, message)
71-
cycle_trace.add_child(tool_trace)
72-
73-
if tool_call_span:
7473
tracer.end_tool_call_span(tool_call_span, result)
7574

7675
return result

0 commit comments

Comments
 (0)