Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/agents/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ def __init__(
self._thread_start_lock = threading.Lock()
self._export_lock = threading.Lock()
self._shutdown_deadline: float | None = None
self._dropped_count: int = 0

@property
def dropped_count(self) -> int:
"""The number of spans or traces dropped due to a full queue."""
return self._dropped_count

def _ensure_thread_started(self) -> None:
# Fast path without holding the lock
Expand All @@ -601,6 +607,7 @@ def on_trace_start(self, trace: Trace) -> None:
try:
self._queue.put_nowait(trace)
except queue.Full:
self._dropped_count += 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Serialize concurrent dropped-count updates

When multiple application threads finish spans or start traces while the queue is full, both callbacks can read the same _dropped_count value before either stores its increment, causing the public metric to undercount dropped telemetry; this is especially reachable on free-threaded Python builds and other interpreters because += is not an atomic counter operation. Protect both increments and reads with a shared lock or another thread-safe counter, and cover the interleaving with a controlled concurrency test rather than only the sequential test.

AGENTS.md reference: AGENTS.md:L149-L149

Useful? React with 👍 / 👎.

logger.warning("Queue is full, dropping trace.")

def on_trace_end(self, trace: Trace) -> None:
Expand All @@ -618,6 +625,7 @@ def on_span_end(self, span: Span[Any]) -> None:
try:
self._queue.put_nowait(span)
except queue.Full:
self._dropped_count += 1
logger.warning("Queue is full, dropping span.")

def shutdown(self, timeout: float | None = None):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_trace_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ def test_batch_trace_processor_queue_full(mocked_exporter):
processor.shutdown()


def test_batch_trace_processor_dropped_count(mocked_exporter):
processor = BatchTraceProcessor(exporter=mocked_exporter, max_queue_size=2, schedule_delay=0.1)

# Fill the queue
processor.on_trace_start(get_trace(processor))
processor.on_trace_start(get_trace(processor))
assert processor.dropped_count == 0

# Next item should be dropped and counter incremented
processor.on_trace_start(get_trace(processor))
assert processor.dropped_count == 1

processor.on_span_end(get_span(processor))
assert processor.dropped_count == 2

processor.shutdown()


def test_batch_processor_doesnt_enqueue_on_trace_end_or_span_start(mocked_exporter):
processor = BatchTraceProcessor(exporter=mocked_exporter)

Expand Down