Skip to content

Expose Management API rate limit info via a client callback - #798

Draft
ProdigyTom wants to merge 2 commits into
auth0:masterfrom
ProdigyTom:feat/v6-expose-rate-limit
Draft

Expose Management API rate limit info via a client callback#798
ProdigyTom wants to merge 2 commits into
auth0:masterfrom
ProdigyTom:feat/v6-expose-rate-limit

Conversation

@ProdigyTom

@ProdigyTom ProdigyTom commented Sep 1, 2026

Copy link
Copy Markdown

Changes

Exposes Auth0's rate-limit information (x-ratelimit-limit / -remaining / -reset) from every Management API response, so callers can monitor how close they are to the limit (the ask in #606). It's delivered via an opt-in callback rather than a changed return value — see the note below on why.

  • Auth0::Internal::Http::RateLimit — value object (limit, remaining as Integers; reset as a UTC Time). Blank/non-numeric header values become nil (never a misleading 0).
  • RawClient gains an optional rate_limit_handler, invoked in #send after retries on every response with the parsed RateLimit. Handler errors are swallowed so a monitoring callback can never break a request. #send's return value is unchanged, so the generated callers are unaffected.
  • Wiring: Auth0::Client.new(..., management_rate_limit_handler: ->(rl) { ... }) attaches the handler to the management raw client.
  • All changes live in fernignored files (lib/auth0/internal/**, lib/auth0/mixins/**, lib/auth0/auth_client.rb), so they survive regeneration.
  • Unit tests for RateLimit, RawClient#send handler behavior, and the client wiring.
client = Auth0::Client.new(
  domain: ENV["AUTH0_DOMAIN"],
  token: ENV["AUTH0_MGMT_TOKEN"],
  management_rate_limit_handler: lambda do |rate_limit|
    StatsD.gauge("auth0.mgmt.remaining", rate_limit.remaining) if rate_limit.remaining
  end
)
client.users.get(id: "auth0|123") # handler fires with the response's rate limit

Why a callback instead of returning the rate limit (e.g. with_raw_response)? The generated endpoint methods (Auth0::Users::Client#get, etc.) parse the body and discard the response, and they're regenerated, so a third-party change to what they return wouldn't survive. A callback invoked at the maintained RawClient layer is the part we can change durably while still delivering the data on every request. If you'd prefer to also expose it as a return value via a generated with_raw_response-style accessor, this RateLimit object slots right in.

References

Testing

Unit tests under test/unit/ cover header parsing (0 vs blank/non-numeric), the handler firing with the parsed rate limit while the response is returned unchanged, handler errors not breaking the request, and the client wiring. Full rake test passes locally with no failures.

  • This change adds unit test coverage
  • This change adds integration test coverage
  • This change has been tested on the latest version of Ruby

Checklist

  • I have read the Auth0 general contribution guidelines
  • I have read the Auth0 Code of Conduct
  • All existing and new tests complete without errors
  • Rubocop passes on all added/modified files
  • All active GitHub checks have passed

Wraps the RawClient#send return value in a RawResponse that delegates #code,
#body, and header access to the underlying response (so the generated callers
are unaffected) and adds a #rate_limit built from the x-ratelimit-* headers.

- Add Auth0::Internal::Http::RateLimit (limit/remaining/reset; blank and
  non-numeric header values become nil rather than 0)
- Add Auth0::Internal::Http::RawResponse wrapper
- RawClient#send returns the wrapper (retry logic still operates on the raw
  response inside the loop; only the final response is wrapped)
- Unit tests for RateLimit, RawResponse, and RawClient#send

All changes live in fernignored files (lib/auth0/internal/**), so they survive
regeneration. Refs auth0#606.
Adds an opt-in callback, invoked with the rate limit parsed from every
Management API response, so callers can monitor how close they are to the
limit. Chosen over changing endpoint return values because the response is
dropped in generated endpoint code that a third-party change can't durably
alter; the callback lives entirely in fernignored files.

- Add Auth0::Internal::Http::RateLimit (limit/remaining/reset; blank and
  non-numeric header values become nil rather than 0)
- RawClient gains a rate_limit_handler, invoked in #send after retries on
  every response; handler errors are swallowed so they can't break a request
- Wire it through the custom client: Auth0::Client.new(management_rate_limit_handler:)
  attaches the handler to the management raw client
- Unit tests for RateLimit, RawClient#send handler behavior, and client wiring

Refs auth0#606.
@ProdigyTom ProdigyTom changed the title Expose rate limit info on Management API responses Expose Management API rate limit info via a client callback Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant