You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Semaphore: trace time spent waiting for permits #10693
When a Semaphore limits concurrency, time spent waiting for a permit is invisible in traces.
We limit concurrent requests to an external API with Semaphore.make(3) and withPermits(1). When the limit is reached, the trace shows the calling span start, then nothing for hundreds of milliseconds, then the request's span. We only found the cause by comparing start times across the whole trace. In one page load, 5 of 8 requests waited between 370 ms and 690 ms for a permit.
withPermits waits and runs the effect in one step, so there's nowhere to put a span around just the wait.
What is the feature you are proposing to solve the problem?
An option to record permit waits, for example as a span:
// when making the semaphore…constlimit=yield*Semaphore.make(3,{waitSpan: 'SomeService.waitForPermit'})// …or when taking permitseffect.pipe(limit.withPermits(1,{waitSpan: 'SomeService.waitForPermit'}))
The span would cover the time from asking for permits to getting them, as a child of the current span, keeping withPermits' interruption and release behaviour.
The option names are just illustrations.
What alternatives have you considered?
Our current workaround swaps withPermits(1) for takeScoped wrapped in a span:
It works, but it's easy to get wrong (it relies on takeScoped releasing the permit when the scope closes) and adds a scope to every call.
Adding a span event or attribute to the current span (e.g. the wait duration) only when a call actually had to wait. This would add nothing on the uncontended path.
Documenting the takeScoped + withSpan pattern as the recommended approach.
The same gap shows up with HttpClient.withRateLimiter (and RateLimiter.sleep / RateLimiter.makeWithRateLimiter): when a request is delayed, nothing in the trace says so. The wait appears only as an empty stretch between the caller's span starting and the request's own span beginning.
For example, we rate limit an API client to its provider's published limit (algorithm: 'token-bucket', limit: 12, window: '1 second'). A page that needs 70 uncached lookups spends most of its five seconds waiting for tokens, but nothing in the trace shows that the gap before each GET is the rate limiter.
withRateLimiter can sleep in several places, none of them visible:
waiting for a token from limiter.consume
waiting for a reported budget to reset
honoring Retry-After on a 429
adaptive backoff
It would help to have a span around each wait, for example HttpClient.withRateLimiter.wait, with attributes for the rate-limit key, the delay in milliseconds, and the reason (token, budget, retry-after or adaptive). A span event on the current span would also work, if a span per wait is too heavy.
The workarounds have the same limits as with semaphores:
Wrapping the client in our own span only covers the wait and the request together.
Calling RateLimiter.sleep ourselves loses withRateLimiter's 429 and header handling.
Activity
thewilkybarkid commentedon Nov 17, 2026
The same gap shows up with
HttpClient.withRateLimiter(andRateLimiter.sleep/RateLimiter.makeWithRateLimiter): when a request is delayed, nothing in the trace says so. The wait appears only as an empty stretch between the caller's span starting and the request's own span beginning.For example, we rate limit an API client to its provider's published limit (
algorithm: 'token-bucket',limit: 12,window: '1 second'). A page that needs 70 uncached lookups spends most of its five seconds waiting for tokens, but nothing in the trace shows that the gap before eachGETis the rate limiter.withRateLimitercan sleep in several places, none of them visible:limiter.consumeRetry-Afteron a 429It would help to have a span around each wait, for example
HttpClient.withRateLimiter.wait, with attributes for the rate-limitkey, the delay in milliseconds, and the reason (token,budget,retry-afteroradaptive). A span event on the current span would also work, if a span per wait is too heavy.The workarounds have the same limits as with semaphores:
RateLimiter.sleepourselves loseswithRateLimiter's 429 and header handling.