Skip to content

Semaphore: trace time spent waiting for permits #10693

Open

Description

@thewilkybarkid

What is the problem this feature would solve?

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…
const limit = yield* Semaphore.make(3, { waitSpan: 'SomeService.waitForPermit' })

// …or when taking permits
effect.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:

    effect =>
      Effect.scoped(
        Effect.andThen(Effect.withSpan(Semaphore.takeScoped(limit, 1), 'SomeService.waitForPermit'), effect),
      )

    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.

Tested with effect 4.1.2.

Activity

  1. added
    enhancement
    on Nov 17, 2026
  2. self-assigned this
    on Nov 17, 2026
  3. thewilkybarkid commented on Nov 17, 2026

    @thewilkybarkid
    ContributorAuthor

    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions