mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
fix(fetcher): keep last-good counts on GitHub rate limits + expose fetch metrics
Rate-limited / 5xx / network failures were silently returning 0 from the
commit- and PR-count paths, then overwriting good values in fetcher.db —
turning a traffic spike into permanently-wrong "0 commits" cards. Raise
GitHubTransientError instead so the fetch aborts and the last-good row is
retained; genuine NOT_FOUND still yields 0.
Add a shared fetch-outcome counter (fetch_metrics table, spanning the web
workers and the cron container) surfaced at an unauthenticated GET /metrics
as ghstats_fetch_total{outcome="ok|not_found|rate_limited|error"}, so rate
limits are visible to the monitoring stack instead of dying silently.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -30,6 +30,20 @@ def test_endpoints_require_internal_token(client):
|
||||
assert r.status_code == 401
|
||||
|
||||
|
||||
def test_metrics_endpoint_no_auth_and_prometheus_format(client):
|
||||
from src import db as dbmod
|
||||
dbmod.bump_fetch_metric("ok")
|
||||
dbmod.bump_fetch_metric("rate_limited")
|
||||
r = client.get("/metrics")
|
||||
assert r.status_code == 200
|
||||
body = r.get_data(as_text=True)
|
||||
assert "# TYPE ghstats_fetch_total counter" in body
|
||||
assert 'ghstats_fetch_total{outcome="ok"} 1' in body
|
||||
assert 'ghstats_fetch_total{outcome="rate_limited"} 1' in body
|
||||
# Every outcome is emitted even at zero so rate() has a baseline sample.
|
||||
assert 'ghstats_fetch_total{outcome="not_found"} 0' in body
|
||||
|
||||
|
||||
@resp_lib.activate
|
||||
def test_data_auto_fetches_on_miss(client, monkeypatch):
|
||||
# The fetcher fires the independent GitHub calls in parallel, so we
|
||||
|
||||
@@ -4,6 +4,7 @@ import pytest
|
||||
from unittest.mock import patch
|
||||
from src import db as dbmod
|
||||
from src import cron as cronmod
|
||||
from src import github as ghmod
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -26,6 +27,22 @@ def test_refresh_tick_calls_github_for_due_users(tmp_db):
|
||||
assert stats["refreshed"] == 1
|
||||
|
||||
|
||||
def test_refresh_rate_limit_keeps_last_good_and_counts_metric(tmp_db):
|
||||
# A rate-limited refresh must NOT overwrite the cached row, and must be
|
||||
# counted as rate_limited so the monitoring stack can see it.
|
||||
dbmod.upsert_user("alice", {"user": {"login": "alice"}, "total_commits": 1841})
|
||||
with dbmod._connect() as c:
|
||||
c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z', last_requested_at=? WHERE username='alice'",
|
||||
(dbmod._now(),))
|
||||
with patch("src.cron.github.fetch_github_data",
|
||||
side_effect=ghmod.GitHubTransientError("rate limited")):
|
||||
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
|
||||
assert stats["failed"] == 1 and stats["refreshed"] == 0
|
||||
# Last-good data retained.
|
||||
assert dbmod.get_user("alice")["data"]["total_commits"] == 1841
|
||||
assert dbmod.read_fetch_metrics()["rate_limited"] == 1
|
||||
|
||||
|
||||
def test_tick_runs_gc_for_abandoned_trial_users(tmp_db):
|
||||
dbmod.upsert_user("ghost", {"user": {"login": "ghost"}})
|
||||
with dbmod._connect() as c:
|
||||
|
||||
@@ -30,6 +30,23 @@ def test_upsert_and_get_round_trip(tmp_db):
|
||||
assert row["fetched_at"]
|
||||
|
||||
|
||||
def test_fetch_metrics_round_trip(tmp_db):
|
||||
# Zero-filled for every known outcome before anything is recorded.
|
||||
assert dbmod.read_fetch_metrics() == {o: 0 for o in dbmod.FETCH_OUTCOMES}
|
||||
dbmod.bump_fetch_metric("ok")
|
||||
dbmod.bump_fetch_metric("ok")
|
||||
dbmod.bump_fetch_metric("rate_limited")
|
||||
counts = dbmod.read_fetch_metrics()
|
||||
assert counts["ok"] == 2
|
||||
assert counts["rate_limited"] == 1
|
||||
assert counts["not_found"] == 0
|
||||
|
||||
|
||||
def test_fetch_metric_unknown_outcome_buckets_to_error(tmp_db):
|
||||
dbmod.bump_fetch_metric("bogus")
|
||||
assert dbmod.read_fetch_metrics()["error"] == 1
|
||||
|
||||
|
||||
def test_payload_hash_is_deterministic_and_changes_on_diff():
|
||||
h1 = dbmod.payload_hash({"a": 1, "b": 2})
|
||||
h2 = dbmod.payload_hash({"b": 2, "a": 1})
|
||||
|
||||
@@ -73,3 +73,44 @@ def test_fetch_handles_404():
|
||||
)
|
||||
data = github.fetch_github_data("nope", token="t")
|
||||
assert data["user"].get("message") == "Not Found" or data["user"] is None
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_commit_count_raises_on_graphql_rate_limit():
|
||||
# GraphQL reports a rate limit as HTTP 200 with an `errors` array — the
|
||||
# exact shape a valid response uses. It must NOT be swallowed into a 0
|
||||
# that would overwrite the last good count in fetcher.db.
|
||||
responses.add(
|
||||
responses.POST, "https://api.github.com/graphql",
|
||||
json={"data": {"user": None},
|
||||
"errors": [{"type": "RATE_LIMITED", "message": "API rate limit exceeded"}]},
|
||||
status=200,
|
||||
)
|
||||
src = github.DirectAPISource(token="t")
|
||||
with pytest.raises(github.GitHubTransientError):
|
||||
src.fetch_commit_count("alice", [])
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_commit_count_raises_on_rest_rate_limit():
|
||||
# A 403 with the remaining budget at 0 is a rate limit, not a real answer.
|
||||
responses.add(
|
||||
responses.POST, "https://api.github.com/graphql",
|
||||
json={"message": "rate limited"}, status=403,
|
||||
headers={"X-RateLimit-Remaining": "0"},
|
||||
)
|
||||
src = github.DirectAPISource(token="t")
|
||||
with pytest.raises(github.GitHubTransientError):
|
||||
src.fetch_commit_count("alice", [])
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_commit_count_zero_on_not_found_is_not_transient():
|
||||
# A genuinely unknown user (NOT_FOUND, no rate-limit error) is a real,
|
||||
# persistable "no data" answer — return 0, don't raise.
|
||||
responses.add(
|
||||
responses.POST, "https://api.github.com/graphql",
|
||||
json={"data": {"user": None}}, status=200,
|
||||
)
|
||||
src = github.DirectAPISource(token="t")
|
||||
assert src.fetch_commit_count("ghost", []) == 0
|
||||
|
||||
Reference in New Issue
Block a user