Skip to content

Only start tracing worker thread on first span/trace #804

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
Jun 2, 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: 29 additions & 3 deletions src/agents/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,27 @@ def __init__(
# Track when we next *must* perform a scheduled export
self._next_export_time = time.time() + self._schedule_delay

self._worker_thread = threading.Thread(target=self._run, daemon=True)
self._worker_thread.start()
# We lazily start the background worker thread the first time a span/trace is queued.
self._worker_thread: threading.Thread | None = None
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not start a task + asyncio q, then you don't have an extra thread to worry about and you can start the task unconditionally

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm I was thinking:

  1. small overhead in cases where there are no traces (the unconditional task would still be scheduled every poll_interval seconds, even though it doesnt have work)
  2. It wouldn't work super well in totally sync contexts
  3. cleaning up the asyncio task is kinda annoying imo

lmk if you feel like I should change, happy to.

Copy link
Contributor

Choose a reason for hiding this comment

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

No strong opinions

self._thread_start_lock = threading.Lock()

def _ensure_thread_started(self) -> None:
# Fast path without holding the lock
if self._worker_thread and self._worker_thread.is_alive():
return

# Double-checked locking to avoid starting multiple threads
with self._thread_start_lock:
if self._worker_thread and self._worker_thread.is_alive():
return

self._worker_thread = threading.Thread(target=self._run, daemon=True)
self._worker_thread.start()

def on_trace_start(self, trace: Trace) -> None:
# Ensure the background worker is running before we enqueue anything.
self._ensure_thread_started()

try:
self._queue.put_nowait(trace)
except queue.Full:
Expand All @@ -206,6 +223,9 @@ def on_span_start(self, span: Span[Any]) -> None:
pass

def on_span_end(self, span: Span[Any]) -> None:
# Ensure the background worker is running before we enqueue anything.
self._ensure_thread_started()

try:
self._queue.put_nowait(span)
except queue.Full:
Expand All @@ -216,7 +236,13 @@ def shutdown(self, timeout: float | None = None):
Called when the application stops. We signal our thread to stop, then join it.
"""
self._shutdown_event.set()
self._worker_thread.join(timeout=timeout)

# Only join if we ever started the background thread; otherwise flush synchronously.
if self._worker_thread and self._worker_thread.is_alive():
self._worker_thread.join(timeout=timeout)
else:
# No background thread: process any remaining items synchronously.
self._export_batches(force=True)

def force_flush(self):
"""
Expand Down