MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / TraceContext

Class TraceContext

agentops/sdk/core.py:36–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35# Define TraceContext to hold span and token
36class TraceContext:
37 def __init__(self, span: Span, token: Optional[context_api.Token] = None, is_init_trace: bool = False):
38 self.span = span
39 self.token = token
40 self.is_init_trace = is_init_trace # Flag to identify the auto-started trace
41 self._end_state = StatusCode.UNSET # Default end state because we don't know yet
42
43 def __enter__(self) -> "TraceContext":
44 """Enter the trace context."""
45 return self
46
47 def __exit__(self, exc_type: Optional[type], exc_val: Optional[Exception], exc_tb: Optional[Any]) -> bool:
48 """Exit the trace context and end the trace.
49
50 Automatically sets the trace status based on whether an exception occurred:
51 - If an exception is present, sets status to ERROR
52 - If no exception occurred, sets status to OK
53
54 Returns:
55 False: Always returns False to propagate any exceptions that occurred
56 within the context manager block, following Python's
57 context manager protocol for proper exception handling.
58 """
59 if exc_type is not None:
60 self._end_state = StatusCode.ERROR
61 if exc_val:
62 logger.debug(f"Trace exiting with exception: {exc_val}")
63 else:
64 # No exception occurred, set to OK
65 self._end_state = StatusCode.OK
66
67 try:
68 tracer.end_trace(self, self._end_state)
69 except Exception as e:
70 logger.error(f"Error ending trace in context manager: {e}")
71
72 return False
73
74
75# get_imported_libraries moved to agentops.helpers.system

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…