feat: propagate trace context through outbox and add pipeline saturation metrics

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 19:54:37 +03:00
co-authored by Claude Opus 5.5
parent ff0073e8e3
commit 72cfa9aaf6
64 changed files with 4699 additions and 552 deletions
+85
View File
@@ -0,0 +1,85 @@
"""`CountingQueuePool` на настоящем engine: ожидающие и отказы по сроку пула
(design change `job-trace-propagation`, решение 10; спек `observability`,
«Пул исчерпан»)."""
import asyncio
import logging
import pytest
from sqlalchemy import exc, text
from conftest import make_engine
from src.infra.db.engine import CountingQueuePool
pytestmark = pytest.mark.anyio
async def test_pool_counts_pending_and_timeouts() -> None:
engine = make_engine(
pool_size=1,
max_overflow=0,
pool_timeout_seconds=0.5,
)
try:
pool = engine.sync_engine.pool
assert isinstance(pool, CountingQueuePool)
assert (pool.pending(), pool.timeouts()) == (0, 0)
async with engine.connect() as held:
await held.execute(text("SELECT 1"))
async def second() -> None:
async with engine.connect() as connection:
await connection.execute(text("SELECT 1"))
waiting = asyncio.create_task(second())
for _ in range(100):
if pool.pending() == 1:
break
await asyncio.sleep(0.01)
# ждёт места, пока первое соединение занято
assert pool.pending() == 1
with pytest.raises(exc.TimeoutError):
await waiting
assert pool.pending() == 0
assert pool.timeouts() == 1
# место свободно — выдача без ожидания и без отказа
async with engine.connect() as connection:
await connection.execute(text("SELECT 1"))
assert (pool.pending(), pool.timeouts()) == (0, 1)
finally:
await engine.dispose()
async def test_pool_logger_stays_quiet() -> None:
"""Логгер пула назван по классу в `src`: журнал держит его на WARNING,
как прежний пул SQLAlchemy, — «Pool disposed» и «Pool recreating» INFO не
пишутся (найдено в apply по журналу консольного relay)."""
from src.infra.logger import get_logger
from src.utils.constants import DB_POOL_LOGGER
get_logger()
engine = make_engine(pool_size=1)
try:
pool_logger = engine.sync_engine.pool.logger
assert pool_logger.name == DB_POOL_LOGGER
assert not pool_logger.isEnabledFor(logging.INFO)
assert pool_logger.isEnabledFor(logging.WARNING)
finally:
await engine.dispose()
async def test_pool_class_survives_dispose() -> None:
"""`dispose()` создаёт новый пул того же класса: метрики читают его."""
engine = make_engine(pool_size=1)
try:
before = engine.sync_engine.pool
async with engine.connect() as connection:
await connection.execute(text("SELECT 1"))
await engine.dispose()
after = engine.sync_engine.pool
assert after is not before
assert isinstance(after, CountingQueuePool)
assert after.size() == 1
assert (after.pending(), after.timeouts()) == (0, 0)
finally:
await engine.dispose()
+4
View File
@@ -473,6 +473,8 @@ async def _statements(engine, action) -> list[str]:
("count_published", "'queued'"),
("count_unfinished", "'processing'"),
("count_unfinished", "'queued'"),
("queue_snapshot", "'processing'"),
("queue_snapshot", "'queued'"),
("lock_stale_pending", "'pending'"),
("get_staging_candidates", "'ready'"),
],
@@ -491,6 +493,8 @@ async def test_status_is_a_constant_in_statement(
await uow.jobs.count_published()
elif method == "count_unfinished":
await uow.jobs.count_unfinished(limit=1)
elif method == "queue_snapshot":
await uow.jobs.queue_snapshot(now=NOW)
elif method == "lock_stale_pending":
await uow.attachments.lock_stale_pending(
attachment_id=user_dto().id,