Files
FabledSteward/tests/plugins/host_agent/test_ingest_timing.py
T
bvandeusenandClaude Opus 5.5 2f208a84fb fix(host_agent): the server writes all five samples an agent post carries (#5181)
MAX_SAMPLES_PER_INGEST was 2 while agent 1.10 drains a backlog five samples a
post, so the server dropped the oldest three of every such post. Posts now
answer in 0.4-1.3 s on dev, including the boot cleanup window, so five fit
the agent's 10 s. A test holds the server cap at or above the agent's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:05:22 -04:00

84 lines
2.8 KiB
Python

"""A slow ingest says where its time went, and one dropped when the agent hung
up says how far it got (#5149)."""
import logging
from plugins.host_agent import routes
class _Clock:
def __init__(self):
self.t = 100.0
def __call__(self):
return self.t
async def test_a_slow_ingest_logs_its_phases(app, monkeypatch, caplog):
clock = _Clock()
monkeypatch.setattr(routes.time, "monotonic", clock)
async with app.app_context():
phases = routes._Phases()
clock.t += 0.1
phases.mark("parse")
clock.t += 8.0
phases.mark("metrics")
clock.t += 0.4
phases.mark("commit")
with caplog.at_level(logging.WARNING):
phases.report("web-1", 12, 480, 30, 900)
line = caplog.text
assert "slow 8.5s for host=web-1" in line
assert "parse=0.1s metrics=8.0s commit=0.4s" in line
assert "samples=12 metric_rows=480 containers=30 log_lines=900" in line
async def test_a_quick_ingest_logs_nothing(app, monkeypatch, caplog):
clock = _Clock()
monkeypatch.setattr(routes.time, "monotonic", clock)
async with app.app_context():
phases = routes._Phases()
clock.t += 0.3
phases.mark("commit")
with caplog.at_level(logging.WARNING):
phases.report("web-1", 1, 40, 0, 0)
assert "host_agent ingest" not in caplog.text
async def test_a_dropped_ingest_says_how_far_it_got(app, monkeypatch, caplog):
clock = _Clock()
monkeypatch.setattr(routes.time, "monotonic", clock)
async with app.app_context():
phases = routes._Phases()
clock.t += 0.2
phases.mark("parse")
phases.size = "samples=21 metric_rows=840"
clock.t += 9.9
with caplog.at_level(logging.WARNING):
phases.cancelled("abcd1234")
assert "dropped after 10.1s for token=abcd1234… (completed: parse=0.2s) samples=21 metric_rows=840" in caplog.text
def _s(minute):
return {"ts": f"2026-10-06T12:{minute:02d}:00+00:00", "cpu_pct": 1.0}
def test_a_full_backlog_is_cut_to_its_newest_samples():
# Sent oldest first (the agent's buffer, then the new sample) or not.
backlog = [_s(m) for m in range(21)]
kept, dropped = routes.newest_samples(list(reversed(backlog)), 5)
assert [s["ts"] for s in kept] == [_s(m)["ts"] for m in range(16, 21)]
assert dropped == 16
def test_a_small_post_is_left_alone():
posted = [_s(3), _s(1)]
assert routes.newest_samples(posted, 5) == (posted, 0)
def test_the_server_writes_every_sample_an_agent_post_carries():
# An agent drains its backlog MAX_SAMPLES_PER_POST at a time; a server cap
# below that drops the oldest of every such post (#5181).
from plugins.host_agent import agent
assert routes.MAX_SAMPLES_PER_INGEST >= agent.MAX_SAMPLES_PER_POST