|
| 1 | +import json |
| 2 | +from importlib_metadata import version as v |
| 3 | +from langtrace.trace_attributes import FrameworkSpanAttributes |
| 4 | +from opentelemetry import baggage |
| 5 | +from opentelemetry.trace import Span, SpanKind, Tracer |
| 6 | +from opentelemetry.trace.status import Status, StatusCode |
| 7 | +from typing import Dict, Any, Optional |
| 8 | + |
| 9 | +from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME |
| 10 | +from langtrace_python_sdk.constants.instrumentation.common import ( |
| 11 | + LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, |
| 12 | + SERVICE_PROVIDERS, |
| 13 | +) |
| 14 | +from langtrace_python_sdk.utils import set_span_attribute |
| 15 | +from langtrace_python_sdk.utils.llm import get_span_name, set_span_attributes |
| 16 | +from langtrace_python_sdk.utils.misc import serialize_args, serialize_kwargs |
| 17 | + |
| 18 | +def _extract_metrics(metrics: Dict[str, Any]) -> Dict[str, Any]: |
| 19 | + """Helper function to extract and format metrics""" |
| 20 | + formatted_metrics = {} |
| 21 | + |
| 22 | + # Extract basic metrics |
| 23 | + for key in ['time', 'time_to_first_token', 'input_tokens', 'output_tokens', |
| 24 | + 'prompt_tokens', 'completion_tokens', 'total_tokens']: |
| 25 | + if key in metrics: |
| 26 | + formatted_metrics[key] = metrics[key] |
| 27 | + |
| 28 | + # Extract nested metric details if present |
| 29 | + if 'prompt_tokens_details' in metrics: |
| 30 | + formatted_metrics['prompt_tokens_details'] = metrics['prompt_tokens_details'] |
| 31 | + if 'completion_tokens_details' in metrics: |
| 32 | + formatted_metrics['completion_tokens_details'] = metrics['completion_tokens_details'] |
| 33 | + if 'tool_call_times' in metrics: |
| 34 | + formatted_metrics['tool_call_times'] = metrics['tool_call_times'] |
| 35 | + |
| 36 | + return formatted_metrics |
| 37 | + |
| 38 | +def patch_memory(operation_name, version, tracer: Tracer): |
| 39 | + def traced_method(wrapped, instance, args, kwargs): |
| 40 | + service_provider = SERVICE_PROVIDERS["AGNO"] |
| 41 | + extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY) |
| 42 | + span_attributes = { |
| 43 | + "langtrace.sdk.name": "langtrace-python-sdk", |
| 44 | + "langtrace.service.name": service_provider, |
| 45 | + "langtrace.service.type": "framework", |
| 46 | + "langtrace.service.version": version, |
| 47 | + "langtrace.version": v(LANGTRACE_SDK_NAME), |
| 48 | + **(extra_attributes if extra_attributes is not None else {}), |
| 49 | + } |
| 50 | + |
| 51 | + span_attributes.update({ |
| 52 | + "agno.memory.type": type(instance).__name__, |
| 53 | + "agno.memory.create_session_summary": str(instance.create_session_summary), |
| 54 | + "agno.memory.create_user_memories": str(instance.create_user_memories), |
| 55 | + "agno.memory.retrieval": str(instance.retrieval) |
| 56 | + }) |
| 57 | + |
| 58 | + inputs = {} |
| 59 | + if len(args) > 0: |
| 60 | + inputs["args"] = serialize_args(*args) |
| 61 | + if len(kwargs) > 0: |
| 62 | + inputs["kwargs"] = serialize_kwargs(**kwargs) |
| 63 | + span_attributes["agno.memory.inputs"] = json.dumps(inputs) |
| 64 | + |
| 65 | + attributes = FrameworkSpanAttributes(**span_attributes) |
| 66 | + |
| 67 | + with tracer.start_as_current_span( |
| 68 | + get_span_name(operation_name), kind=SpanKind.CLIENT |
| 69 | + ) as span: |
| 70 | + try: |
| 71 | + set_span_attributes(span, attributes) |
| 72 | + result = wrapped(*args, **kwargs) |
| 73 | + |
| 74 | + if result is not None: |
| 75 | + set_span_attribute(span, "agno.memory.output", str(result)) |
| 76 | + |
| 77 | + if instance.summary is not None: |
| 78 | + set_span_attribute(span, "agno.memory.summary", str(instance.summary)) |
| 79 | + if instance.memories is not None: |
| 80 | + set_span_attribute(span, "agno.memory.memories_count", str(len(instance.memories))) |
| 81 | + |
| 82 | + span.set_status(Status(StatusCode.OK)) |
| 83 | + return result |
| 84 | + |
| 85 | + except Exception as err: |
| 86 | + span.record_exception(err) |
| 87 | + span.set_status(Status(StatusCode.ERROR, str(err))) |
| 88 | + raise |
| 89 | + |
| 90 | + return traced_method |
| 91 | + |
| 92 | +def patch_agent(operation_name, version, tracer: Tracer): |
| 93 | + def traced_method(wrapped, instance, args, kwargs): |
| 94 | + service_provider = SERVICE_PROVIDERS["AGNO"] |
| 95 | + extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY) |
| 96 | + span_attributes = { |
| 97 | + "langtrace.sdk.name": "langtrace-python-sdk", |
| 98 | + "langtrace.service.name": service_provider, |
| 99 | + "langtrace.service.type": "framework", |
| 100 | + "langtrace.service.version": version, |
| 101 | + "langtrace.version": v(LANGTRACE_SDK_NAME), |
| 102 | + **(extra_attributes if extra_attributes is not None else {}), |
| 103 | + } |
| 104 | + |
| 105 | + attributes = FrameworkSpanAttributes(**span_attributes) |
| 106 | + |
| 107 | + with tracer.start_as_current_span( |
| 108 | + get_span_name(operation_name), kind=SpanKind.CLIENT |
| 109 | + ) as span: |
| 110 | + try: |
| 111 | + set_span_attributes(span, attributes) |
| 112 | + AgnoSpanAttributes(span=span, instance=instance) |
| 113 | + result_generator = wrapped(*args, **kwargs) |
| 114 | + |
| 115 | + accumulated_content = "" |
| 116 | + current_tool_call = None |
| 117 | + response_metadata = None |
| 118 | + seen_tool_calls = set() |
| 119 | + |
| 120 | + try: |
| 121 | + for response in result_generator: |
| 122 | + if not hasattr(response, 'to_dict'): |
| 123 | + yield response |
| 124 | + continue |
| 125 | + |
| 126 | + if not response_metadata: |
| 127 | + response_metadata = { |
| 128 | + "run_id": response.run_id, |
| 129 | + "agent_id": response.agent_id, |
| 130 | + "session_id": response.session_id, |
| 131 | + "model": response.model, |
| 132 | + "content_type": response.content_type, |
| 133 | + } |
| 134 | + for key, value in response_metadata.items(): |
| 135 | + if value is not None: |
| 136 | + set_span_attribute(span, f"agno.agent.{key}", str(value)) |
| 137 | + |
| 138 | + if response.content: |
| 139 | + accumulated_content += response.content |
| 140 | + set_span_attribute(span, "agno.agent.response", accumulated_content) |
| 141 | + |
| 142 | + if response.messages: |
| 143 | + for msg in response.messages: |
| 144 | + if msg.tool_calls: |
| 145 | + for tool_call in msg.tool_calls: |
| 146 | + tool_id = tool_call.get('id') |
| 147 | + if tool_id and tool_id not in seen_tool_calls: |
| 148 | + seen_tool_calls.add(tool_id) |
| 149 | + tool_info = { |
| 150 | + 'id': tool_id, |
| 151 | + 'name': tool_call.get('function', {}).get('name'), |
| 152 | + 'arguments': tool_call.get('function', {}).get('arguments'), |
| 153 | + 'start_time': msg.created_at, |
| 154 | + } |
| 155 | + current_tool_call = tool_info |
| 156 | + set_span_attribute(span, f"agno.agent.tool_call.{tool_id}", json.dumps(tool_info)) |
| 157 | + |
| 158 | + if msg.metrics: |
| 159 | + metrics = _extract_metrics(msg.metrics) |
| 160 | + role_prefix = f"agno.agent.metrics.{msg.role}" |
| 161 | + for key, value in metrics.items(): |
| 162 | + set_span_attribute(span, f"{role_prefix}.{key}", str(value)) |
| 163 | + |
| 164 | + if response.tools: |
| 165 | + for tool in response.tools: |
| 166 | + tool_id = tool.get('tool_call_id') |
| 167 | + if tool_id and current_tool_call and current_tool_call['id'] == tool_id: |
| 168 | + tool_result = { |
| 169 | + **current_tool_call, |
| 170 | + 'result': tool.get('content'), |
| 171 | + 'error': tool.get('tool_call_error'), |
| 172 | + 'end_time': tool.get('created_at'), |
| 173 | + 'metrics': tool.get('metrics'), |
| 174 | + } |
| 175 | + set_span_attribute(span, f"agno.agent.tool_call.{tool_id}", json.dumps(tool_result)) |
| 176 | + current_tool_call = None |
| 177 | + |
| 178 | + yield response |
| 179 | + |
| 180 | + except Exception as err: |
| 181 | + span.record_exception(err) |
| 182 | + span.set_status(Status(StatusCode.ERROR, str(err))) |
| 183 | + raise |
| 184 | + finally: |
| 185 | + span.set_status(Status(StatusCode.OK)) |
| 186 | + if len(seen_tool_calls) > 0: |
| 187 | + span.set_attribute("agno.agent.total_tool_calls", len(seen_tool_calls)) |
| 188 | + except Exception as err: |
| 189 | + span.record_exception(err) |
| 190 | + span.set_status(Status(StatusCode.ERROR, str(err))) |
| 191 | + raise |
| 192 | + |
| 193 | + return traced_method |
| 194 | + |
| 195 | +class AgnoSpanAttributes: |
| 196 | + span: Span |
| 197 | + agent_data: dict |
| 198 | + |
| 199 | + def __init__(self, span: Span, instance) -> None: |
| 200 | + self.span = span |
| 201 | + self.instance = instance |
| 202 | + self.agent_data = { |
| 203 | + "memory": {}, |
| 204 | + "model": {}, |
| 205 | + "tools": [], |
| 206 | + } |
| 207 | + |
| 208 | + self.run() |
| 209 | + |
| 210 | + def run(self): |
| 211 | + instance_attrs = { |
| 212 | + "agent_id": self.instance.agent_id, |
| 213 | + "session_id": self.instance.session_id, |
| 214 | + "name": self.instance.name, |
| 215 | + "markdown": self.instance.markdown, |
| 216 | + "reasoning": self.instance.reasoning, |
| 217 | + "add_references": self.instance.add_references, |
| 218 | + "show_tool_calls": self.instance.show_tool_calls, |
| 219 | + "stream": self.instance.stream, |
| 220 | + "stream_intermediate_steps": self.instance.stream_intermediate_steps, |
| 221 | + } |
| 222 | + |
| 223 | + for key, value in instance_attrs.items(): |
| 224 | + if value is not None: |
| 225 | + set_span_attribute(self.span, f"agno.agent.{key}", str(value)) |
| 226 | + |
| 227 | + if self.instance.model: |
| 228 | + model_attrs = { |
| 229 | + "id": self.instance.model.id, |
| 230 | + "name": self.instance.model.name, |
| 231 | + "provider": self.instance.model.provider, |
| 232 | + "structured_outputs": self.instance.model.structured_outputs, |
| 233 | + "supports_structured_outputs": self.instance.model.supports_structured_outputs, |
| 234 | + } |
| 235 | + for key, value in model_attrs.items(): |
| 236 | + if value is not None: |
| 237 | + set_span_attribute(self.span, f"agno.agent.model.{key}", str(value)) |
| 238 | + |
| 239 | + if hasattr(self.instance.model, 'metrics') and self.instance.model.metrics: |
| 240 | + metrics = _extract_metrics(self.instance.model.metrics) |
| 241 | + set_span_attribute(self.span, "agno.agent.model.metrics", json.dumps(metrics)) |
| 242 | + |
| 243 | + if self.instance.tools: |
| 244 | + tool_list = [] |
| 245 | + for tool in self.instance.tools: |
| 246 | + if hasattr(tool, "name"): |
| 247 | + tool_list.append(tool.name) |
| 248 | + elif hasattr(tool, "__name__"): |
| 249 | + tool_list.append(tool.__name__) |
| 250 | + set_span_attribute(self.span, "agno.agent.tools", str(tool_list)) |
| 251 | + |
| 252 | + if self.instance.memory: |
| 253 | + memory_attrs = { |
| 254 | + "create_session_summary": self.instance.memory.create_session_summary, |
| 255 | + "create_user_memories": self.instance.memory.create_user_memories, |
| 256 | + "update_session_summary_after_run": self.instance.memory.update_session_summary_after_run, |
| 257 | + "update_user_memories_after_run": self.instance.memory.update_user_memories_after_run, |
| 258 | + } |
| 259 | + for key, value in memory_attrs.items(): |
| 260 | + if value is not None: |
| 261 | + set_span_attribute(self.span, f"agno.agent.memory.{key}", str(value)) |
0 commit comments