Starts a new trace (root span) and returns its context. This allows for multiple concurrent, user-managed traces. Args: trace_name: Name for the trace (e.g., "session", "my_custom_task"). tags: Optional tags to attach to the trace span (list of strings or dict). Re
(
trace_name: str = "session", tags: Optional[Union[Dict[str, Any], List[str]]] = None
)
| 225 | |
| 226 | |
| 227 | def start_trace( |
| 228 | trace_name: str = "session", tags: Optional[Union[Dict[str, Any], List[str]]] = None |
| 229 | ) -> Optional[TraceContext]: |
| 230 | """ |
| 231 | Starts a new trace (root span) and returns its context. |
| 232 | This allows for multiple concurrent, user-managed traces. |
| 233 | |
| 234 | Args: |
| 235 | trace_name: Name for the trace (e.g., "session", "my_custom_task"). |
| 236 | tags: Optional tags to attach to the trace span (list of strings or dict). |
| 237 | |
| 238 | Returns: |
| 239 | A TraceContext object containing the span and context token, or None if SDK not initialized. |
| 240 | """ |
| 241 | if not tracer.initialized: |
| 242 | # Optionally, attempt to initialize the client if not already, or log a more severe warning. |
| 243 | # For now, align with legacy start_session that would try to init. |
| 244 | # However, explicit init is preferred before starting traces. |
| 245 | logger.warning("AgentOps SDK not initialized. Attempting to initialize with defaults before starting trace.") |
| 246 | try: |
| 247 | init() # Attempt to initialize with environment variables / defaults |
| 248 | if not tracer.initialized: |
| 249 | logger.error("SDK initialization failed. Cannot start trace.") |
| 250 | return None |
| 251 | except Exception as e: |
| 252 | logger.error(f"SDK auto-initialization failed during start_trace: {e}. Cannot start trace.") |
| 253 | return None |
| 254 | |
| 255 | return tracer.start_trace(trace_name=trace_name, tags=tags) |
| 256 | |
| 257 | |
| 258 | def end_trace( |
searching dependent graphs…