|
| 1 | +"""Storage operation attribute handlers for Agno workflow instrumentation.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Any, Dict, Optional, Tuple |
| 5 | +from opentelemetry.util.types import AttributeValue |
| 6 | + |
| 7 | +from agentops.semconv.span_attributes import SpanAttributes |
| 8 | +from agentops.semconv.span_kinds import SpanKind as AgentOpsSpanKind |
| 9 | +from agentops.instrumentation.common.attributes import get_common_attributes |
| 10 | + |
| 11 | + |
| 12 | +def get_storage_read_attributes( |
| 13 | + args: Tuple[Any, ...] = (), |
| 14 | + kwargs: Optional[Dict[str, Any]] = None, |
| 15 | + return_value: Optional[Any] = None, |
| 16 | +) -> Dict[str, AttributeValue]: |
| 17 | + """Extract attributes from storage read operations. |
| 18 | +
|
| 19 | + Args: |
| 20 | + args: Positional arguments passed to read_from_storage |
| 21 | + kwargs: Keyword arguments passed to read_from_storage |
| 22 | + return_value: Return value from read_from_storage (the cached data or None) |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Dictionary of OpenTelemetry attributes for storage read operations |
| 26 | + """ |
| 27 | + attributes = get_common_attributes() |
| 28 | + kwargs = kwargs or {} |
| 29 | + |
| 30 | + # Mark this as a storage operation within workflow context |
| 31 | + attributes["storage.operation"] = "read" |
| 32 | + attributes[SpanAttributes.AGENTOPS_SPAN_KIND] = AgentOpsSpanKind.WORKFLOW |
| 33 | + |
| 34 | + if args and len(args) > 0: |
| 35 | + workflow = args[0] |
| 36 | + |
| 37 | + # Get workflow information |
| 38 | + if hasattr(workflow, "workflow_id") and workflow.workflow_id: |
| 39 | + attributes["storage.workflow_id"] = str(workflow.workflow_id) |
| 40 | + if hasattr(workflow, "session_id") and workflow.session_id: |
| 41 | + attributes["storage.session_id"] = str(workflow.session_id) |
| 42 | + |
| 43 | + # Get storage type |
| 44 | + if hasattr(workflow, "storage") and workflow.storage: |
| 45 | + storage_type = type(workflow.storage).__name__ |
| 46 | + attributes["storage.backend"] = storage_type |
| 47 | + |
| 48 | + # Get session state info for context |
| 49 | + if hasattr(workflow, "session_state") and isinstance(workflow.session_state, dict): |
| 50 | + # Get all cache keys |
| 51 | + cache_keys = list(workflow.session_state.keys()) |
| 52 | + attributes["storage.cache_size"] = len(cache_keys) |
| 53 | + if cache_keys: |
| 54 | + attributes["storage.cache_keys"] = json.dumps(cache_keys) |
| 55 | + |
| 56 | + # Analyze the return value to determine cache hit/miss |
| 57 | + if return_value is not None: |
| 58 | + # Cache hit |
| 59 | + attributes["storage.cache_hit"] = True |
| 60 | + attributes["storage.result"] = "hit" |
| 61 | + |
| 62 | + # Get data type and size |
| 63 | + data_type = type(return_value).__name__ |
| 64 | + attributes["storage.data_type"] = data_type |
| 65 | + |
| 66 | + # For dict/list, show structure |
| 67 | + if isinstance(return_value, dict): |
| 68 | + attributes["storage.data_keys"] = json.dumps(list(return_value.keys())) |
| 69 | + attributes["storage.data_size"] = len(return_value) |
| 70 | + elif isinstance(return_value, (list, tuple)): |
| 71 | + attributes["storage.data_size"] = len(return_value) |
| 72 | + elif isinstance(return_value, str): |
| 73 | + attributes["storage.data_size"] = len(return_value) |
| 74 | + # Show full string data without truncation |
| 75 | + attributes["storage.data_preview"] = return_value |
| 76 | + else: |
| 77 | + # Cache miss |
| 78 | + attributes["storage.cache_hit"] = False |
| 79 | + attributes["storage.result"] = "miss" |
| 80 | + |
| 81 | + return attributes |
| 82 | + |
| 83 | + |
| 84 | +def get_storage_write_attributes( |
| 85 | + args: Tuple[Any, ...] = (), |
| 86 | + kwargs: Optional[Dict[str, Any]] = None, |
| 87 | + return_value: Optional[Any] = None, |
| 88 | +) -> Dict[str, AttributeValue]: |
| 89 | + """Extract attributes from storage write operations. |
| 90 | +
|
| 91 | + Args: |
| 92 | + args: Positional arguments passed to write_to_storage |
| 93 | + kwargs: Keyword arguments passed to write_to_storage |
| 94 | + return_value: Return value from write_to_storage (usually None or success indicator) |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Dictionary of OpenTelemetry attributes for storage write operations |
| 98 | + """ |
| 99 | + attributes = get_common_attributes() |
| 100 | + kwargs = kwargs or {} |
| 101 | + |
| 102 | + # Mark this as a storage operation within workflow context |
| 103 | + attributes["storage.operation"] = "write" |
| 104 | + attributes[SpanAttributes.AGENTOPS_SPAN_KIND] = AgentOpsSpanKind.WORKFLOW |
| 105 | + |
| 106 | + if args and len(args) > 0: |
| 107 | + workflow = args[0] |
| 108 | + |
| 109 | + # Get workflow information |
| 110 | + if hasattr(workflow, "workflow_id") and workflow.workflow_id: |
| 111 | + attributes["storage.workflow_id"] = str(workflow.workflow_id) |
| 112 | + if hasattr(workflow, "session_id") and workflow.session_id: |
| 113 | + attributes["storage.session_id"] = str(workflow.session_id) |
| 114 | + |
| 115 | + # Get storage type |
| 116 | + if hasattr(workflow, "storage") and workflow.storage: |
| 117 | + storage_type = type(workflow.storage).__name__ |
| 118 | + attributes["storage.backend"] = storage_type |
| 119 | + |
| 120 | + # Get session state info to see what's being written |
| 121 | + if hasattr(workflow, "session_state") and isinstance(workflow.session_state, dict): |
| 122 | + # Get cache state after write |
| 123 | + cache_keys = list(workflow.session_state.keys()) |
| 124 | + attributes["storage.cache_size"] = len(cache_keys) |
| 125 | + if cache_keys: |
| 126 | + attributes["storage.cache_keys"] = json.dumps(cache_keys) |
| 127 | + |
| 128 | + # Try to identify what was written (the newest/changed data) |
| 129 | + # This is a heuristic - in practice you might need to track state changes |
| 130 | + if cache_keys: |
| 131 | + # Show the last key as likely the one just written |
| 132 | + last_key = cache_keys[-1] |
| 133 | + attributes["storage.written_key"] = last_key |
| 134 | + |
| 135 | + # Get value preview |
| 136 | + value = workflow.session_state.get(last_key) |
| 137 | + if value is not None: |
| 138 | + value_type = type(value).__name__ |
| 139 | + attributes["storage.written_value_type"] = value_type |
| 140 | + |
| 141 | + if isinstance(value, str): |
| 142 | + if len(value) > 100: |
| 143 | + attributes["storage.written_value_preview"] = value[:100] + "..." |
| 144 | + else: |
| 145 | + attributes["storage.written_value_preview"] = value |
| 146 | + attributes["storage.written_value_size"] = len(value) |
| 147 | + elif isinstance(value, (dict, list)): |
| 148 | + attributes["storage.written_value_size"] = len(value) |
| 149 | + attributes["storage.written_value_preview"] = f"{value_type} with {len(value)} items" |
| 150 | + |
| 151 | + # Check write result |
| 152 | + if return_value is not None: |
| 153 | + attributes["storage.write_success"] = True |
| 154 | + else: |
| 155 | + # Most storage writes return None on success, so this is normal |
| 156 | + attributes["storage.write_success"] = True |
| 157 | + |
| 158 | + return attributes |
0 commit comments