Superuser route to flush the metrics collector now

POST /observability/flush handles what the collector has queued and writes
it, so the soak harness no longer sleeps FLUSH_INTERVAL_S before clearing
its own rows.
This commit is contained in:
2026-10-15 09:53:19 +02:00
parent ae81d95fd7
commit 290b9fa705
8 changed files with 106 additions and 11 deletions
+17
View File
@@ -103,6 +103,7 @@ class MetricsCollector:
# held with the time it arrived so an unpaired one does not stay put.
self._tracebacks: dict[tuple[str, str], tuple[float, str]] = {}
self._last_prune = 0.0
self._queue: asyncio.Queue[dict[str, Any]] | None = None
# -------------------------------------------------------------------------
# The loop
@@ -112,6 +113,7 @@ class MetricsCollector:
"""Consume the bus until cancelled, flushing on a fixed interval."""
last = time.monotonic()
async with self._events.subscribe() as queue:
self._queue = queue
while True:
# A busy bus never idles, so the flush is on a deadline rather
# than on the timeout alone.
@@ -129,6 +131,21 @@ class MetricsCollector:
await self.flush()
last = time.monotonic()
async def flush_now(self) -> None:
"""Write what has been heard so far, without waiting for the interval.
Events the bus has handed over and the loop has not reached yet are
taken first, so whoever caused an event and then asks for a flush finds
it written.
"""
while self._queue is not None and not self._queue.empty():
event = self._queue.get_nowait()
try:
self.handle(event)
except Exception:
logger.exception("Could not record %s", event.get("type"))
await self.flush()
# -------------------------------------------------------------------------
# Accumulating
# -------------------------------------------------------------------------