fix(traefik): grade routers on their last 15 minutes; the Config tile reads Traefik's reload time (#5355)
CI / lint (push) Failing after 2s
CI / integration (push) Successful in 54s
CI / unit (push) Successful in 1m24s
CI / publish (push) Skipped

The operator: problem cards "can disappear so quickly". Each router was
graded on its latest scrape, so a 4xx share near the 1% cutoff crossed it
on one scrape and fell back on the next. recent_window reads every
router's last 15 minutes in one query, the error shares weighted by
traffic; grade_routers uses it on the page and the dashboard widget, and
the why says "over the last 15 min".

traefik_config_last_reload_success is the Unix time of the last good
reload, not a 0/1 flag, so the tile said "Reload failed" on every scrape.
traefik_005 stores it as config_last_reload_at, and the tile reads
"Config reloaded 3m ago". The router index also carries the 4xx share.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 08:26:10 -04:00
co-authored by Claude Opus 5.5
parent 7a444da871
commit 7ce61a2099
7 changed files with 194 additions and 24 deletions
+31
View File
@@ -108,3 +108,34 @@ def test_router_history_reads_every_router_in_one_query_bucketed(app):
assert got[a][0].latency_p95_ms == 20.0
assert [r.request_rate for r in got[b]] == [4.0]
assert got[idle] == [] # its only row is before the range
def test_recent_window_weights_shares_by_traffic_and_keeps_to_the_window(app):
"""#5214: a router is graded on its last 15 minutes. An idle scrape's
100% 4xx counts for its trickle of traffic, not as a third of the
average; a row older than the window is not read."""
from plugins.traefik.models import TraefikMetric
from plugins.traefik.routes import recent_window
now = datetime(2097, 3, 1, 12, 0, tzinfo=timezone.utc) + timedelta(minutes=uuid.uuid4().int % 10_000)
tag = uuid.uuid4().hex[:8]
busy, empty = f"busy-{tag}@docker", f"empty-{tag}@docker"
def row(name, ago, rate, e4, p95=10.0):
return TraefikMetric(router_name=name, scraped_at=now - timedelta(minutes=ago), request_rate=rate,
error_rate_4xx_pct=e4, error_rate_5xx_pct=0.0, latency_p50_ms=1.0,
latency_p95_ms=p95, latency_p99_ms=3.0, response_bytes_rate=0.0)
async def go():
async with app.db_sessionmaker() as s:
async with s.begin():
s.add_all([row(busy, 1, 9.9, 0.0, 10.0), row(busy, 2, 0.1, 100.0, 30.0),
row(busy, 30, 50.0, 100.0), # outside the window
row(empty, 30, 1.0, 100.0)])
async with s.begin():
return await recent_window(s, [busy, empty], now)
got = asyncio.run(go())
assert set(got) == {busy} # nothing in empty's window
assert got[busy].error_rate_4xx_pct == pytest.approx(1.0) # 0.1 × 100 over 10 req/s
assert got[busy].latency_p95_ms == pytest.approx(20.0)