Skip to content

[Tracing] Scheduled exports use wall clock and are sensitive to clock changes #4057

Closed

Description

@GautamSharma99

Describe the bug

BatchTraceProcessor implements a relative scheduling deadline using time.time():

  • Initial deadline:
    # The queue size threshold at which we export immediately.
    self._export_trigger_size = max(1, int(max_queue_size * export_trigger_ratio))
    # Track when we next *must* perform a scheduled export
    self._next_export_time = time.time() + self._schedule_delay
  • Deadline comparison/reset:
    def _run(self):
    while not self._shutdown_event.is_set():
    current_time = time.time()
    queue_size = self._queue.qsize()
    # If it's time for a scheduled flush or queue is above the trigger threshold
    if current_time >= self._next_export_time or queue_size >= self._export_trigger_size:
    self._export_batches()
    # Reset the next scheduled flush time
    self._next_export_time = time.time() + self._schedule_delay
    else:
    # Sleep a short interval so we don't busy-wait.
    time.sleep(0.2)

Wall-clock time is adjustable. If the clock moves backward, a scheduled batch can remain queued much longer than schedule_delay. If it moves forward, a flush can be triggered immediately. This class already uses time.monotonic() for shutdown deadlines, so its two elapsed-time mechanisms currently follow inconsistent clocks.

Debug information

  • Agents SDK version: v0.19.1
  • Audited commit: 000a96b602889b00f7cfaa210c41e1a74be65272
  • Python version: Python 3.10+
  • Evidence type: static scheduling inspection

Repro steps

A deterministic unit regression can patch the clock sources:

  1. Construct a processor with a long queue threshold so only scheduled export triggers.
  2. Enqueue an item.
  3. Advance the monotonic clock past schedule_delay while moving the wall clock backward.
  4. Under the current implementation, the scheduled comparison remains false and the item is not exported.
  5. Repeat with a forward wall-clock jump to show an early trigger.

The test need not sleep in real time; the scheduling decision can be isolated with a recording exporter and controlled clock values.

Expected behavior

A configured delay should measure elapsed process time and should be unaffected by NTP corrections, host time synchronization, daylight/time configuration, or manual clock changes. All relative deadlines in the processor should use a monotonic clock.

Why this matters

Tracing is commonly buffered specifically to reduce request overhead. Unexpectedly delayed batches increase the chance that telemetry remains in memory and is lost during abnormal termination; premature batches undermine batching efficiency.

Suggested scope

Use time.monotonic() consistently for _next_export_time initialization, comparison, and reset. Add a controlled-clock regression test.

I searched current tracing issues and PRs for scheduled flush clocks, monotonic deadlines, and clock adjustment and found no active duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions