|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TracingProcessor(abc.ABC):
|
10 |
| - """Interface for processing spans.""" |
| 10 | + """Interface for processing and monitoring traces and spans in the OpenAI Agents system. |
| 11 | +
|
| 12 | + This abstract class defines the interface that all tracing processors must implement. |
| 13 | + Processors receive notifications when traces and spans start and end, allowing them |
| 14 | + to collect, process, and export tracing data. |
| 15 | +
|
| 16 | + Example: |
| 17 | + ```python |
| 18 | + class CustomProcessor(TracingProcessor): |
| 19 | + def __init__(self): |
| 20 | + self.active_traces = {} |
| 21 | + self.active_spans = {} |
| 22 | +
|
| 23 | + def on_trace_start(self, trace): |
| 24 | + self.active_traces[trace.trace_id] = trace |
| 25 | +
|
| 26 | + def on_trace_end(self, trace): |
| 27 | + # Process completed trace |
| 28 | + del self.active_traces[trace.trace_id] |
| 29 | +
|
| 30 | + def on_span_start(self, span): |
| 31 | + self.active_spans[span.span_id] = span |
| 32 | +
|
| 33 | + def on_span_end(self, span): |
| 34 | + # Process completed span |
| 35 | + del self.active_spans[span.span_id] |
| 36 | +
|
| 37 | + def shutdown(self): |
| 38 | + # Clean up resources |
| 39 | + self.active_traces.clear() |
| 40 | + self.active_spans.clear() |
| 41 | +
|
| 42 | + def force_flush(self): |
| 43 | + # Force processing of any queued items |
| 44 | + pass |
| 45 | + ``` |
| 46 | +
|
| 47 | + Notes: |
| 48 | + - All methods should be thread-safe |
| 49 | + - Methods should not block for long periods |
| 50 | + - Handle errors gracefully to prevent disrupting agent execution |
| 51 | + """ |
11 | 52 |
|
12 | 53 | @abc.abstractmethod
|
13 | 54 | def on_trace_start(self, trace: "Trace") -> None:
|
14 |
| - """Called when a trace is started. |
| 55 | + """Called when a new trace begins execution. |
15 | 56 |
|
16 | 57 | Args:
|
17 |
| - trace: The trace that started. |
| 58 | + trace: The trace that started. Contains workflow name and metadata. |
| 59 | +
|
| 60 | + Notes: |
| 61 | + - Called synchronously on trace start |
| 62 | + - Should return quickly to avoid blocking execution |
| 63 | + - Any errors should be caught and handled internally |
18 | 64 | """
|
19 | 65 | pass
|
20 | 66 |
|
21 | 67 | @abc.abstractmethod
|
22 | 68 | def on_trace_end(self, trace: "Trace") -> None:
|
23 |
| - """Called when a trace is finished. |
| 69 | + """Called when a trace completes execution. |
24 | 70 |
|
25 | 71 | Args:
|
26 |
| - trace: The trace that finished. |
| 72 | + trace: The completed trace containing all spans and results. |
| 73 | +
|
| 74 | + Notes: |
| 75 | + - Called synchronously when trace finishes |
| 76 | + - Good time to export/process the complete trace |
| 77 | + - Should handle cleanup of any trace-specific resources |
27 | 78 | """
|
28 | 79 | pass
|
29 | 80 |
|
30 | 81 | @abc.abstractmethod
|
31 | 82 | def on_span_start(self, span: "Span[Any]") -> None:
|
32 |
| - """Called when a span is started. |
| 83 | + """Called when a new span begins execution. |
33 | 84 |
|
34 | 85 | Args:
|
35 |
| - span: The span that started. |
| 86 | + span: The span that started. Contains operation details and context. |
| 87 | +
|
| 88 | + Notes: |
| 89 | + - Called synchronously on span start |
| 90 | + - Should return quickly to avoid blocking execution |
| 91 | + - Spans are automatically nested under current trace/span |
36 | 92 | """
|
37 | 93 | pass
|
38 | 94 |
|
39 | 95 | @abc.abstractmethod
|
40 | 96 | def on_span_end(self, span: "Span[Any]") -> None:
|
41 |
| - """Called when a span is finished. Should not block or raise exceptions. |
| 97 | + """Called when a span completes execution. |
42 | 98 |
|
43 | 99 | Args:
|
44 |
| - span: The span that finished. |
| 100 | + span: The completed span containing execution results. |
| 101 | +
|
| 102 | + Notes: |
| 103 | + - Called synchronously when span finishes |
| 104 | + - Should not block or raise exceptions |
| 105 | + - Good time to export/process the individual span |
45 | 106 | """
|
46 | 107 | pass
|
47 | 108 |
|
48 | 109 | @abc.abstractmethod
|
49 | 110 | def shutdown(self) -> None:
|
50 |
| - """Called when the application stops.""" |
| 111 | + """Called when the application stops to clean up resources. |
| 112 | +
|
| 113 | + Should perform any necessary cleanup like: |
| 114 | + - Flushing queued traces/spans |
| 115 | + - Closing connections |
| 116 | + - Releasing resources |
| 117 | + """ |
51 | 118 | pass
|
52 | 119 |
|
53 | 120 | @abc.abstractmethod
|
54 | 121 | def force_flush(self) -> None:
|
55 |
| - """Forces an immediate flush of all queued spans/traces.""" |
| 122 | + """Forces immediate processing of any queued traces/spans. |
| 123 | +
|
| 124 | + Notes: |
| 125 | + - Should process all queued items before returning |
| 126 | + - Useful before shutdown or when immediate processing is needed |
| 127 | + - May block while processing completes |
| 128 | + """ |
56 | 129 | pass
|
57 | 130 |
|
58 | 131 |
|
|
0 commit comments