Skip to content

Commit 5e31be9

Browse files
authored
Handle processor exceptions and fix tracing log formatting (#1292)
* Wrap each call in `SynchronousMultiTracingProcessor` (`on_trace_start`, `on_trace_end`, `on_span_start`, `on_span_end`, `shutdown`, `force_flush`) in a `try/except` so one failing processor won’t prevent the others from running. * Add a missing space in the `logger.error` message when no active trace is found, ensuring it reads “…first Returning NoOpSpan.” cleanly.
1 parent 63d72d0 commit 5e31be9

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/agents/tracing/provider.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,61 @@ def on_trace_start(self, trace: Trace) -> None:
4343
Called when a trace is started.
4444
"""
4545
for processor in self._processors:
46-
processor.on_trace_start(trace)
46+
try:
47+
processor.on_trace_start(trace)
48+
except Exception as e:
49+
logger.error(f"Error in trace processor {processor} during on_trace_start: {e}")
4750

4851
def on_trace_end(self, trace: Trace) -> None:
4952
"""
5053
Called when a trace is finished.
5154
"""
5255
for processor in self._processors:
53-
processor.on_trace_end(trace)
56+
try:
57+
processor.on_trace_end(trace)
58+
except Exception as e:
59+
logger.error(f"Error in trace processor {processor} during on_trace_end: {e}")
5460

5561
def on_span_start(self, span: Span[Any]) -> None:
5662
"""
5763
Called when a span is started.
5864
"""
5965
for processor in self._processors:
60-
processor.on_span_start(span)
66+
try:
67+
processor.on_span_start(span)
68+
except Exception as e:
69+
logger.error(f"Error in trace processor {processor} during on_span_start: {e}")
6170

6271
def on_span_end(self, span: Span[Any]) -> None:
6372
"""
6473
Called when a span is finished.
6574
"""
6675
for processor in self._processors:
67-
processor.on_span_end(span)
76+
try:
77+
processor.on_span_end(span)
78+
except Exception as e:
79+
logger.error(f"Error in trace processor {processor} during on_span_end: {e}")
6880

6981
def shutdown(self) -> None:
7082
"""
7183
Called when the application stops.
7284
"""
7385
for processor in self._processors:
7486
logger.debug(f"Shutting down trace processor {processor}")
75-
processor.shutdown()
87+
try:
88+
processor.shutdown()
89+
except Exception as e:
90+
logger.error(f"Error shutting down trace processor {processor}: {e}")
7691

7792
def force_flush(self):
7893
"""
7994
Force the processors to flush their buffers.
8095
"""
8196
for processor in self._processors:
82-
processor.force_flush()
97+
try:
98+
processor.force_flush()
99+
except Exception as e:
100+
logger.error(f"Error flushing trace processor {processor}: {e}")
83101

84102

85103
class TraceProvider(ABC):
@@ -247,7 +265,7 @@ def create_span(
247265
current_trace = Scope.get_current_trace()
248266
if current_trace is None:
249267
logger.error(
250-
"No active trace. Make sure to start a trace with `trace()` first"
268+
"No active trace. Make sure to start a trace with `trace()` first "
251269
"Returning NoOpSpan."
252270
)
253271
return NoOpSpan(span_data)

0 commit comments

Comments
 (0)