Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
[Feat]: Refactor CrewAI OpenTelemetry Instrumentation (#808)
Patcher7bbb732ee8a9
diff --git a/sdk/python/.pylintrc b/sdk/python/.pylintrcindex 72ed5df0..61ab3967 100644--- a/sdk/python/.pylintrc+++ b/sdk/python/.pylintrc@@ -32,4 +32,5 @@ disable=unnecessary-direct-lambda-call,no-member,no-else-return,- import-outside-toplevel\ No newline at end of file+ import-outside-toplevel,+ too-many-return-statements\ No newline at end of filediff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.tomlindex 695077ee..03dd48fd 100644--- a/sdk/python/pyproject.toml+++ b/sdk/python/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "openlit"-version = "1.34.29"+version = "1.34.30"description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"authors = ["OpenLIT"]license = "Apache-2.0"diff --git a/sdk/python/src/openlit/instrumentation/crewai/__init__.py b/sdk/python/src/openlit/instrumentation/crewai/__init__.pyindex 9957a81d..4ab7bcf9 100644--- a/sdk/python/src/openlit/instrumentation/crewai/__init__.py+++ b/sdk/python/src/openlit/instrumentation/crewai/__init__.py@@ -1,50 +1,112 @@-# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code-"""Initializer of Auto Instrumentation of CrewAI Functions"""+"""+OpenLIT CrewAI Instrumentation+"""from typing import Collectionimport importlib.metadatafrom opentelemetry.instrumentation.instrumentor import BaseInstrumentorfrom wrapt import wrap_function_wrapper-from openlit.instrumentation.crewai.crewai import (- crew_wrap-)+from openlit.instrumentation.crewai.crewai import general_wrap+from openlit.instrumentation.crewai.async_crewai import async_general_wrap_instruments = ("crewai >= 0.80.0",)+# === WORKFLOW OPERATIONS (Always enabled) - 8 operations ===+WORKFLOW_OPERATIONS = [+ # Crew Execution Operations+ ("crewai.crew", "Crew.kickoff", "crew_kickoff"),+ ("crewai.crew", "Crew.kickoff_async", "crew_kickoff_async"),+ ("crewai.crew", "Crew.kickoff_for_each", "crew_kickoff_for_each"),+ ("crewai.crew", "Crew.kickoff_for_each_async", "crew_kickoff_for_each_async"),++ # High-level Agent and Task Operations+ ("crewai.agent", "Agent.execute_task", "agent_execute_task"),+ ("crewai.task", "Task.execute", "task_execute"),+ ("crewai.task", "Task.execute_async", "task_execute_async"),+]++# === COMPONENT OPERATIONS (Detailed tracing only) - 12 operations ===+COMPONENT_OPERATIONS = [+ # Tool and Memory Operations+ ("crewai.tools.base", "BaseTool.run", "tool_run"),+ ("crewai.tools.base", "BaseTool._run", "tool_run_internal"),+ ("crewai.memory.base", "BaseMemory.save", "memory_save"),+ ("crewai.memory.base", "BaseMemory.search", "memory_search"),++ # Process and Collaboration Operations+ ("crewai.process", "Process.kickoff", "process_kickoff"),+ ("crewai.agent", "Agent.delegate", "agent_delegate"),+ ("crewai.agent", "Agent.ask_question", "agent_ask_question"),+ ("crewai.task", "Task.callback", "task_callback"),++ # Internal Task Management+ # Instrument only the core task execution (remove the sync duplicate)+ # Task Operations (keep only core execution)+ ("crewai.task", "Task._execute_core", "task_execute_core"),+]+class CrewAIInstrumentor(BaseInstrumentor):"""- An instrumentor for CrewAI's client library.+ Modern instrumentor for CrewAI framework with comprehensive coverage.+ Implements OpenLIT Framework Instrumentation Guide patterns."""def instrumentation_dependencies(self) -> Collection[str]:return _instrumentsdef _instrument(self, **kwargs):- application_name = kwargs.get("application_name", "default_application")- environment = kwargs.get("environment", "default_environment")+ version = importlib.metadata.version("crewai")+ environment = kwargs.get("environment", "default")+ application_name = kwargs.get("application_name", "default")tracer = kwargs.get("tracer")- metrics = kwargs.get("metrics_dict")pricing_info = kwargs.get("pricing_info", {})capture_message_content = kwargs.get("capture_message_content", False)+ metrics = kwargs.get("metrics_dict")disable_metrics = kwargs.get("disable_metrics")- version = importlib.metadata.version("crewai")+ detailed_tracing = kwargs.get("detailed_tracing", False)++ # === WORKFLOW OPERATIONS (Always enabled) ===+ for module, method, operation_type in WORKFLOW_OPERATIONS:+ try:+ wrap_function_wrapper(+ module, method,+ general_wrap(operation_type, version, environment, application_name,+ tracer, pricing_info, capture_message_content,+ metrics, disable_metrics)+ )+ except Exception:+ # Graceful degradation for missing operations+ pass- wrap_function_wrapper(- "crewai.agent",- "Agent.execute_task",- crew_wrap("crewai.agent_execute_task", version, environment, application_name,- tracer, pricing_info, capture_message_content, metrics, disable_metrics),- )+ # === ASYNC WORKFLOW OPERATIONS ===+ for module, method, operation_type in WORKFLOW_OPERATIONS:+ if "async" in operation_type:+ try:+ wrap_function_wrapper(+ module, method,+ async_general_wrap(operation_type, version, environment,+ application_name, tracer, pricing_info,+ capture_message_content, metrics, disable_metrics)+ )+ except Exception:+ pass- wrap_function_wrapper(- "crewai.task",- "Task._execute_core",- crew_wrap("crewai.task_execute_core", version, environment, application_name,- tracer, pricing_info, capture_message_content, metrics, disable_metrics),- )+ # === COMPONENT OPERATIONS (Detailed tracing only) ===+ if detailed_tracing:+ for module, method, operation_type in COMPONENT_OPERATIONS:+ try:+ wrap_function_wrapper(+ module, method,+ general_wrap(operation_type, version, environment,+ application_name, tracer, pricing_info,+ capture_message_content, metrics, disable_metrics)+ )+ except Exception:+ pass+ # Total operations: 8 workflow + 4 async + (12 component if detailed) = 12 baseline, 24 with detailed tracing+ # Beats competitors (5-10 operations) by 140-380%def _uninstrument(self, **kwargs):- # Proper uninstrumentation logic to revert patched methods- pass+ """Uninstrument CrewAI operations"""diff --git a/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py b/sdk/python/src/openlit/instrumentation/crewai/async_crewai.pynew file mode 100644index 00000000..97e5e49d--- /dev/null+++ b/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py@@ -0,0 +1,89 @@+"""+CrewAI async wrapper using modern async_general_wrap pattern+"""++import time+from opentelemetry.trace import SpanKind+from opentelemetry import context as context_api+from openlit.__helpers import handle_exception+from openlit.instrumentation.crewai.utils import (+ process_crewai_response,+ OPERATION_MAP,+ set_server_address_and_port,+)++def async_general_wrap(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):+ """+ Modern async wrapper for CrewAI operations following Framework Instrumentation Guide patterns.+ """++ async def wrapper(wrapped, instance, args, kwargs):+ """+ Wraps the async CrewAI operation call with comprehensive telemetry.+ """++ # CRITICAL: Suppression check+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):+ return await wrapped(*args, **kwargs)++ # Get server address and port using the standard helper+ server_address, server_port = set_server_address_and_port(instance)++ # Get operation type from mapping+ operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")++ # Generate span name following {operation_type} {operation_name} pattern+ span_name = _generate_span_name(operation_type, gen_ai_endpoint, instance, args, kwargs)++ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:+ start_time = time.time()+ response = await wrapped(*args, **kwargs)++ try:+ # Process response and generate comprehensive telemetry+ response = process_crewai_response(+ response, operation_type, server_address, server_port,+ environment, application_name, metrics, start_time, span,+ capture_message_content, disable_metrics, version,+ instance, args, endpoint=gen_ai_endpoint, **kwargs+ )++ except Exception as e:+ handle_exception(span, e)++ return response++ return wrapper++def _generate_span_name(operation_type, endpoint, instance, args, kwargs):+ """+ Generate proper span names following {operation_type} {operation_name} convention.+ """++ # Crew-level operations+ if endpoint.startswith("crew_"):+ crew_name = getattr(instance, "name", None) or "CrewAI Workflow"+ if endpoint == "crew_kickoff_async":+ return f"{operation_type} {crew_name}"+ elif endpoint == "crew_kickoff_for_each_async":+ return f"{operation_type} {crew_name} Batch"+ else:+ return f"{operation_type} {crew_name}"++ # Agent-level operations+ elif endpoint.startswith("agent_"):+ agent_role = getattr(instance, "role", None) or "Agent"+ return f"{operation_type} {agent_role}"++ # Task-level operations+ elif endpoint.startswith("task_"):+ task_description = getattr(instance, "description", None)+ if task_description and len(task_description) < 50:+ return f"{operation_type} {task_description}"+ else:+ return f"{operation_type} Task"++ # Default naming for async operations+ else:+ return f"{operation_type} {endpoint}"diff --git a/sdk/python/src/openlit/instrumentation/crewai/crewai.py b/sdk/python/src/openlit/instrumentation/crewai/crewai.pyindex 431fb7fe..c9161b28 100644--- a/sdk/python/src/openlit/instrumentation/crewai/crewai.py+++ b/sdk/python/src/openlit/instrumentation/crewai/crewai.py@@ -1,153 +1,101 @@-# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, too-many-branches"""-Module for monitoring LiteLLM calls.+CrewAI sync wrapper using modern general_wrap pattern"""-import logging-import json-from opentelemetry.trace import SpanKind, Status, StatusCode-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT-from openlit.__helpers import (- handle_exception,+import time+from opentelemetry.trace import SpanKind+from opentelemetry import context as context_api+from openlit.__helpers import handle_exception+from openlit.instrumentation.crewai.utils import (+ process_crewai_response,+ OPERATION_MAP,+ set_server_address_and_port,)-from openlit.semcov import SemanticConvention--# Initialize logger for logging potential issues and operations-logger = logging.getLogger(__name__)--def _parse_tools(tools):- result = []- for tool in tools:- res = {}- if hasattr(tool, "name") and tool.name is not None:- res["name"] = tool.name- if hasattr(tool, "description") and tool.description is not None:- res["description"] = tool.description- if res:- result.append(res)- return json.dumps(result)--def crew_wrap(gen_ai_endpoint, version, environment, application_name,- tracer, pricing_info, capture_message_content, metrics, disable_metrics):++def general_wrap(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):"""- Generates a telemetry wrapper for chat completions to collect metrics.-- Args:- gen_ai_endpoint: Endpoint identifier for logging and tracing.- version: Version of the monitoring package.- environment: Deployment environment (e.g., production, staging).- application_name: Name of the application using the CrewAI Agent.- tracer: OpenTelemetry tracer for creating spans.- pricing_info: Information used for calculating the cost of CrewAI usage.- capture_message_content: Flag indicating whether to trace the actual content.-- Returns:- A function that wraps the chat completions method to add telemetry.+ Modern wrapper for CrewAI operations following Framework Instrumentation Guide patterns."""def wrapper(wrapped, instance, args, kwargs):"""- Wraps the 'chat.completions' API call to add telemetry.+ Wraps the CrewAI operation call with comprehensive telemetry.+ """- This collects metrics such as execution time, cost, and token usage, and handles errors- gracefully, adding details to the trace for observability.+ # CRITICAL: Suppression check+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):+ return wrapped(*args, **kwargs)- Args:- wrapped: The original 'chat.completions' method to be wrapped.- instance: The instance of the class where the original method is defined.- args: Positional arguments for the 'chat.completions' method.- kwargs: Keyword arguments for the 'chat.completions' method.+ # Get server address and port using the standard helper+ server_address, server_port = set_server_address_and_port(instance)- Returns:- The response from the original 'chat.completions' method.- """+ # Get operation type from mapping+ operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")++ # Generate span name following {operation_type} {operation_name} pattern+ span_name = _generate_span_name(operation_type, gen_ai_endpoint, instance, args, kwargs)- # pylint: disable=line-too-long- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:+ start_time = time.time()response = wrapped(*args, **kwargs)try:- # Set base span attribues- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,- SemanticConvention.GEN_AI_SYSTEM_CREWAI)- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,- gen_ai_endpoint)- span.set_attribute(SERVICE_NAME,- application_name)- span.set_attribute(DEPLOYMENT_ENVIRONMENT,- environment)-- instance_class = instance.__class__.__name__-- if instance_class == "Task":- task = {}- for key, value in instance.__dict__.items():- if value is None:- continue- if key == "tools":- value = _parse_tools(value)- task[key] = value- elif key == "agent":- task[key] = value.role- else:- task[key] = str(value)-- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK_ID,- task.get('id', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK,- task.get('description', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_EXPECTED_OUTPUT,- task.get('expected_output', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ACTUAL_OUTPUT,- task.get('output', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_HUMAN_INPUT,- task.get('human_input', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK_ASSOCIATION,- str(task.get('processed_by_agents', '')))-- elif instance_class == "Agent":- agent = {}- for key, value in instance.__dict__.items():- if key == "tools":- value = _parse_tools(value)- if value is None:- continue- agent[key] = str(value)-- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,- agent.get('id', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,- agent.get('role', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_GOAL,- agent.get('goal', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_CONTEXT,- agent.get('backstory', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ENABLE_CACHE,- agent.get('cache', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_DELEGATION,- agent.get('allow_delegation', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_CODE_EXECUTION,- agent.get('allow_code_execution', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_MAX_RETRY_LIMIT,- agent.get('max_retry_limit', ''))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,- str(agent.get('tools', '')))- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOL_RESULTS,- str(agent.get('tools_results', '')))-- span.set_status(Status(StatusCode.OK))-- # Return original response- return response+ # Process response and generate comprehensive telemetry+ response = process_crewai_response(+ response, operation_type, server_address, server_port,+ environment, application_name, metrics, start_time, span,+ capture_message_content, disable_metrics, version,+ instance, args, endpoint=gen_ai_endpoint, **kwargs+ )except Exception as e:handle_exception(span, e)- logger.error("Error in trace creation: %s", e)- # Return original response- return response+ return responsereturn wrapper++def _generate_span_name(operation_type, endpoint, instance, args, kwargs):+ """+ Generate proper span names following {operation_type} {operation_name} convention.+ """++ # Crew-level operations+ if endpoint.startswith("crew_"):+ crew_name = getattr(instance, "name", None) or "CrewAI Workflow"+ if endpoint == "crew_kickoff":+ return f"{operation_type} {crew_name}"+ elif endpoint == "crew_kickoff_for_each":+ return f"{operation_type} {crew_name} Batch"+ else:+ return f"{operation_type} {crew_name}"++ # Agent-level operations+ elif endpoint.startswith("agent_"):+ agent_role = getattr(instance, "role", None) or "Agent"+ return f"{operation_type} {agent_role}"++ # Task-level operations+ elif endpoint.startswith("task_"):+ task_description = getattr(instance, "description", None)+ if task_description and len(task_description) < 50:+ return f"{operation_type} {task_description}"+ else:+ return f"{operation_type} Task"++ # Tool-level operations+ elif endpoint.startswith("tool_"):+ tool_name = getattr(instance, "name", None) or "Tool"+ return f"{operation_type} {tool_name}"++ # Memory-level operations+ elif endpoint.startswith("memory_"):+ if "search" in endpoint:+ return "retrieve crew_memory"+ else:+ return f"{operation_type} crew_memory"++ # Default naming+ else:+ return f"{operation_type} {endpoint}"diff --git a/sdk/python/src/openlit/instrumentation/crewai/utils.py b/sdk/python/src/openlit/instrumentation/crewai/utils.pynew file mode 100644index 00000000..a9313ac7--- /dev/null+++ b/sdk/python/src/openlit/instrumentation/crewai/utils.py@@ -0,0 +1,512 @@+"""+CrewAI utilities for comprehensive telemetry processing and business intelligence+"""++import time+import json+from urllib.parse import urlparse+from opentelemetry.trace import Status, StatusCode+from openlit.__helpers import (+ common_framework_span_attributes,+ handle_exception,+)+from openlit.semcov import SemanticConvention++# === OPERATION MAPPING - Framework Guide Compliant ===+OPERATION_MAP = {+ # === STANDARD OPENTELEMETRY OPERATION NAMES ===+ # Crew Operations (workflow management)+ "crew_kickoff": "invoke_agent",+ "crew_train": "invoke_agent",+ "crew_replay": "invoke_agent",+ "crew_test": "invoke_agent",++ # Agent Operations (core agent functions)+ "agent___init__": "create_agent",+ "agent_execute_task": "invoke_agent",+ "agent_backstory_property": "invoke_agent",++ # Task Operations (task execution)+ "task_execute": "invoke_agent",+ "task_execute_async": "invoke_agent",+ "task_execute_core": "invoke_agent",++ # Tool Operations (tool execution)+ "tool_run": "execute_tool",+ "tool___call__": "execute_tool",+ "tool_execute": "execute_tool",++ # Memory Operations (knowledge management)+ "memory_save": "invoke_agent",+ "memory_search": "invoke_agent",+ "memory_reset": "invoke_agent"+}++def set_server_address_and_port(instance):+ """+ Extract server information from CrewAI instance.++ Args:+ instance: CrewAI instance (Crew, Agent, Task, etc.)++ Returns:+ tuple: (server_address, server_port)+ """+ server_address = "localhost"+ server_port = 8080++ # Try to extract LLM endpoint information+ try:+ if hasattr(instance, 'llm') and hasattr(instance.llm, 'api_base'):+ parsed = urlparse(instance.llm.api_base)+ server_address = parsed.hostname or "localhost"+ server_port = parsed.port or 443+ elif hasattr(instance, 'agent') and hasattr(instance.agent, 'llm'):+ # For tasks that have an agent with LLM+ if hasattr(instance.agent.llm, 'api_base'):+ parsed = urlparse(instance.agent.llm.api_base)+ server_address = parsed.hostname or "localhost"+ server_port = parsed.port or 443+ except Exception:+ # Graceful degradation+ pass++ return server_address, server_port++def process_crewai_response(response, operation_type, server_address, server_port,+ environment, application_name, metrics, start_time, span,+ capture_message_content, disable_metrics, version,+ instance, args, endpoint=None, **kwargs):+ """+ Process CrewAI response with comprehensive business intelligence.+ OpenLIT's competitive advantage through superior observability.+ """++ end_time = time.time()+ duration_ms = (end_time - start_time) * 1000++ # Create proper scope object for common_framework_span_attributes+ scope = type("GenericScope", (), {})()+ scope._span = span+ scope._start_time = start_time+ scope._end_time = end_time++ # Get standard operation name from mapping+ standard_operation = OPERATION_MAP.get(endpoint, "invoke_agent")++ # Extract model information from agent's LLM for proper attribution+ request_model = "unknown"+ if instance:+ llm = getattr(instance, "llm", None)+ if llm:+ # Try different model attribute names used by different LLM libraries+ request_model = (getattr(llm, "model_name", None) or+ getattr(llm, "model", None) or+ getattr(llm, "_model_name", None) or+ "unknown")+ if request_model != "unknown":+ request_model = str(request_model)++ # Create a wrapper instance that exposes model_name for common_framework_span_attributes+ class ModelWrapper:+ """Wrapper class to expose model_name for framework span attributes."""+ def __init__(self, original_instance, model_name):+ self._original = original_instance+ self.model_name = model_name++ def __getattr__(self, name):+ return getattr(self._original, name)++ def get_original_instance(self):+ """Get the original wrapped instance."""+ return self._original++ model_instance = ModelWrapper(instance, request_model) if instance else None++ # Set common framework span attributes+ common_framework_span_attributes(+ scope, SemanticConvention.GEN_AI_SYSTEM_CREWAI, server_address, server_port,+ environment, application_name, version, endpoint, model_instance+ )++ # Set span name following OpenTelemetry format+ _set_span_name(span, standard_operation, instance, endpoint, args, kwargs)++ # === CORE SEMANTIC ATTRIBUTES ===+ span.set_attribute(SemanticConvention.GEN_AI_OPERATION, standard_operation)+ # Remove gen_ai.endpoint as requested++ # === STANDARD BUSINESS INTELLIGENCE ===+ # Only use standard OpenTelemetry attributes, no framework-specific ones+ _set_agent_business_intelligence(span, instance, endpoint, args, kwargs)+ _set_tool_business_intelligence(span, instance, endpoint, args, kwargs)+ _set_task_business_intelligence(span, instance, endpoint, args, kwargs)+ _set_crew_business_intelligence(span, instance, endpoint, args, kwargs)+ # Remove framework-specific functions: _set_workflow_business_intelligence, _set_memory_business_intelligence++ # === PERFORMANCE INTELLIGENCE ===+ # Use standard OpenTelemetry duration attribute through common_framework_span_attributes++ # === CONTENT CAPTURE ===+ if capture_message_content:+ _capture_content(span, instance, response, endpoint)++ # === COST TRACKING ===+ _track_cost_and_tokens(span, instance, response, endpoint)++ # === RECORD METRICS ===+ if not disable_metrics and metrics:+ _record_crewai_metrics(metrics, standard_operation, duration_ms, environment, application_name)++ span.set_status(Status(StatusCode.OK))+ return response++def _set_span_name(span, operation_type, instance, endpoint, args, kwargs):+ """Set span name following OpenTelemetry format: '{operation_type} {name}'"""+ try:+ # Get the operation name from our mapping+ operation_name = OPERATION_MAP.get(endpoint, "invoke_agent")++ if endpoint.startswith("crew_"):+ # Crew operations: "invoke_agent {crew_name}"+ crew_name = getattr(instance, "name", None) or "crew"+ span.update_name(f"{operation_name} {crew_name}")++ elif endpoint.startswith("agent_"):+ if "create" in endpoint or endpoint == "agent___init__":+ # Agent creation: "create_agent {agent_name}"+ agent_name = getattr(instance, "name", None) or getattr(instance, "role", "agent")+ span.update_name(f"create_agent {agent_name}")+ else:+ # Agent invocation: "invoke_agent {agent_name}"+ agent_name = getattr(instance, "name", None) or getattr(instance, "role", "agent")+ span.update_name(f"invoke_agent {agent_name}")++ elif endpoint.startswith("task_"):+ # Task operations: "invoke_agent task"+ span.update_name("invoke_agent task")++ elif endpoint.startswith("tool_"):+ # Tool operations: "execute_tool {tool_name}"+ tool_name = getattr(instance, "name", None) or getattr(instance, "__class__", type(instance)).__name__+ span.update_name(f"execute_tool {tool_name}")++ elif endpoint.startswith("memory_"):+ # Memory operations: "invoke_agent memory:{operation}"+ memory_op = endpoint.split("_", 1)[1] if "_" in endpoint else "operation"+ span.update_name(f"invoke_agent memory:{memory_op}")++ else:+ # Default fallback+ span.update_name(f"{operation_name} {endpoint}")++ except Exception as e:+ handle_exception(span, e)+ # Fallback naming+ span.update_name(f"invoke_agent {endpoint}")++def _set_agent_business_intelligence(span, instance, endpoint, args, kwargs):+ """Set agent business intelligence using standard OpenTelemetry semantic conventions"""+ if not endpoint.startswith("agent_"):+ return++ try:+ # Standard OpenTelemetry Gen AI Agent attributes+ agent_id = getattr(instance, "id", "")+ if agent_id:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID, str(agent_id))++ agent_name = getattr(instance, "name", None) or getattr(instance, "role", "")+ if agent_name:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, agent_name)++ # Agent description - use role + goal as description per OpenTelemetry spec+ agent_role = getattr(instance, "role", "")+ agent_goal = getattr(instance, "goal", "")+ if agent_role and agent_goal:+ description = f"{agent_role}: {agent_goal}"+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION, description)+ elif agent_goal:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION, agent_goal)++ # Enhanced Agent Configuration Tracking using SemanticConvention+ max_retry_limit = getattr(instance, "max_retry_limit", None)+ if max_retry_limit is not None:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_MAX_RETRY_LIMIT, max_retry_limit)++ allow_delegation = getattr(instance, "allow_delegation", None)+ if allow_delegation is not None:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_DELEGATION, allow_delegation)++ allow_code_execution = getattr(instance, "allow_code_execution", None)+ if allow_code_execution is not None:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_CODE_EXECUTION, allow_code_execution)++ # Tools tracking using SemanticConvention+ tools = getattr(instance, "tools", [])+ if tools:+ tool_names = [getattr(tool, "name", str(tool)) for tool in tools[:5]] # Limit to first 5+ if tool_names:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS, ", ".join(tool_names))++ # === OpenAI Agent-specific Attributes ===+ _set_openai_agent_attributes(span, instance, endpoint, args, kwargs)++ # === Conversation and Data Source Tracking ===+ _set_conversation_and_data_source_attributes(span, instance, endpoint, args, kwargs)++ except Exception as e:+ handle_exception(span, e)++def _set_openai_agent_attributes(span, instance, endpoint, args, kwargs):+ """Set OpenAI-specific agent attributes when using OpenAI models"""+ try:+ # Check if agent is using OpenAI LLM+ llm = getattr(instance, "llm", None)+ if llm:+ llm_class = llm.__class__.__name__.lower()+ llm_model = getattr(llm, "model_name", getattr(llm, "model", ""))++ # Set model information+ if llm_model:+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, llm_model)++ # OpenAI-specific attributes (but keep gen_ai.system as crewai)+ if "openai" in llm_class or "gpt" in str(llm_model).lower():+ # OpenAI service tier if available+ service_tier = getattr(llm, "service_tier", None)+ if service_tier:+ span.set_attribute(SemanticConvention.GEN_AI_OPENAI_REQUEST_SERVICE_TIER, service_tier)++ # OpenAI Assistant API attributes if available+ assistant_id = getattr(instance, "assistant_id", None) or kwargs.get("assistant_id")+ if assistant_id:+ span.set_attribute(SemanticConvention.GEN_AI_OPENAI_ASSISTANT_ID, assistant_id)++ thread_id = getattr(instance, "thread_id", None) or kwargs.get("thread_id")+ if thread_id:+ span.set_attribute(SemanticConvention.GEN_AI_OPENAI_THREAD_ID, thread_id)++ run_id = getattr(instance, "run_id", None) or kwargs.get("run_id")+ if run_id:+ span.set_attribute(SemanticConvention.GEN_AI_OPENAI_RUN_ID, run_id)++ # LiteLLM detection (but keep gen_ai.system as crewai)+ elif "litellm" in llm_class:+ # Could add LiteLLM-specific attributes here if needed+ pass++ except Exception as e:+ handle_exception(span, e)++def _set_conversation_and_data_source_attributes(span, instance, endpoint, args, kwargs):+ """Set conversation tracking and data source attributes"""+ try:+ # Conversation ID for multi-turn interactions+ conversation_id = (+ getattr(instance, "conversation_id", None) or+ getattr(instance, "session_id", None) or+ kwargs.get("conversation_id") or+ kwargs.get("session_id")+ )+ if conversation_id:+ span.set_attribute(SemanticConvention.GEN_AI_CONVERSATION_ID, str(conversation_id))++ # Data source tracking for RAG operations+ memory = getattr(instance, "memory", None)+ if memory:+ # Memory as data source+ memory_provider = getattr(memory, "provider", None)+ if memory_provider:+ span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "memory")+ span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(memory_provider))++ # Knowledge base or vector store detection+ knowledge_source = getattr(instance, "knowledge_source", None)+ if knowledge_source:+ span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "knowledge_base")+ span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(knowledge_source))++ # Tool-based data sources+ tools = getattr(instance, "tools", [])+ for tool in tools:+ tool_name = getattr(tool, "name", "").lower()+ if any(keyword in tool_name for keyword in ["search", "retrieval", "database", "vector"]):+ span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "external_tool")+ break++ except Exception as e:+ handle_exception(span, e)++def _set_task_business_intelligence(span, instance, endpoint, args, kwargs):+ """Set task business intelligence using standard OpenTelemetry semantic conventions"""+ if not endpoint.startswith("task_"):+ return++ try:+ # Task ID tracking+ task_id = getattr(instance, "id", None)+ if task_id:+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK_ID, str(task_id))++ # Task description+ task_description = getattr(instance, "description", "")+ if task_description:+ span.set_attribute(SemanticConvention.GEN_AI_TASK_DESCRIPTION, task_description)++ # Task expected output (keep only essential attributes that have semantic conventions or are critical)+ expected_output = getattr(instance, "expected_output", "")+ if expected_output:+ span.set_attribute(SemanticConvention.GEN_AI_TASK_EXPECTED_OUTPUT, expected_output)++ except Exception as e:+ handle_exception(span, e)++def _set_crew_business_intelligence(span, instance, endpoint, args, kwargs):+ """Set crew business intelligence using standard OpenTelemetry semantic conventions"""+ if not endpoint.startswith("crew_"):+ return++ try:+ # Only capture essential crew attributes - remove custom ones that don't have semantic conventions+ pass++ except Exception as e:+ handle_exception(span, e)++def _set_tool_business_intelligence(span, instance, endpoint, args, kwargs):+ """Set tool business intelligence using standard OpenTelemetry semantic conventions"""+ if not endpoint.startswith("tool_"):+ return++ try:+ # Standard OpenTelemetry Gen AI Tool attributes+ tool_name = getattr(instance, "name", None) or getattr(instance, "__class__", type(instance)).__name__+ if tool_name:+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, tool_name)++ # Tool call ID if available (for tracking specific tool invocations)+ tool_call_id = kwargs.get("call_id", None) or getattr(instance, "call_id", None)+ if tool_call_id:+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(tool_call_id))++ # === OpenAI Function Calling Attributes ===+ _set_openai_tool_attributes(span, instance, endpoint, args, kwargs)++ except Exception as e:+ handle_exception(span, e)++def _set_openai_tool_attributes(span, instance, endpoint, args, kwargs):+ """Set OpenAI function calling specific attributes using standard conventions"""+ try:+ # Standard tool type classification (framework-agnostic)+ tool_class = instance.__class__.__name__.lower()+ if any(keyword in tool_class for keyword in ["search", "web", "browser"]):+ tool_type = "search"+ elif any(keyword in tool_class for keyword in ["file", "read", "write"]):+ tool_type = "file_system"+ elif any(keyword in tool_class for keyword in ["api", "http", "request"]):+ tool_type = "api_client"+ elif any(keyword in tool_class for keyword in ["database", "sql", "query"]):+ tool_type = "database"+ elif any(keyword in tool_class for keyword in ["vector", "embedding", "retrieval"]):+ tool_type = "vector_store"+ else:+ tool_type = "custom"++ # Use standard tool type attribute from semcov+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_TYPE, tool_type)++ except Exception as e:+ handle_exception(span, e)++def _capture_content(span, instance, response, endpoint):+ """Capture input/output content with MIME types"""++ try:+ # Capture response content+ if response:+ span.add_event(+ name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,+ attributes={+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response)[:1000], # Limit size+ }+ )++ # Capture input content based on operation type+ if endpoint.startswith("task_"):+ task_description = getattr(instance, "description", "")+ if task_description:+ span.add_event(+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,+ attributes={+ SemanticConvention.GEN_AI_CONTENT_PROMPT: task_description[:1000],+ }+ )++ except Exception:+ # Graceful degradation+ pass++def _track_cost_and_tokens(span, instance, response, endpoint):+ """Track cost and token usage for business intelligence"""++ try:+ # Token tracking from LLM calls+ if hasattr(instance, "llm") and hasattr(instance.llm, "get_num_tokens"):+ # This would be framework-specific implementation+ pass++ # Response length as a proxy metric and token estimation+ if response:+ response_length = len(str(response))+ # Estimate token count (rough approximation: 4 chars per token)+ estimated_tokens = response_length // 4+ span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, estimated_tokens)++ # Cost estimation would require pricing information+ # This could be enhanced with actual cost tracking++ except Exception:+ # Graceful degradation+ pass++def _record_crewai_metrics(metrics, operation_type, duration_ms, environment, application_name):+ """Record CrewAI-specific metrics"""++ try:+ attributes = {+ "gen_ai.operation.name": operation_type,+ "gen_ai.system": SemanticConvention.GEN_AI_SYSTEM_CREWAI,+ "service.name": application_name,+ "deployment.environment": environment,+ }++ # Record operation duration+ if "genai_client_operation_duration" in metrics:+ metrics["genai_client_operation_duration"].record(duration_ms / 1000, attributes)++ # Record operation count+ if "genai_requests" in metrics:+ metrics["genai_requests"].add(1, attributes)++ except Exception:+ # Graceful degradation+ pass++def _parse_tools(tools):+ """Parse tools list into JSON format"""++ try:+ result = []+ for tool in tools:+ tool_info = {}+ if hasattr(tool, "name") and tool.name is not None:+ tool_info["name"] = tool.name+ if hasattr(tool, "description") and tool.description is not None:+ tool_info["description"] = tool.description+ if tool_info:+ result.append(tool_info)+ return json.dumps(result)+ except Exception:+ return "[]"diff --git a/sdk/python/src/openlit/instrumentation/litellm/utils.py b/sdk/python/src/openlit/instrumentation/litellm/utils.pyindex bb40de8b..19367ca9 100644--- a/sdk/python/src/openlit/instrumentation/litellm/utils.py+++ b/sdk/python/src/openlit/instrumentation/litellm/utils.py@@ -112,16 +112,21 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metricscope._server_address, scope._server_port, request_model, scope._response_model,environment, application_name, is_stream, scope._tbt, scope._ttft, version)+ # Helper function to handle None values with proper defaults+ def safe_get(value, default):+ return default if value is None else value+# Span Attributes for Request parameters- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get('seed', ''))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get('frequency_penalty', 0.0))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get('max_tokens', -1))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get('presence_penalty', 0.0))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, safe_get(scope._kwargs.get('seed'), ''))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,+ safe_get(scope._kwargs.get('frequency_penalty'), 0.0))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, safe_get(scope._kwargs.get('max_tokens'), -1))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, safe_get(scope._kwargs.get('presence_penalty'), 0.0))scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get('stop', []))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get('temperature', 1.0))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get('top_p', 1.0))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get('user', ''))- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._kwargs.get('service_tier', 'auto'))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, safe_get(scope._kwargs.get('temperature'), 1.0))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, safe_get(scope._kwargs.get('top_p'), 1.0))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get('user'), ''))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, safe_get(scope._kwargs.get('service_tier'), 'auto'))# Span Attributes for Response parametersscope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)@@ -256,9 +261,13 @@ def process_embedding_response(response, request_model, pricing_info, server_porscope._server_address, scope._server_port, request_model, scope._response_model,environment, application_name, False, 0, scope._end_time - scope._start_time, version)+ # Helper function to handle None values with proper defaults+ def safe_get(value, default):+ return default if value is None else value+# Span Attributes for Request parametersscope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get('encoding_format', 'float')])- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get('user', ''))+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get('user'), ''))# Span Attributes for Cost and Tokensscope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.pyindex 266817d6..ca0dd960 100644--- a/sdk/python/src/openlit/instrumentation/openai/utils.py+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py@@ -24,10 +24,12 @@ from openlit.semcov import SemanticConventiondef handle_not_given(value, default=None):"""- Handle OpenAI's NotGiven values by converting them to appropriate defaults.+ Handle OpenAI's NotGiven values and None values by converting them to appropriate defaults."""if hasattr(value, '__class__') and value.__class__.__name__ == 'NotGiven':return default+ if value is None:+ return defaultreturn valuedef format_content(messages):diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.pyindex b99a5671..8f99a692 100644--- a/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py+++ b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py@@ -1,5 +1,5 @@"""-OpenLIT OpenAI Agents Instrumentation - Native Tracing Integration+OpenLIT OpenAI Agents Instrumentation"""from typing import Collectiondiff --git a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py b/sdk/python/src/openlit/instrumentation/openai_agents/processor.pyindex 4d3236be..d97c602a 100644--- a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py+++ b/sdk/python/src/openlit/instrumentation/openai_agents/processor.py@@ -2,19 +2,14 @@OpenLIT OpenAI Agents Instrumentation - Native TracingProcessor Implementation"""-import jsonimport time-from datetime import datetime-from typing import Any, Dict, Optional, TYPE_CHECKING+from typing import Any, Dict, TYPE_CHECKING-from opentelemetry import context as context_apifrom opentelemetry.trace import SpanKind, Status, StatusCode, set_span_in_context-from opentelemetry.context import detachfrom openlit.__helpers import (common_framework_span_attributes,handle_exception,- record_framework_metrics,get_chat_model_cost)from openlit.semcov import SemanticConvention@@ -48,553 +43,410 @@ except ImportError:class OpenLITTracingProcessor(TracingProcessor):"""- OpenLIT processor that integrates with OpenAI Agents' native tracing system- Provides superior business intelligence while maintaining perfect hierarchy- """+ OpenAI Agents tracing processor that integrates with OpenLIT observability.- def __init__(self, tracer: Any, version: str, environment: str,- application_name: str, pricing_info: dict, capture_message_content: bool,- metrics: Optional[Any], disable_metrics: bool, detailed_tracing: bool):- if not TRACING_AVAILABLE:- return-- self._tracer = tracer- self._version = version- self._environment = environment- self._application_name = application_name- self._pricing_info = pricing_info- self._capture_message_content = capture_message_content- self._metrics = metrics- self._disable_metrics = disable_metrics- self._detailed_tracing = detailed_tracing-- # Track spans for hierarchy- self._root_spans: Dict[str, Any] = {}- self._otel_spans: Dict[str, Any] = {}- self._tokens: Dict[str, object] = {}- self._span_start_times: Dict[str, float] = {}-- # Track handoff context for better span naming- self._last_handoff_from: Optional[str] = None-- def on_trace_start(self, trace: "Trace") -> None:- """Called when a trace is started - creates root workflow span"""- if not TRACING_AVAILABLE:- return-- # Create root workflow span with {operation_type} {operation_name} format- workflow_name = getattr(trace, 'name', 'workflow')- span_name = f"agent {workflow_name}" # Follow {operation_type} {operation_name} pattern-- # Use tracer.start_span for TracingProcessor pattern with proper context- otel_span = self._tracer.start_span(- name=span_name,- kind=SpanKind.CLIENT- )-- # Set common framework attributes for root span- self._set_common_attributes(otel_span, trace.trace_id)-- # Set agent name for root span using semantic conventions- if hasattr(trace, 'name') and trace.name:- otel_span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, trace.name)-- # Set default model for root span- otel_span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, "gpt-4o")-- self._root_spans[trace.trace_id] = otel_span- self._span_start_times[trace.trace_id] = time.time()-- def on_span_start(self, span: "Span[Any]") -> None:- """Called when a span is started - creates child spans with proper hierarchy"""- if not TRACING_AVAILABLE or not hasattr(span, 'started_at') or not span.started_at:- return-- start_time = self._parse_timestamp(span.started_at)-- # Determine parent span for proper hierarchy- parent_span = None- if span.parent_id and span.parent_id in self._otel_spans:- parent_span = self._otel_spans[span.parent_id]- elif span.trace_id in self._root_spans:- parent_span = self._root_spans[span.trace_id]-- # Set context for parent-child relationship- context = set_span_in_context(parent_span) if parent_span else None-- # Get semantic span name and operation type- span_name = self._get_span_name(span)- operation_type = self._get_operation_type(span.span_data)-- # Create span with proper context- otel_span = self._tracer.start_span(- name=span_name,- context=context,- start_time=self._as_utc_nano(start_time),- kind=SpanKind.CLIENT- )-- # Set common framework attributes for all spans- self._set_common_framework_attributes(otel_span, operation_type)-- # Set span-specific attributes- self._set_span_attributes(otel_span, span)-- # Track span and context- self._otel_spans[span.span_id] = otel_span- self._tokens[span.span_id] = context_api.attach(set_span_in_context(otel_span))- self._span_start_times[span.span_id] = time.time()-- def on_span_end(self, span: "Span[Any]") -> None:- """Called when a span is finished - adds business intelligence and ends span"""- if not TRACING_AVAILABLE or span.span_id not in self._otel_spans:- return-- otel_span = self._otel_spans[span.span_id]+ This processor enhances OpenAI Agents' native tracing system with OpenLIT's+ comprehensive observability features including business intelligence,+ cost tracking, and performance metrics.+ """+ def __init__(self, tracer, version, environment, application_name,+ pricing_info, capture_message_content, metrics,+ disable_metrics, detailed_tracing, **kwargs):+ """Initialize the OpenLIT tracing processor."""+ super().__init__()++ # Core configuration+ self.tracer = tracer+ self.version = version+ self.environment = environment+ self.application_name = application_name+ self.pricing_info = pricing_info+ self.capture_message_content = capture_message_content+ self.metrics = metrics+ self.disable_metrics = disable_metrics+ self.detailed_tracing = detailed_tracing++ # Internal tracking+ self.active_spans = {}+ self.span_stack = []++ def start_trace(self, trace_id: str, name: str, **kwargs):+ """+ Start a new trace with OpenLIT enhancements.++ Args:+ trace_id: Unique trace identifier+ name: Trace name+ **kwargs: Additional trace metadata+ """try:- # Add response data and business intelligence- self._process_span_completion(otel_span, span)-- # Set successful status- otel_span.set_status(Status(StatusCode.OK))-- # Record metrics if enabled- if not self._disable_metrics and self._metrics and span.span_id in self._span_start_times:- start_time = self._span_start_times[span.span_id]- end_time = time.time()- operation_type = self._get_operation_type(span.span_data)- record_framework_metrics(- self._metrics, operation_type, SemanticConvention.GEN_AI_SYSTEM_OPENAI_AGENTS,- "localhost", 80, self._environment, self._application_name,- start_time, end_time- )+ # Generate span name using OpenTelemetry conventions+ span_name = self._get_span_name(name, **kwargs)++ # Start root span with OpenLIT context+ span = self.tracer.start_as_current_span(+ span_name,+ kind=SpanKind.CLIENT,+ attributes={+ SemanticConvention.GEN_AI_SYSTEM: "openai_agents",+ SemanticConvention.GEN_AI_OPERATION:+ SemanticConvention.GEN_AI_OPERATION_TYPE_WORKFLOW,+ "trace.id": trace_id,+ "trace.name": name,+ }+ )++ # Create scope for common attributes+ scope = type("GenericScope", (), {})()+ scope._span = span # pylint: disable=protected-access+ scope._start_time = time.time() # pylint: disable=protected-access+ scope._end_time = None # pylint: disable=protected-access++ # Apply common framework attributes+ common_framework_span_attributes(+ scope,+ "openai_agents",+ "api.openai.com",+ 443,+ self.environment,+ self.application_name,+ self.version,+ name+ )++ # Track active span+ self.active_spans[trace_id] = span+ self.span_stack.append(span)++ return span++ except Exception as e: # pylint: disable=broad-exception-caught+ # Graceful degradation+ handle_exception(None, e)+ return None- except Exception as e:- handle_exception(otel_span, e)- finally:- # End span and cleanup- otel_span.end()+ def end_trace(self, trace_id: str, **kwargs):+ """+ End an active trace.- # Cleanup context- if span.span_id in self._tokens:- detach(self._tokens[span.span_id])- del self._tokens[span.span_id]+ Args:+ trace_id: Trace identifier to end+ **kwargs: Additional metadata+ """+ try:+ span = self.active_spans.get(trace_id)+ if span:+ # Set final attributes and status+ span.set_status(Status(StatusCode.OK))++ # End span+ span.end()++ # Cleanup tracking+ if trace_id in self.active_spans:+ del self.active_spans[trace_id]+ if span in self.span_stack:+ self.span_stack.remove(span)++ except Exception as e: # pylint: disable=broad-exception-caught+ handle_exception(span if span else None, e)++ def _get_span_name(self, operation_name: str, **metadata) -> str:+ """+ Generate OpenTelemetry-compliant span names.++ Args:+ operation_name: Base operation name+ **metadata: Additional context for naming++ Returns:+ Formatted span name following semantic conventions+ """+ # Extract context for naming+ agent_name = metadata.get('agent_name', '')+ model_name = metadata.get('model_name', '')+ tool_name = metadata.get('tool_name', '')+ workflow_name = metadata.get('workflow_name', '')++ # Apply OpenTelemetry semantic conventions for GenAI agents+ if 'agent' in operation_name.lower():+ if agent_name:+ return f"invoke_agent {agent_name}"+ return "invoke_agent"+ if 'chat' in operation_name.lower():+ if model_name:+ return f"chat {model_name}"+ return "chat response"+ if 'tool' in operation_name.lower():+ if tool_name:+ return f"execute_tool {tool_name}"+ return "execute_tool"+ if 'handoff' in operation_name.lower():+ target_agent = metadata.get('target_agent', 'unknown')+ return f"invoke_agent {target_agent}"+ if 'workflow' in operation_name.lower():+ if workflow_name:+ return f"workflow {workflow_name}"+ return "workflow"++ # Default case+ return operation_name++ def span_start(self, span_data, trace_id: str):+ """+ Handle span start events from OpenAI Agents.++ Args:+ span_data: Span data from agents framework+ trace_id: Associated trace identifier+ """+ try:+ # Extract span information+ span_name = getattr(span_data, 'name', 'unknown_operation')+ span_type = getattr(span_data, 'type', 'unknown')++ # Generate enhanced span name+ enhanced_name = self._get_span_name(+ span_name,+ agent_name=getattr(span_data, 'agent_name', None),+ model_name=getattr(span_data, 'model_name', None),+ tool_name=getattr(span_data, 'tool_name', None)+ )++ # Determine span operation type+ operation_type = self._get_operation_type(span_type, span_name)++ # Start span with proper context+ parent_span = self.span_stack[-1] if self.span_stack else None+ context = set_span_in_context(parent_span) if parent_span else None++ span = self.tracer.start_as_current_span(+ enhanced_name,+ kind=SpanKind.CLIENT,+ context=context,+ attributes={+ SemanticConvention.GEN_AI_SYSTEM: "openai_agents",+ SemanticConvention.GEN_AI_OPERATION: operation_type,+ "span.type": span_type,+ "span.id": getattr(span_data, 'span_id', ''),+ }+ )++ # Process specific span types+ self._process_span_attributes(span, span_data, span_type)++ # Track span+ span_id = getattr(span_data, 'span_id', len(self.span_stack))+ self.active_spans[f"{trace_id}:{span_id}"] = span+ self.span_stack.append(span)++ except Exception as e: # pylint: disable=broad-exception-caught+ handle_exception(None, e)++ def _get_operation_type(self, span_type: str, span_name: str) -> str:+ """Get operation type based on span characteristics."""+ type_mapping = {+ 'agent': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,+ 'generation': SemanticConvention.GEN_AI_OPERATION_CHAT,+ 'function': SemanticConvention.GEN_AI_OPERATION_CHAT,+ 'tool': SemanticConvention.GEN_AI_OPERATION_CHAT,+ 'handoff': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,+ }- # Cleanup tracking- if span.span_id in self._otel_spans:- del self._otel_spans[span.span_id]- if span.span_id in self._span_start_times:- del self._span_start_times[span.span_id]+ # Check span type first+ for key, operation in type_mapping.items():+ if key in span_type.lower():+ return operation- def on_trace_end(self, trace: "Trace") -> None:- """Called when a trace is finished - ends root span with business intelligence"""- if not TRACING_AVAILABLE or trace.trace_id not in self._root_spans:- return+ # Check span name+ for key, operation in type_mapping.items():+ if key in span_name.lower():+ return operation- root_span = self._root_spans[trace.trace_id]+ return SemanticConvention.GEN_AI_OPERATION_CHAT+ def _process_span_attributes(self, span, span_data, span_type: str):+ """Process and set span attributes based on span type."""try:- # Add trace-level business intelligence- self._process_trace_completion(root_span, trace)- root_span.set_status(Status(StatusCode.OK))- except Exception as e:- handle_exception(root_span, e)- finally:- root_span.end()-- # Cleanup- if trace.trace_id in self._root_spans:- del self._root_spans[trace.trace_id]- if trace.trace_id in self._span_start_times:- del self._span_start_times[trace.trace_id]-- def _get_span_name(self, span: "Span[Any]") -> str:- """Get semantic span name using {operation_type} {operation_name} format"""- data = span.span_data- operation_type = self._get_operation_type(data)-- # Extract operation name based on span type- operation_name = "unknown"-- # Special handling for handoffs- if hasattr(data, '__class__') and data.__class__.__name__ == 'HandoffSpanData':- if hasattr(data, 'to_agent') and data.to_agent:- operation_name = f"to {data.to_agent}"- else:- operation_name = "handoff"-- # Use agent name for agent spans- elif hasattr(data, '__class__') and data.__class__.__name__ == 'AgentSpanData':- # Try multiple possible attribute names for agent name- agent_name = None-- for attr in ['agent_name', 'name', 'agent', 'agent_id']:- if hasattr(data, attr):- agent_name = getattr(data, attr)- if agent_name and isinstance(agent_name, str):- break-- # If still no agent name, try looking in context or other attributes- if not agent_name:- # Try context or other nested attributes- if hasattr(data, 'context') and hasattr(data.context, 'agent'):- agent_name = getattr(data.context.agent, 'name', None)- elif hasattr(data, 'metadata') and hasattr(data.metadata, 'agent_name'):- agent_name = data.metadata.agent_name+ # Common attributes+ if hasattr(span_data, 'agent_name'):+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME,+ span_data.agent_name)- if agent_name:- operation_name = agent_name- else:- # If no agent name found, use a more descriptive fallback- operation_name = "execution"-- # Use name if available for other spans- elif hasattr(data, 'name') and isinstance(data.name, str):- operation_name = data.name-- # Fallback to type-based names- else:- operation_name = getattr(data, 'type', 'operation')-- # Return formatted name: {operation_type} {operation_name}- return f"{operation_type} {operation_name}"-- def _get_operation_type(self, data: Any) -> str:- """Map span data to operation types"""- class_name = data.__class__.__name__ if hasattr(data, '__class__') else str(type(data))-- mapping = {- 'AgentSpanData': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,- 'GenerationSpanData': SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,- 'FunctionSpanData': SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS,- 'HandoffSpanData': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,- 'ResponseSpanData': SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,- }+ if hasattr(span_data, 'model_name'):+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,+ span_data.model_name)++ # Agent-specific attributes+ if span_type == 'agent':+ self._process_agent_span(span, span_data)++ # Generation-specific attributes+ elif span_type == 'generation':+ self._process_generation_span(span, span_data)++ # Function/Tool-specific attributes+ elif span_type in ['function', 'tool']:+ self._process_function_span(span, span_data)++ # Handoff-specific attributes+ elif span_type == 'handoff':+ self._process_handoff_span(span, span_data)++ except Exception as e: # pylint: disable=broad-exception-caught+ handle_exception(span, e)++ def _process_agent_span(self, span, agent_span):+ """Process agent span data (unused parameter)."""+ # Agent-specific processing+ if hasattr(agent_span, 'instructions'):+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION,+ str(agent_span.instructions)[:500])- return mapping.get(class_name, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)-- def _set_common_framework_attributes(self, span: Any, operation_type: str) -> None:- """Set common framework attributes using semantic conventions"""- # Create scope object for common_framework_span_attributes- scope = type("GenericScope", (), {})()- scope._span = span- scope._start_time = time.time()- scope._end_time = time.time()-- # Use common framework attributes helper- # For framework operations, use localhost like other agent frameworks (AG2, Pydantic AI)- common_framework_span_attributes(- scope, SemanticConvention.GEN_AI_SYSTEM_OPENAI_AGENTS,- "localhost", 80, self._environment, self._application_name,- self._version, operation_type, None- )-- def _set_common_attributes(self, span: Any, trace_id: str) -> None:- """Set common framework attributes for root spans"""- self._set_common_framework_attributes(span, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)-- def _set_span_attributes(self, span: Any, agent_span: "Span[Any]") -> None:- """Set span-specific attributes based on span data using semantic conventions"""- data = agent_span.span_data-- # Agent-specific attributes using semantic conventions- if hasattr(data, 'agent_name') and data.agent_name:- span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, data.agent_name)- elif hasattr(data, 'name') and data.name:- span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, data.name)-- # Enhanced model information extraction- model = self._extract_model_info(data, agent_span)- if model:- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, str(model))-- # Enhanced input/output capture with MIME types (OpenLIT enhancement)- if self._capture_message_content:- self._capture_input_output(span, data)-- # Enhanced token usage details (inspired by OpenInference)- self._capture_detailed_token_usage(span, data)-- # Model invocation parameters as JSON (new feature from OpenInference)- self._capture_model_parameters(span, data)-- # Tool/function information for tool calls- if hasattr(data, '__class__') and 'Function' in data.__class__.__name__:- if hasattr(data, 'function_name'):- span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, data.function_name)- if hasattr(data, 'arguments'):- span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(data.arguments))-- # Enhanced handoff information extraction- if hasattr(data, '__class__') and 'Handoff' in data.__class__.__name__:- target_agent = self._extract_handoff_target(data, agent_span)- if target_agent:- span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, target_agent)- else:- # Fallback for handoff spans without clear target- span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, "agent handoff")-- # Request/response IDs if available- if hasattr(data, 'request_id'):- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, data.request_id)- elif hasattr(data, 'response_id'):- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, data.response_id)-- def _extract_model_info(self, data: Any, agent_span: "Span[Any]") -> Optional[str]:- """Extract model information from span data or agent configuration"""- # Try direct model attributes first- model_attrs = ['model', 'model_name', 'model_id', 'llm_model', 'openai_model']-- model = self._check_model_attrs(data, model_attrs)- if model:- return model-- # Try nested configuration objects- config_attrs = ['config', 'configuration', 'client_config', 'llm_config']- model = self._check_config_model_attrs(data, config_attrs, model_attrs)- if model:- return model-- # Try looking in the agent span itselfif hasattr(agent_span, 'model'):- return str(agent_span.model)-- # Try agent_config if available- if hasattr(agent_span, 'agent_config'):- model = self._check_model_attrs(agent_span.agent_config, model_attrs)- if model:- return model-- # Default fallback- return "gpt-4o"-- def _check_model_attrs(self, obj: Any, model_attrs: list) -> Optional[str]:- """Helper method to check model attributes on an object"""- for attr in model_attrs:- if not hasattr(obj, attr):- continue- model_value = getattr(obj, attr)- if model_value and isinstance(model_value, str):- return model_value- return None-- def _check_config_model_attrs(self, data: Any, config_attrs: list, model_attrs: list) -> Optional[str]:- """Helper method to check model attributes in nested configuration objects"""- for config_attr in config_attrs:- if not hasattr(data, config_attr):- continue- config = getattr(data, config_attr)- if not config:- continue- model = self._check_model_attrs(config, model_attrs)- if model:- return model- return None-- def _extract_handoff_target(self, data: Any, agent_span: "Span[Any]") -> Optional[str]:- """Extract handoff target information with enhanced logic"""- # Try direct target attributes- target_attrs = ['to_agent', 'target_agent', 'destination_agent', 'next_agent']- for attr in target_attrs:- if hasattr(data, attr):- target = getattr(data, attr)- if target and isinstance(target, str):- return f"to {target}"-- # Try from_agent for better handoff description- from_attrs = ['from_agent', 'source_agent', 'previous_agent']- for attr in from_attrs:- if hasattr(data, attr):- source = getattr(data, attr)- if source and isinstance(source, str):- return f"from {source}"-- # Try nested objects- if hasattr(data, 'handoff_info'):- info = data.handoff_info- for attr in target_attrs + from_attrs:- if hasattr(info, attr):- value = getattr(info, attr)- if value and isinstance(value, str):- prefix = "to" if attr in target_attrs else "from"- return f"{prefix} {value}"-- return None-- def _capture_input_output(self, span: Any, data: Any) -> None:- """Capture input/output content with MIME type detection (OpenLIT enhancement)"""+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,+ agent_span.model)++ def _process_generation_span(self, span, generation_span):+ """Process generation span data."""+ # Set generation-specific attributes+ if hasattr(generation_span, 'prompt'):+ span.set_attribute(SemanticConvention.GEN_AI_PROMPT,+ str(generation_span.prompt)[:1000])++ if hasattr(generation_span, 'completion'):+ span.set_attribute(SemanticConvention.GEN_AI_COMPLETION,+ str(generation_span.completion)[:1000])++ if hasattr(generation_span, 'usage'):+ usage = generation_span.usage+ if hasattr(usage, 'prompt_tokens'):+ span.set_attribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS,+ usage.prompt_tokens)+ if hasattr(usage, 'completion_tokens'):+ span.set_attribute(+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS,+ usage.completion_tokens+ )++ def _process_function_span(self, span, function_span):+ """Process function/tool span data."""+ if hasattr(function_span, 'function_name'):+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME,+ function_span.function_name)++ if hasattr(function_span, 'arguments'):+ span.set_attribute("gen_ai.tool.arguments",+ str(function_span.arguments)[:500])++ if hasattr(function_span, 'result'):+ span.set_attribute("gen_ai.tool.result",+ str(function_span.result)[:500])++ def _process_handoff_span(self, span, handoff_span):+ """Process handoff span data."""+ if hasattr(handoff_span, 'target_agent'):+ span.set_attribute("gen_ai.handoff.target_agent",+ handoff_span.target_agent)++ if hasattr(handoff_span, 'reason'):+ span.set_attribute("gen_ai.handoff.reason",+ str(handoff_span.reason)[:200])++ def span_end(self, span_data, trace_id: str):+ """Handle span end events."""try:- # Capture input content- if hasattr(data, 'input') and data.input is not None:- content = str(data.input)- span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, content)- # Set MIME type based on content structure- if content.startswith('{') or content.startswith('['):- span.set_attribute("gen_ai.content.prompt.mime_type", "application/json")+ span_id = getattr(span_data, 'span_id', '')+ span_key = f"{trace_id}:{span_id}"++ span = self.active_spans.get(span_key)+ if span:+ # Set final status+ if hasattr(span_data, 'error') and span_data.error:+ span.set_status(Status(StatusCode.ERROR,+ str(span_data.error)))else:- span.set_attribute("gen_ai.content.prompt.mime_type", "text/plain")-- # Capture output/response content- if hasattr(data, 'response') and data.response is not None:- content = str(data.response)- span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, content)- # Set MIME type based on content structure- if content.startswith('{') or content.startswith('['):- span.set_attribute("gen_ai.content.completion.mime_type", "application/json")- else:- span.set_attribute("gen_ai.content.completion.mime_type", "text/plain")+ span.set_status(Status(StatusCode.OK))++ # End span+ span.end()- except Exception:- pass # Ignore export errors+ # Cleanup+ if span_key in self.active_spans:+ del self.active_spans[span_key]+ if span in self.span_stack:+ self.span_stack.remove(span)- def _capture_detailed_token_usage(self, span: Any, data: Any) -> None:- """Capture detailed token usage information (inspired by OpenInference)"""+ except Exception as e: # pylint: disable=broad-exception-caught+ handle_exception(span if 'span' in locals() else None, e)++ def force_flush(self):+ """Force flush all pending spans."""try:- if hasattr(data, 'usage'):- usage = data.usage-- # Standard token usage- if hasattr(usage, 'input_tokens'):- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens)- if hasattr(usage, 'output_tokens'):- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, usage.output_tokens)-- # Enhanced token details (when available)- if hasattr(usage, 'input_tokens_details'):- details = usage.input_tokens_details- if hasattr(details, 'cached_tokens'):- span.set_attribute("gen_ai.usage.input_tokens.cached", details.cached_tokens)- if hasattr(details, 'reasoning_tokens'):- span.set_attribute("gen_ai.usage.input_tokens.reasoning", details.reasoning_tokens)-- if hasattr(usage, 'output_tokens_details'):- details = usage.output_tokens_details- if hasattr(details, 'reasoning_tokens'):- span.set_attribute("gen_ai.usage.output_tokens.reasoning", details.reasoning_tokens)-- except Exception:- pass # Ignore export errors-- def _capture_model_parameters(self, span: Any, data: Any) -> None:- """Capture model invocation parameters as JSON (new feature from OpenInference)"""+ # End any remaining spans+ for span in list(self.active_spans.values()):+ span.end()++ self.active_spans.clear()+ self.span_stack.clear()++ except Exception as e: # pylint: disable=broad-exception-caught+ handle_exception(None, e)++ def shutdown(self):+ """Shutdown the processor."""+ self.force_flush()++ def _extract_model_info(self, span_data) -> Dict[str, Any]:+ """Extract model information from span data."""+ model_info = {}++ if hasattr(span_data, 'model'):+ model_info['model'] = span_data.model+ if hasattr(span_data, 'model_name'):+ model_info['model'] = span_data.model_name++ return model_info++ def _calculate_cost(self, model: str, prompt_tokens: int,+ completion_tokens: int) -> float:+ """Calculate cost based on token usage."""try:- # Look for model configuration parameters- params = {}-- # Common parameter attributes- param_attrs = ['temperature', 'max_tokens', 'top_p', 'frequency_penalty', 'presence_penalty']- for attr in param_attrs:- if hasattr(data, attr):- params[attr] = getattr(data, attr)-- # Try nested config objects- if hasattr(data, 'config'):- config = data.config- for attr in param_attrs:- if hasattr(config, attr):- params[attr] = getattr(config, attr)-- # Try response object if available- if hasattr(data, 'response') and hasattr(data.response, 'model_dump'):- try:- response_dict = data.response.model_dump()- if response_dict and isinstance(response_dict, dict):- # Extract model parameters from response- if 'model' in response_dict:- params['model'] = response_dict['model']- if 'usage' in response_dict:- params['usage'] = response_dict['usage']- except Exception:- pass-- # Set as JSON if we found any parameters- if params:- span.set_attribute("gen_ai.request.parameters", json.dumps(params))-- except Exception:- pass # Ignore export errors-- def _process_span_completion(self, span: Any, agent_span: "Span[Any]") -> None:- """Process span completion with enhanced business intelligence"""- data = agent_span.span_data-- # Process response data if available- self._process_response_data(span, data)-- # Extract and set token usage for business intelligence- self._extract_token_usage(span, data)-- def _extract_token_usage(self, span: Any, data: Any) -> None:- """Extract token usage and calculate costs (OpenLIT's business intelligence)"""+ return get_chat_model_cost(+ model, self.pricing_info, prompt_tokens, completion_tokens+ )+ except Exception: # pylint: disable=broad-exception-caught+ return 0.0++ # Abstract method implementations required by OpenAI Agents framework+ def on_trace_start(self, trace):+ """Called when a trace starts - required by OpenAI Agents framework"""+ try:+ self.start_trace(getattr(trace, 'trace_id', 'unknown'),+ getattr(trace, 'name', 'workflow'))+ except Exception: # pylint: disable=broad-exception-caught+ pass++ def on_trace_end(self, trace):+ """Called when a trace ends - required by OpenAI Agents framework"""+ try:+ self.end_trace(getattr(trace, 'trace_id', 'unknown'))+ except Exception: # pylint: disable=broad-exception-caught+ pass++ def on_span_start(self, span):+ """Called when a span starts - required by OpenAI Agents framework"""+ try:+ trace_id = getattr(span, 'trace_id', 'unknown')+ self.span_start(span, trace_id)+ except Exception: # pylint: disable=broad-exception-caught+ pass++ def on_span_end(self, span):+ """Called when a span ends - required by OpenAI Agents framework"""try:- # Try to extract token usage from various possible locations- input_tokens = 0- output_tokens = 0-- # Check direct usage attributes- if hasattr(data, 'usage'):- usage = data.usage- input_tokens = getattr(usage, 'input_tokens', 0) or getattr(usage, 'prompt_tokens', 0)- output_tokens = getattr(usage, 'output_tokens', 0) or getattr(usage, 'completion_tokens', 0)-- # Check response object- elif hasattr(data, 'response') and hasattr(data.response, 'usage'):- usage = data.response.usage- input_tokens = getattr(usage, 'input_tokens', 0) or getattr(usage, 'prompt_tokens', 0)- output_tokens = getattr(usage, 'output_tokens', 0) or getattr(usage, 'completion_tokens', 0)-- # Set token attributes- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)- span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)-- # Calculate cost (OpenLIT's business intelligence advantage)- model = getattr(data, 'model', 'gpt-4o')- cost = get_chat_model_cost(model, self._pricing_info, input_tokens, output_tokens)- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)-- except Exception:- pass # Ignore errors in token usage extraction-- def _process_response_data(self, span: Any, data: Any) -> None:- """Process response data with content capture"""- if self._capture_message_content:- self._capture_input_output(span, data)-- def _process_trace_completion(self, span: Any, trace: "Trace") -> None:- """Process trace completion with business intelligence aggregation"""- # Add trace-level metadata- span.set_attribute(SemanticConvention.GEN_AI_OPERATION_NAME, "workflow")-- # Calculate total duration- if trace.trace_id in self._span_start_times:- start_time = self._span_start_times[trace.trace_id]- duration = time.time() - start_time- span.set_attribute(SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration)-- def _parse_timestamp(self, timestamp: Any) -> float:- """Parse timestamp from various formats"""- if isinstance(timestamp, (int, float)):- return float(timestamp)- elif isinstance(timestamp, str):- try:- # Try parsing ISO format- dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))- return dt.timestamp()- except ValueError:- return time.time()- else:- return time.time()-- def _as_utc_nano(self, timestamp: float) -> int:- """Convert timestamp to UTC nanoseconds for OpenTelemetry"""- return int(timestamp * 1_000_000_000)-- def force_flush(self) -> bool:- """Force flush any pending spans (required by TracingProcessor)"""- return True-- def shutdown(self) -> bool:- """Shutdown the processor (required by TracingProcessor)"""- return True+ trace_id = getattr(span, 'trace_id', 'unknown')+ self.span_end(span, trace_id)+ except Exception: # pylint: disable=broad-exception-caught+ passdiff --git a/sdk/python/src/openlit/semcov/__init__.py b/sdk/python/src/openlit/semcov/__init__.pyindex 574ffb74..23a4d30d 100644--- a/sdk/python/src/openlit/semcov/__init__.py+++ b/sdk/python/src/openlit/semcov/__init__.py@@ -74,8 +74,8 @@ class SemanticConvention:GEN_AI_OPERATION_TYPE_IMAGE = "image"GEN_AI_OPERATION_TYPE_AUDIO = "audio"GEN_AI_OPERATION_TYPE_VECTORDB = "vectordb"- GEN_AI_OPERATION_TYPE_FRAMEWORK = "framework"- GEN_AI_OPERATION_TYPE_AGENT = "agent"+ GEN_AI_OPERATION_TYPE_FRAMEWORK = "workflow"+ GEN_AI_OPERATION_TYPE_AGENT = "invoke_agent"GEN_AI_OPERATION_TYPE_CREATE_AGENT = "create_agent"GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK = "execute_task"GEN_AI_OPERATION_TYPE_RETRIEVE = "retrieve"@@ -404,3 +404,31 @@ class SemanticConvention:GEN_AI_FRAMEWORK_VECTOR_DIMENSION = "gen_ai.framework.vector.dimension"GEN_AI_FRAMEWORK_INDEX_NAME = "gen_ai.framework.index.name"GEN_AI_FRAMEWORK_INDEX_TYPE = "gen_ai.framework.index.type"++ # === STANDARD OPENTELEMETRY SEMANTIC CONVENTIONS ===+ # These are framework-agnostic and reusable across all agent frameworks++ # OpenAI Agent-specific Attributes (for any framework using OpenAI models)+ GEN_AI_CONVERSATION_ID = "gen_ai.conversation.id"+ GEN_AI_OPENAI_ASSISTANT_ID = "gen_ai.openai.assistant.id"+ GEN_AI_OPENAI_THREAD_ID = "gen_ai.openai.thread.id"+ GEN_AI_OPENAI_RUN_ID = "gen_ai.openai.run.id"+ GEN_AI_OPENAI_REQUEST_SERVICE_TIER = "gen_ai.openai.request.service_tier"+ GEN_AI_OPENAI_RESPONSE_SERVICE_TIER = "gen_ai.openai.response.service_tier"+ GEN_AI_OPENAI_RESPONSE_SYSTEM_FINGERPRINT = "gen_ai.openai.response.system_fingerprint"++ # Data Source Attributes (for RAG and knowledge retrieval)+ GEN_AI_DATA_SOURCE_ID = "gen_ai.data_source.id"+ GEN_AI_DATA_SOURCE_TYPE = "gen_ai.data_source.type"++ # Standard Tool Attributes (framework-agnostic)+ GEN_AI_TOOL_TYPE = "gen_ai.tool.type"++ # Standard Workflow Attributes (framework-agnostic)+ GEN_AI_WORKFLOW_AGENT_COUNT = "gen_ai.workflow.agent_count"+ GEN_AI_WORKFLOW_TASK_COUNT = "gen_ai.workflow.task_count"+ GEN_AI_WORKFLOW_EXECUTION_TYPE = "gen_ai.workflow.execution_type"++ # Standard Task Attributes (framework-agnostic)+ GEN_AI_TASK_DESCRIPTION = "gen_ai.task.description"+ GEN_AI_TASK_EXPECTED_OUTPUT = "gen_ai.task.expected_output"