Validate that spans have been sent to AgentOps. Args: trace_id: Direct trace ID to validate trace_context: TraceContext object from start_trace (alternative to trace_id) max_retries: Maximum number of retries to wait for spans to appear retry_delay: Delay be
(
trace_id: Optional[str] = None,
trace_context: Optional[Any] = None,
max_retries: int = 10,
retry_delay: float = 1.0,
check_llm: bool = True,
min_spans: int = 1,
api_key: Optional[str] = None,
)
| 207 | |
| 208 | |
| 209 | def validate_trace_spans( |
| 210 | trace_id: Optional[str] = None, |
| 211 | trace_context: Optional[Any] = None, |
| 212 | max_retries: int = 10, |
| 213 | retry_delay: float = 1.0, |
| 214 | check_llm: bool = True, |
| 215 | min_spans: int = 1, |
| 216 | api_key: Optional[str] = None, |
| 217 | ) -> Dict[str, Any]: |
| 218 | """ |
| 219 | Validate that spans have been sent to AgentOps. |
| 220 | |
| 221 | Args: |
| 222 | trace_id: Direct trace ID to validate |
| 223 | trace_context: TraceContext object from start_trace (alternative to trace_id) |
| 224 | max_retries: Maximum number of retries to wait for spans to appear |
| 225 | retry_delay: Delay between retries in seconds |
| 226 | check_llm: Whether to specifically check for LLM spans |
| 227 | min_spans: Minimum number of spans expected |
| 228 | api_key: Optional API key (uses environment variable if not provided) |
| 229 | |
| 230 | Returns: |
| 231 | Dictionary containing validation results and metrics |
| 232 | |
| 233 | Raises: |
| 234 | ValidationError: If validation fails |
| 235 | ValueError: If neither trace_id nor trace_context is provided |
| 236 | """ |
| 237 | # Extract trace ID |
| 238 | if trace_id is None and trace_context is None: |
| 239 | # Try to get from current span |
| 240 | try: |
| 241 | from opentelemetry.trace import get_current_span |
| 242 | |
| 243 | current_span = get_current_span() |
| 244 | if current_span and hasattr(current_span, "get_span_context"): |
| 245 | span_context = current_span.get_span_context() |
| 246 | if hasattr(span_context, "trace_id") and span_context.trace_id: |
| 247 | if isinstance(span_context.trace_id, int): |
| 248 | trace_id = format(span_context.trace_id, "032x") |
| 249 | else: |
| 250 | trace_id = str(span_context.trace_id) |
| 251 | except ImportError: |
| 252 | pass |
| 253 | |
| 254 | elif trace_context is not None and trace_id is None: |
| 255 | # Extract from TraceContext |
| 256 | if hasattr(trace_context, "span") and trace_context.span: |
| 257 | span_context = trace_context.span.get_span_context() |
| 258 | if hasattr(span_context, "trace_id"): |
| 259 | if isinstance(span_context.trace_id, int): |
| 260 | trace_id = format(span_context.trace_id, "032x") |
| 261 | else: |
| 262 | trace_id = str(span_context.trace_id) |
| 263 | |
| 264 | if trace_id is None: |
| 265 | raise ValueError("No trace ID found. Provide either trace_id or trace_context parameter.") |
| 266 |
searching dependent graphs…