Replace timer-based span buffering with streaming-onEnd micro-batching#175
Draft
Replace timer-based span buffering with streaming-onEnd micro-batching#175
Conversation
Co-authored-by: fpfp100 <126631706+fpfp100@users.noreply.github.com>
Co-authored-by: fpfp100 <126631706+fpfp100@users.noreply.github.com>
…mentation Co-authored-by: fpfp100 <126631706+fpfp100@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update PerRequestSpanProcessor to use streaming-onEnd approach
Replace timer-based span buffering with streaming-onEnd micro-batching
Jan 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Timer-based flush logic (grace period, max-age sweeps) drops late-ending spans when trace state is deleted. Export latency is unpredictable and memory usage scales with buffered span count.
Changes
Streaming export on span end
setImmediatemicro-batching (default batch size: 64)LRU eviction replaces trace dropping
maxBufferedTracesreached, evict least-recently-active entryData structure changes
Removed
ensureSweepTimer(),stopSweepTimerIfIdle(),sweep()methodsflushGraceMs,maxTraceAgeMsusage (parameters retained for compatibility)sweepTimer,isSweepingfieldsAdded
scheduleMicroBatchFlush(): next-tick export viasetImmediateevictLeastRecentlyUsed(): capacity management with linear scan (O(n), acceptable for default limit of 1000)A365_PER_REQUEST_MAX_BATCH_SIZEenvironment variableToken correctness preserved
context.with(rootCtx ?? fallbackCtx)as beforeMemory footprint
maxBufferedTraces=1000Original prompt
Summary
Switch PerRequestSpanProcessor to a streaming-onEnd span export approach with minor micro-batching to eliminate timeout-induced drops of late-ending spans while preserving token correctness by exporting under the saved request Context (rootCtx). Remove sweeper and time-based flushes, retain only per-trace Context and a tiny queue, and keep concurrency guardrails. Add idle/LRU eviction for contexts to bound memory while avoiding per-trace timeouts.
Background and current behavior
The existing PerRequestSpanProcessor buffers ended spans per trace and flushes them based on:
On flush, the trace entry is deleted. Any spans that end afterward (common for long-running children) will find no buffer and are effectively dropped because onEnd returns early when buf is missing.
Snippet showing current export call (from the original file):
Problem
Goals
Proposed approach: Streaming on onEnd with micro-batching (Method 1)
Detailed changes
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.