Processor interface
TracingProcessor
Bases:ABC
Interface for processing and monitoring traces and spans in the OpenAI Agents system.
This abstract class defines the interface that all tracing processors must implement.Processors receive notifications when traces and spans start and end, allowing themto collect, process, and export tracing data.
Example
classCustomProcessor(TracingProcessor):def__init__(self):self.active_traces={}self.active_spans={}defon_trace_start(self,trace):self.active_traces[trace.trace_id]=tracedefon_trace_end(self,trace):# Process completed tracedelself.active_traces[trace.trace_id]defon_span_start(self,span):self.active_spans[span.span_id]=spandefon_span_end(self,span):# Process completed spandelself.active_spans[span.span_id]defshutdown(self):# Clean up resourcesself.active_traces.clear()self.active_spans.clear()defforce_flush(self):# Force processing of any queued itemspassNotes
- All methods should be thread-safe
- Methods should not block for long periods
- Handle errors gracefully to prevent disrupting agent execution
Source code insrc/agents/tracing/processor_interface.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | |
on_trace_startabstractmethod
on_trace_start(trace:Trace)->NoneCalled when a new trace begins execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace | Trace | The trace that started. Contains workflow name and metadata. | required |
Notes
- Called synchronously on trace start
- Should return quickly to avoid blocking execution
- Any errors should be caught and handled internally
Source code insrc/agents/tracing/processor_interface.py
on_trace_endabstractmethod
on_trace_end(trace:Trace)->NoneCalled when a trace completes execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace | Trace | The completed trace containing all spans and results. | required |
Notes
- Called synchronously when trace finishes
- Good time to export/process the complete trace
- Should handle cleanup of any trace-specific resources
Source code insrc/agents/tracing/processor_interface.py
on_span_startabstractmethod
on_span_start(span:Span[Any])->NoneCalled when a new span begins execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span | Span[Any] | The span that started. Contains operation details and context. | required |
Notes
- Called synchronously on span start
- Should return quickly to avoid blocking execution
- Spans are automatically nested under current trace/span
Source code insrc/agents/tracing/processor_interface.py
on_span_endabstractmethod
on_span_end(span:Span[Any])->NoneCalled when a span completes execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span | Span[Any] | The completed span containing execution results. | required |
Notes
- Called synchronously when span finishes
- Should not block or raise exceptions
- Good time to export/process the individual span
Source code insrc/agents/tracing/processor_interface.py
shutdownabstractmethod
Called when the application stops to clean up resources.
Should perform any necessary cleanup like:- Flushing queued traces/spans- Closing connections- Releasing resources
Source code insrc/agents/tracing/processor_interface.py
force_flushabstractmethod
Forces immediate processing of any queued traces/spans.
Notes
- Should process all queued items before returning
- Useful before shutdown or when immediate processing is needed
- May block while processing completes