Skip to content

feat: expose rate limit headers via on_rate_limit_info callback#347

Merged
mikeh-lago merged 2 commits into
mainfrom
feat/rate-limit-observability
May 5, 2026
Merged

feat: expose rate limit headers via on_rate_limit_info callback#347
mikeh-lago merged 2 commits into
mainfrom
feat/rate-limit-observability

Conversation

@mikeh-lago
Copy link
Copy Markdown
Contributor

Adds an optional on_rate_limit_info callback on Lago::Api::Client so callers can read x-ratelimit-* headers from successful responses. Today the SDK drops them on 2xx, which makes 80/90/95% threshold observability impossible without poking at internals. The 429 path (via RateLimitError) is unchanged. Mirrors PRs getlago/lago-python-client#393 and getlago/lago-go-client#336.

Notes for review

  • New option is optional and defaults to nil. Backward-compatible.
  • Callback fires only on successful responses with rate limit headers present.
  • Callback errors are rescued and surfaced via warn so a buggy observer can't break a request.
  • Sync, runs inline before returning to the caller.

Tests

New spec/lago/api/rate_limit_observability_spec.rb with coverage for RateLimitInfo (usage_pct + parse), the callback path (success, missing headers, error swallow, single-fire after 429-then-200), LoggingRateLimitObserver, and Client wiring. Existing rate limit specs untouched.

@mikeh-lago mikeh-lago force-pushed the feat/rate-limit-observability branch 2 times, most recently from 7cdb19e to eca14c2 Compare May 5, 2026 10:52
Surface x-ratelimit-* headers from successful responses so callers can
build observability around the rate limit (warn at thresholds, emit
metrics) without inspecting internals.

- Add Lago::Api::RateLimitInfo with usage_pct
- Add on_rate_limit_info option on Client and Connection
- Parse and emit headers on every non-429 response in Connection
- Ship Lago::Api::LoggingRateLimitObserver as a zero-config observer (defaults: 80/90/95%)
- Callback errors are rescued and warned so they cannot break requests
- Backward-compatible: callback is optional, default behavior unchanged
Comment thread lib/lago/api/connection.rb
Comment thread lib/lago/api/connection.rb Outdated
Address review feedback from @vincent-pochet on PR #347:
declare the method string once per action method and reuse it for both
execute_request and http_client.send_request, instead of repeating the
literal twice.
@mikeh-lago mikeh-lago merged commit 124b091 into main May 5, 2026
6 checks passed
@mikeh-lago mikeh-lago deleted the feat/rate-limit-observability branch May 5, 2026 15:50
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.

2 participants