Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/strands/telemetry/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def end_model_invoke_span(

self._end_span(span, attributes, error)

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

Args:
Expand Down
37 changes: 18 additions & 19 deletions src/strands/tools/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
from typing import Any, Optional, cast

from opentelemetry import trace
from opentelemetry import trace as trace_api

from ..telemetry.metrics import EventLoopMetrics, Trace
from ..telemetry.tracer import get_tracer
Expand All @@ -23,7 +23,7 @@ async def run_tools(
invalid_tool_use_ids: list[str],
tool_results: list[ToolResult],
cycle_trace: Trace,
parent_span: Optional[trace.Span] = None,
parent_span: Optional[trace_api.Span] = None,
) -> ToolGenerator:
"""Execute tools concurrently.

Expand Down Expand Up @@ -53,24 +53,23 @@ async def work(
tool_name = tool_use["name"]
tool_trace = Trace(f"Tool: {tool_name}", parent_id=cycle_trace.id, raw_name=tool_name)
tool_start_time = time.time()
with trace_api.use_span(tool_call_span):
try:
async for event in handler(tool_use):
worker_queue.put_nowait((worker_id, event))
await worker_event.wait()
worker_event.clear()

result = cast(ToolResult, event)
finally:
worker_queue.put_nowait((worker_id, stop_event))

tool_success = result.get("status") == "success"
tool_duration = time.time() - tool_start_time
message = Message(role="user", content=[{"toolResult": result}])
event_loop_metrics.add_tool_usage(tool_use, tool_duration, tool_trace, tool_success, message)
cycle_trace.add_child(tool_trace)

try:
async for event in handler(tool_use):
worker_queue.put_nowait((worker_id, event))
await worker_event.wait()
worker_event.clear()

result = cast(ToolResult, event)
finally:
worker_queue.put_nowait((worker_id, stop_event))

tool_success = result.get("status") == "success"
tool_duration = time.time() - tool_start_time
message = Message(role="user", content=[{"toolResult": result}])
event_loop_metrics.add_tool_usage(tool_use, tool_duration, tool_trace, tool_success, message)
cycle_trace.add_child(tool_trace)

if tool_call_span:
tracer.end_tool_call_span(tool_call_span, result)

return result
Expand Down
Loading