|
20 | 20 | from agentops.semconv.tool import ToolAttributes |
21 | 21 | from agentops.semconv.span_kinds import AgentOpsSpanKindValues |
22 | 22 |
|
23 | | -from opentelemetry.trace import SpanKind, get_tracer, set_span_in_context, get_current |
| 23 | +from opentelemetry.trace import SpanKind, get_tracer, set_span_in_context |
24 | 24 |
|
25 | 25 | logger = logging.getLogger(__name__) |
26 | 26 |
|
27 | 27 | LLM_REQUEST_TYPE = LLMRequestTypeValues.CHAT |
28 | 28 |
|
29 | 29 |
|
30 | | -def _create_tool_span(parent_span: Span, tool_call_data: Dict[str, Any]): |
| 30 | +def _create_tool_span(parent_span: Span, tool_call_data: Dict[str, Any]) -> Span: |
31 | 31 | """ |
32 | | - Create a distinct span for each tool call. |
33 | | -
|
| 32 | + Create a distinct span for each tool call and return it. |
34 | 33 | Args: |
35 | 34 | parent_span: The parent LLM span |
36 | 35 | tool_call_data: The tool call data dictionary |
| 36 | + Returns: |
| 37 | + The created tool span. |
37 | 38 | """ |
38 | 39 | # Get the tracer for this module |
39 | 40 | tracer = get_tracer(__name__) |
40 | 41 |
|
41 | 42 | # Create a child span for the tool call |
42 | | - with tracer.start_as_current_span( |
| 43 | + tool_span = tracer.start_span( |
43 | 44 | name=f"tool_call.{tool_call_data['function']['name']}", |
44 | 45 | kind=SpanKind.INTERNAL, |
45 | | - context=set_span_in_context(parent_span, get_current()), |
46 | | - ) as tool_span: |
47 | | - # Set the span kind to TOOL |
48 | | - tool_span.set_attribute("agentops.span.kind", AgentOpsSpanKindValues.TOOL) |
| 46 | + context=set_span_in_context(parent_span), |
| 47 | + ) |
| 48 | + |
| 49 | + # Set the span kind to TOOL |
| 50 | + tool_span.set_attribute("agentops.span.kind", AgentOpsSpanKindValues.TOOL) |
49 | 51 |
|
50 | | - # Set tool-specific attributes |
51 | | - tool_span.set_attribute(ToolAttributes.TOOL_NAME, tool_call_data["function"]["name"]) |
52 | | - tool_span.set_attribute(ToolAttributes.TOOL_PARAMETERS, tool_call_data["function"]["arguments"]) |
53 | | - tool_span.set_attribute("tool.call.id", tool_call_data["id"]) |
54 | | - tool_span.set_attribute("tool.call.type", tool_call_data["type"]) |
| 52 | + # Set tool-specific attributes |
| 53 | + tool_span.set_attribute(ToolAttributes.TOOL_NAME, tool_call_data["function"]["name"]) |
| 54 | + tool_span.set_attribute(ToolAttributes.TOOL_PARAMETERS, tool_call_data["function"]["arguments"]) |
| 55 | + tool_span.set_attribute("tool.call.id", tool_call_data["id"]) |
| 56 | + tool_span.set_attribute("tool.call.type", tool_call_data["type"]) |
| 57 | + return tool_span |
55 | 58 |
|
56 | 59 |
|
57 | 60 | def handle_chat_attributes( |
@@ -231,20 +234,23 @@ def handle_chat_attributes( |
231 | 234 | # Tool calls |
232 | 235 | if "tool_calls" in message: |
233 | 236 | tool_calls = message["tool_calls"] |
234 | | - if tool_calls and span is not None: |
235 | | - for i, tool_call in enumerate(tool_calls): |
236 | | - # Convert tool_call to the format expected by _create_tool_span |
237 | | - function = tool_call.get("function", {}) |
238 | | - tool_call_data = { |
239 | | - "id": tool_call.get("id", ""), |
240 | | - "type": tool_call.get("type", "function"), |
241 | | - "function": { |
242 | | - "name": function.get("name", ""), |
243 | | - "arguments": function.get("arguments", ""), |
244 | | - }, |
245 | | - } |
246 | | - # Create a child span for this tool call |
247 | | - _create_tool_span(span, tool_call_data) |
| 237 | + if tool_calls: |
| 238 | + if span is None: |
| 239 | + logger.debug("Skipping tool span creation: no parent span provided") |
| 240 | + else: |
| 241 | + for i, tool_call in enumerate(tool_calls): |
| 242 | + # Convert tool_call to the format expected by _create_tool_span |
| 243 | + function = tool_call.get("function", {}) |
| 244 | + tool_call_data = { |
| 245 | + "id": tool_call.get("id", ""), |
| 246 | + "type": tool_call.get("type", "function"), |
| 247 | + "function": { |
| 248 | + "name": function.get("name", ""), |
| 249 | + "arguments": function.get("arguments", ""), |
| 250 | + }, |
| 251 | + } |
| 252 | + # Create a child span for this tool call |
| 253 | + _create_tool_span(span, tool_call_data) |
248 | 254 |
|
249 | 255 | # Prompt filter results |
250 | 256 | if "prompt_filter_results" in response_dict: |
|
0 commit comments