257 lines
9.9 KiB
Python
257 lines
9.9 KiB
Python
"""
|
|
Utility module for mocking spans and tracers in OpenTelemetry tests.
|
|
This provides reusable mock classes for testing instrumentation.
|
|
"""
|
|
|
|
import builtins
|
|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
class MockSpanData:
|
|
"""Mock span data object for testing instrumentation."""
|
|
|
|
def __init__(self, data: Any, span_type: str = "GenerationSpanData"):
|
|
"""Initialize mock span data.
|
|
|
|
Args:
|
|
data: The data dictionary to include in the span data
|
|
span_type: The type of span data (used for __class__.__name__)
|
|
"""
|
|
# Set all keys from the data dictionary as attributes
|
|
for key, value in data.items():
|
|
setattr(self, key, value)
|
|
|
|
self.__class__.__name__ = span_type
|
|
|
|
|
|
class MockSpan:
|
|
"""Mock span object for testing instrumentation."""
|
|
|
|
def __init__(self, data: Any, span_type: str = "GenerationSpanData"):
|
|
"""Initialize mock span.
|
|
|
|
Args:
|
|
data: The data dictionary to include in the span data
|
|
span_type: The type of span data
|
|
"""
|
|
self.trace_id = data.get("trace_id", "trace123")
|
|
self.span_id = data.get("span_id", "span456")
|
|
self.parent_id = data.get("parent_id", None)
|
|
self.span_data = MockSpanData(data, span_type)
|
|
self.error = None
|
|
|
|
|
|
class MockTracingSpan:
|
|
"""Mock span for capturing attributes."""
|
|
|
|
def __init__(self):
|
|
"""Initialize the mock span."""
|
|
self.attributes = {}
|
|
self.status = None
|
|
self.events = []
|
|
self._is_ended = False
|
|
|
|
def set_attribute(self, key: str, value: Any) -> None:
|
|
"""Set an attribute on the span, capturing it for testing."""
|
|
self.attributes[key] = value
|
|
|
|
def set_status(self, status: Any) -> None:
|
|
"""Mock setting status."""
|
|
self.status = status
|
|
|
|
def record_exception(self, exception: Exception, attributes: Optional[Dict[str, Any]] = None) -> None:
|
|
"""Mock recording an exception."""
|
|
self.events.append({"name": "exception", "exception": exception, "attributes": attributes or {}})
|
|
|
|
def end(self) -> None:
|
|
"""End the span."""
|
|
self._is_ended = True
|
|
|
|
def __enter__(self) -> "MockTracingSpan":
|
|
"""Context manager entry."""
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
"""Context manager exit."""
|
|
self._is_ended = True
|
|
|
|
|
|
class MockTracer:
|
|
"""Mock tracer that captures attributes set on spans."""
|
|
|
|
def __init__(self, captured_attributes: Dict[str, Any]):
|
|
"""Initialize the mock tracer.
|
|
|
|
Args:
|
|
captured_attributes: Dictionary to store captured attributes
|
|
"""
|
|
self.captured_attributes = captured_attributes
|
|
|
|
def start_as_current_span(self, name: str, kind: Any = None, attributes: Optional[Dict[str, Any]] = None):
|
|
"""Start a new span and capture attributes."""
|
|
span = CapturedAttributeSpan(self.captured_attributes)
|
|
# Set any provided attributes
|
|
if attributes:
|
|
for key, val in attributes.items():
|
|
span.set_attribute(key, val)
|
|
return span
|
|
|
|
def start_span(self, name: str, kind: Any = None, attributes: Optional[Dict[str, Any]] = None):
|
|
"""Start a new span without making it the current span."""
|
|
span = CapturedAttributeSpan(self.captured_attributes)
|
|
# Set any provided attributes
|
|
if attributes:
|
|
for key, val in attributes.items():
|
|
span.set_attribute(key, val)
|
|
return span
|
|
|
|
|
|
class CapturedAttributeSpan(MockTracingSpan):
|
|
"""Mock span that captures attributes in a shared dictionary."""
|
|
|
|
def __init__(self, captured_attributes: Dict[str, Any]):
|
|
"""Initialize with a shared dictionary for capturing attributes.
|
|
|
|
Args:
|
|
captured_attributes: Dictionary to store captured attributes
|
|
"""
|
|
super().__init__()
|
|
self.captured_attributes = captured_attributes
|
|
|
|
def set_attribute(self, key: str, value: Any) -> None:
|
|
"""Set an attribute, capturing it in the shared dictionary."""
|
|
self.captured_attributes[key] = value
|
|
self.attributes[key] = value
|
|
|
|
|
|
def setup_mock_tracer(captured_attributes: Dict[str, Any]):
|
|
"""Set up a mock tracer by monkey patching OpenTelemetry.
|
|
|
|
Args:
|
|
captured_attributes: Dictionary to store captured attributes
|
|
|
|
Returns:
|
|
The original import function for cleanup
|
|
"""
|
|
original_import = builtins.__import__
|
|
|
|
def mocked_import(name, *args, **kwargs):
|
|
module = original_import(name, *args, **kwargs)
|
|
if name == "opentelemetry.trace":
|
|
# Monkey patch the get_tracer function
|
|
module.get_tracer = lambda *args, **kwargs: MockTracer(captured_attributes)
|
|
|
|
# Create a mock Status class
|
|
if not hasattr(module, "Status") or not isinstance(module.Status, type):
|
|
mock_status = MagicMock()
|
|
mock_status.return_value = MagicMock()
|
|
module.Status = mock_status
|
|
|
|
# Create a mock StatusCode enum
|
|
if not hasattr(module, "StatusCode"):
|
|
|
|
class MockStatusCode:
|
|
OK = "OK"
|
|
ERROR = "ERROR"
|
|
UNSET = "UNSET"
|
|
|
|
module.StatusCode = MockStatusCode
|
|
return module
|
|
|
|
builtins.__import__ = mocked_import
|
|
return original_import
|
|
|
|
|
|
def process_with_instrumentor(mock_span, exporter_class, captured_attributes: Dict[str, Any]):
|
|
"""Process a mock span with an instrumentor exporter.
|
|
|
|
Args:
|
|
mock_span: The mock span to process
|
|
exporter_class: The exporter class to use
|
|
captured_attributes: Dictionary to store captured attributes
|
|
|
|
Returns:
|
|
The captured attributes
|
|
"""
|
|
# Add core trace/span attributes from the mock_span directly to the captured_attributes
|
|
# This ensures that both semantic convention attributes and direct access attributes work
|
|
from agentops.semconv import CoreAttributes, AgentAttributes, WorkflowAttributes
|
|
|
|
# Add consistent formats for tools if it's an AgentSpanData
|
|
if hasattr(mock_span.span_data, "tools"):
|
|
# If tools is a list of dictionaries, convert it to a list of strings
|
|
tools = mock_span.span_data.tools
|
|
if isinstance(tools, list) and tools and isinstance(tools[0], dict):
|
|
tools_str = [tool.get("name", str(tool)) for tool in tools]
|
|
mock_span.span_data.tools = tools_str
|
|
|
|
# Set base attributes
|
|
core_attribute_mapping = {
|
|
CoreAttributes.TRACE_ID: mock_span.trace_id,
|
|
CoreAttributes.SPAN_ID: mock_span.span_id,
|
|
}
|
|
|
|
if mock_span.parent_id:
|
|
core_attribute_mapping[CoreAttributes.PARENT_ID] = mock_span.parent_id
|
|
|
|
for target_attr, value in core_attribute_mapping.items():
|
|
if value is not None:
|
|
captured_attributes[target_attr] = value
|
|
|
|
# Set agent attributes based on span type
|
|
span_type = mock_span.span_data.__class__.__name__
|
|
if span_type == "AgentSpanData":
|
|
if hasattr(mock_span.span_data, "name"):
|
|
captured_attributes[AgentAttributes.AGENT_NAME] = mock_span.span_data.name
|
|
if hasattr(mock_span.span_data, "input"):
|
|
captured_attributes[WorkflowAttributes.WORKFLOW_INPUT] = mock_span.span_data.input
|
|
if hasattr(mock_span.span_data, "output"):
|
|
captured_attributes[WorkflowAttributes.WORKFLOW_FINAL_OUTPUT] = mock_span.span_data.output
|
|
if hasattr(mock_span.span_data, "tools"):
|
|
captured_attributes[AgentAttributes.AGENT_TOOLS] = ",".join(mock_span.span_data.tools)
|
|
if hasattr(mock_span.span_data, "target_agent"):
|
|
captured_attributes[AgentAttributes.TO_AGENT] = mock_span.span_data.target_agent
|
|
|
|
elif span_type == "FunctionSpanData":
|
|
if hasattr(mock_span.span_data, "name"):
|
|
captured_attributes[AgentAttributes.AGENT_NAME] = mock_span.span_data.name
|
|
if hasattr(mock_span.span_data, "input"):
|
|
captured_attributes[WorkflowAttributes.WORKFLOW_INPUT] = json.dumps(mock_span.span_data.input)
|
|
if hasattr(mock_span.span_data, "output"):
|
|
captured_attributes[WorkflowAttributes.WORKFLOW_FINAL_OUTPUT] = json.dumps(mock_span.span_data.output)
|
|
if hasattr(mock_span.span_data, "from_agent"):
|
|
captured_attributes[AgentAttributes.FROM_AGENT] = mock_span.span_data.from_agent
|
|
|
|
# Also handle from_agent in AgentSpanData to support the hierarchy test
|
|
if span_type == "AgentSpanData" and hasattr(mock_span.span_data, "from_agent"):
|
|
captured_attributes[AgentAttributes.FROM_AGENT] = mock_span.span_data.from_agent
|
|
|
|
# Monkey patch the get_tracer function to return our MockTracer
|
|
with patch("opentelemetry.trace.get_tracer", return_value=MockTracer(captured_attributes)):
|
|
with patch("opentelemetry.trace.SpanKind"):
|
|
# Create a mocked Status class
|
|
with patch("opentelemetry.trace.Status"):
|
|
with patch("opentelemetry.trace.StatusCode"):
|
|
# Create a direct instance of the exporter with mocked tracer provider
|
|
mock_tracer_provider = MagicMock()
|
|
mock_tracer = MockTracer(captured_attributes)
|
|
mock_tracer_provider.get_tracer.return_value = mock_tracer
|
|
|
|
exporter = exporter_class(tracer_provider=mock_tracer_provider)
|
|
|
|
# Call the exporter's export_span method
|
|
try:
|
|
exporter.export_span(mock_span)
|
|
|
|
# If this span has error attribute, simulate error handling
|
|
if hasattr(mock_span, "error") and mock_span.error:
|
|
# Mark as an end event with error
|
|
mock_span.status = "ERROR"
|
|
exporter.export_span(mock_span)
|
|
except Exception as e:
|
|
print(f"Error during export_span: {e}")
|
|
|
|
return captured_attributes
|