Movatterモバイル変換


[0]ホーム

URL:


Skip to content

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 itemspass
Notes
  • 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
classTracingProcessor(abc.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 them    to collect, process, and export tracing data.    Example:        ```python        class CustomProcessor(TracingProcessor):            def __init__(self):                self.active_traces = {}                self.active_spans = {}            def on_trace_start(self, trace):                self.active_traces[trace.trace_id] = trace            def on_trace_end(self, trace):                # Process completed trace                del self.active_traces[trace.trace_id]            def on_span_start(self, span):                self.active_spans[span.span_id] = span            def on_span_end(self, span):                # Process completed span                del self.active_spans[span.span_id]            def shutdown(self):                # Clean up resources                self.active_traces.clear()                self.active_spans.clear()            def force_flush(self):                # Force processing of any queued items                pass        ```    Notes:        - All methods should be thread-safe        - Methods should not block for long periods        - Handle errors gracefully to prevent disrupting agent execution    """@abc.abstractmethoddefon_trace_start(self,trace:"Trace")->None:"""Called when a new trace begins execution.        Args:            trace: The trace that started. Contains workflow name and metadata.        Notes:            - Called synchronously on trace start            - Should return quickly to avoid blocking execution            - Any errors should be caught and handled internally        """pass@abc.abstractmethoddefon_trace_end(self,trace:"Trace")->None:"""Called when a trace completes execution.        Args:            trace: The completed trace containing all spans and results.        Notes:            - Called synchronously when trace finishes            - Good time to export/process the complete trace            - Should handle cleanup of any trace-specific resources        """pass@abc.abstractmethoddefon_span_start(self,span:"Span[Any]")->None:"""Called when a new span begins execution.        Args:            span: The span that started. Contains operation details and context.        Notes:            - Called synchronously on span start            - Should return quickly to avoid blocking execution            - Spans are automatically nested under current trace/span        """pass@abc.abstractmethoddefon_span_end(self,span:"Span[Any]")->None:"""Called when a span completes execution.        Args:            span: The completed span containing execution results.        Notes:            - Called synchronously when span finishes            - Should not block or raise exceptions            - Good time to export/process the individual span        """pass@abc.abstractmethoddefshutdown(self)->None:"""Called when the application stops to clean up resources.        Should perform any necessary cleanup like:        - Flushing queued traces/spans        - Closing connections        - Releasing resources        """pass@abc.abstractmethoddefforce_flush(self)->None:"""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        """pass

on_trace_startabstractmethod

on_trace_start(trace:Trace)->None

Called when a new trace begins execution.

Parameters:

NameTypeDescriptionDefault
traceTrace

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
@abc.abstractmethoddefon_trace_start(self,trace:"Trace")->None:"""Called when a new trace begins execution.    Args:        trace: The trace that started. Contains workflow name and metadata.    Notes:        - Called synchronously on trace start        - Should return quickly to avoid blocking execution        - Any errors should be caught and handled internally    """pass

on_trace_endabstractmethod

on_trace_end(trace:Trace)->None

Called when a trace completes execution.

Parameters:

NameTypeDescriptionDefault
traceTrace

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
@abc.abstractmethoddefon_trace_end(self,trace:"Trace")->None:"""Called when a trace completes execution.    Args:        trace: The completed trace containing all spans and results.    Notes:        - Called synchronously when trace finishes        - Good time to export/process the complete trace        - Should handle cleanup of any trace-specific resources    """pass

on_span_startabstractmethod

on_span_start(span:Span[Any])->None

Called when a new span begins execution.

Parameters:

NameTypeDescriptionDefault
spanSpan[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
@abc.abstractmethoddefon_span_start(self,span:"Span[Any]")->None:"""Called when a new span begins execution.    Args:        span: The span that started. Contains operation details and context.    Notes:        - Called synchronously on span start        - Should return quickly to avoid blocking execution        - Spans are automatically nested under current trace/span    """pass

on_span_endabstractmethod

on_span_end(span:Span[Any])->None

Called when a span completes execution.

Parameters:

NameTypeDescriptionDefault
spanSpan[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
@abc.abstractmethoddefon_span_end(self,span:"Span[Any]")->None:"""Called when a span completes execution.    Args:        span: The completed span containing execution results.    Notes:        - Called synchronously when span finishes        - Should not block or raise exceptions        - Good time to export/process the individual span    """pass

shutdownabstractmethod

shutdown()->None

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
@abc.abstractmethoddefshutdown(self)->None:"""Called when the application stops to clean up resources.    Should perform any necessary cleanup like:    - Flushing queued traces/spans    - Closing connections    - Releasing resources    """pass

force_flushabstractmethod

force_flush()->None

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
Source code insrc/agents/tracing/processor_interface.py
@abc.abstractmethoddefforce_flush(self)->None:"""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    """pass

TracingExporter

Bases:ABC

Exports traces and spans. For example, could log them or send them to a backend.

Source code insrc/agents/tracing/processor_interface.py
classTracingExporter(abc.ABC):"""Exports traces and spans. For example, could log them or send them to a backend."""@abc.abstractmethoddefexport(self,items:list["Trace | Span[Any]"])->None:"""Exports a list of traces and spans.        Args:            items: The items to export.        """pass

exportabstractmethod

export(items:list[Trace|Span[Any]])->None

Exports a list of traces and spans.

Parameters:

NameTypeDescriptionDefault
itemslist[Trace |Span[Any]]

The items to export.

required
Source code insrc/agents/tracing/processor_interface.py
@abc.abstractmethoddefexport(self,items:list["Trace | Span[Any]"])->None:"""Exports a list of traces and spans.    Args:        items: The items to export.    """pass

[8]ページ先頭

©2009-2025 Movatter.jp