fix: report the real API rate-limit budget from response headers - #26
Merged
Merged
Conversation
GET /rate_limit reported used=0 for our tokens, so github_rate_limit_remaining
sat at 5000 and the low-budget alert could never fire. Read X-RateLimit-* from
every API response instead, as GitHub documents. GitHub counts per region, so
one token can be in several core windows at once: track each by reset time,
report the scarcest as github_rate_limit_remaining, and expose every window as
github_rate_limit_window_remaining{reset}. Drops one request per org refresh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
github_rate_limit_remainingsat at 5000/5000 almost permanently: over 7 days it dipped only once, to 4962. That's becauseGET /rate_limitreportsused: 0for our tokens. The headers on ordinary API calls showed the real count at the same time, e.g.used: 1210. As a result,GithubApiRateLimitLowcould never fire.What GitHub is doing
According to GitHub's REST rate-limit docs:
x-ratelimit-*headers are authoritative over/rate_limit.Measured with one token, it's counted in two
corewindows at once, each with its own reset time and count:/orgs/{o}/actions/runners,/repos/{r}/actions/workflows,/repos/{r}/dependabot/alerts/user,/orgs/{o}, repos, pulls, issues, commits,/actions/runs, contents)Changes
X-RateLimit-Limit/Remaining/Resetheaders of every core response and keeps one entry per window (keyed by reset time). Within a window it keeps the lowest count seen. Windows drop off once they reset.github_rate_limit_remaining/_limitnow report the scarcest active window, the one that runs out first, so the alert and dashboard need no changes. They're read at scrape time from the client, so the 15s runner poll keeps them fresh. They were previously part of the 5-minute org snapshot.github_rate_limit_window_remaining{reset}: one series per active window./rate_limitcall is gone, which saves one request per org refresh.Verification
go test -race ./...passes. The new client test covers two concurrent windows, a non-core resource being ignored, and window expiry.github_rate_limit_window_remaining{reset="17:43:05Z"} 3367and{reset="18:12:54Z"} 4797. Live headers moments later read 3365 and 4796, andgithub_rate_limit_remaining= 3367.