feat: add pool stats to periodic metrics log
Append pool=alive/total to the 60-second metrics summary and shutdown log. Pool health is now visible without waiting for health test cycles.
This commit is contained in:
@@ -192,7 +192,11 @@ async def _handle_client(
|
|||||||
# -- entry point -------------------------------------------------------------
|
# -- entry point -------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
async def _metrics_logger(metrics: Metrics, stop: asyncio.Event) -> None:
|
async def _metrics_logger(
|
||||||
|
metrics: Metrics,
|
||||||
|
stop: asyncio.Event,
|
||||||
|
pool: ProxyPool | None = None,
|
||||||
|
) -> None:
|
||||||
"""Log metrics summary every 60 seconds."""
|
"""Log metrics summary every 60 seconds."""
|
||||||
while not stop.is_set():
|
while not stop.is_set():
|
||||||
try:
|
try:
|
||||||
@@ -200,7 +204,10 @@ async def _metrics_logger(metrics: Metrics, stop: asyncio.Event) -> None:
|
|||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
pass
|
pass
|
||||||
if not stop.is_set():
|
if not stop.is_set():
|
||||||
logger.info("metrics: %s", metrics.summary())
|
line = metrics.summary()
|
||||||
|
if pool:
|
||||||
|
line += f" pool={pool.alive_count}/{pool.count}"
|
||||||
|
logger.info("metrics: %s", line)
|
||||||
|
|
||||||
|
|
||||||
async def serve(config: Config) -> None:
|
async def serve(config: Config) -> None:
|
||||||
@@ -247,13 +254,17 @@ async def serve(config: Config) -> None:
|
|||||||
loop.add_signal_handler(sig, lambda s=sig: stop.set_result(s))
|
loop.add_signal_handler(sig, lambda s=sig: stop.set_result(s))
|
||||||
|
|
||||||
metrics_stop = asyncio.Event()
|
metrics_stop = asyncio.Event()
|
||||||
metrics_task = asyncio.create_task(_metrics_logger(metrics, metrics_stop))
|
pool_ref = proxy_pool if isinstance(proxy_pool, ProxyPool) else None
|
||||||
|
metrics_task = asyncio.create_task(_metrics_logger(metrics, metrics_stop, pool_ref))
|
||||||
|
|
||||||
async with srv:
|
async with srv:
|
||||||
sig = await stop
|
sig = await stop
|
||||||
logger.info("received %s, shutting down", signal.Signals(sig).name)
|
logger.info("received %s, shutting down", signal.Signals(sig).name)
|
||||||
if isinstance(proxy_pool, ProxyPool):
|
if isinstance(proxy_pool, ProxyPool):
|
||||||
await proxy_pool.stop()
|
await proxy_pool.stop()
|
||||||
logger.info("metrics: %s", metrics.summary())
|
shutdown_line = metrics.summary()
|
||||||
|
if pool_ref:
|
||||||
|
shutdown_line += f" pool={pool_ref.alive_count}/{pool_ref.count}"
|
||||||
|
logger.info("metrics: %s", shutdown_line)
|
||||||
metrics_stop.set()
|
metrics_stop.set()
|
||||||
await metrics_task
|
await metrics_task
|
||||||
|
|||||||
Reference in New Issue
Block a user