Skip to content

feat: expose rate limit headers via on_rate_limit_info callback#41

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

feat: expose rate limit headers via on_rate_limit_info callback#41
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 Config 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 inspecting internals. The 429 path is unchanged. Mirrors PRs getlago/lago-python-client#393, getlago/lago-go-client#336, getlago/lago-ruby-client#347, and getlago/lago-javascript-client#86.

Notes for review

  • New option is Option<Arc<dyn Fn(&RateLimitInfo) + Send + Sync>> so the client stays Clone and Send + Sync.
  • Callback fires only on successful responses with rate limit headers present.
  • Callback panics are caught with std::panic::catch_unwind and logged so a buggy observer can't break a request.
  • RateLimitInfo gained method, url fields and a usage_pct() helper. Default is now derived so existing struct-literal tests use ..Default::default().
  • Two pre-existing clippy::collapsible_if warnings in unrelated examples (invoice.rs, credit_note.rs) were folded in to keep CI green; happy to split out if you'd prefer.

Tests

5 new tests in client.rs (usage_pct happy + unusable, success path, missing-headers, panic recovery) plus 3 in observability.rs. Backward-compatible — all existing tests pass after ..Default::default() updates.

Surface x-ratelimit-* headers from successful responses so callers can
build observability around the rate limit (warn at thresholds, emit
metrics) without inspecting internals.

- Extend RateLimitInfo with method, url, and usage_pct() helper
- Add RateLimitInfoCallback type alias (Arc<dyn Fn + Send + Sync>)
- Add on_rate_limit_info option on Config and ConfigBuilder
- Parse and emit headers on every successful response in make_request
- Ship logging_rate_limit_observer as a zero-config observer (defaults: 80/90/95%)
- Callback panics are caught with std::panic::catch_unwind so they cannot break requests
- Fix pre-existing clippy collapsible_if warnings in invoice and credit_note examples
- Backward-compatible: option is optional, default behavior unchanged
@mikeh-lago mikeh-lago force-pushed the feat/rate-limit-observability branch from 8ebc8c6 to 5c73b17 Compare May 7, 2026 14:10
@mikeh-lago mikeh-lago requested review from jdenquin and rsempe and removed request for jdenquin May 11, 2026 15:22
@mikeh-lago mikeh-lago merged commit 5e04c3b into main May 11, 2026
1 check passed
@mikeh-lago mikeh-lago deleted the feat/rate-limit-observability branch May 11, 2026 15:33
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