Skip to content

Handle processor exceptions and fix tracing log formatting #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/agents/tracing/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,61 @@ def on_trace_start(self, trace: Trace) -> None:
Called when a trace is started.
"""
for processor in self._processors:
processor.on_trace_start(trace)
try:
processor.on_trace_start(trace)
except Exception as e:
logger.error(f"Error in trace processor {processor} during on_trace_start: {e}")

def on_trace_end(self, trace: Trace) -> None:
"""
Called when a trace is finished.
"""
for processor in self._processors:
processor.on_trace_end(trace)
try:
processor.on_trace_end(trace)
except Exception as e:
logger.error(f"Error in trace processor {processor} during on_trace_end: {e}")

def on_span_start(self, span: Span[Any]) -> None:
"""
Called when a span is started.
"""
for processor in self._processors:
processor.on_span_start(span)
try:
processor.on_span_start(span)
except Exception as e:
logger.error(f"Error in trace processor {processor} during on_span_start: {e}")

def on_span_end(self, span: Span[Any]) -> None:
"""
Called when a span is finished.
"""
for processor in self._processors:
processor.on_span_end(span)
try:
processor.on_span_end(span)
except Exception as e:
logger.error(f"Error in trace processor {processor} during on_span_end: {e}")

def shutdown(self) -> None:
"""
Called when the application stops.
"""
for processor in self._processors:
logger.debug(f"Shutting down trace processor {processor}")
processor.shutdown()
try:
processor.shutdown()
except Exception as e:
logger.error(f"Error shutting down trace processor {processor}: {e}")

def force_flush(self):
"""
Force the processors to flush their buffers.
"""
for processor in self._processors:
processor.force_flush()
try:
processor.force_flush()
except Exception as e:
logger.error(f"Error flushing trace processor {processor}: {e}")


class TraceProvider(ABC):
Expand Down Expand Up @@ -247,7 +265,7 @@ def create_span(
current_trace = Scope.get_current_trace()
if current_trace is None:
logger.error(
"No active trace. Make sure to start a trace with `trace()` first"
"No active trace. Make sure to start a trace with `trace()` first "
"Returning NoOpSpan."
)
return NoOpSpan(span_data)
Expand Down