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:
@@ -938,6 +938,109 @@ async def check_count_unfinished(new_uow: NewUow) -> None:
|
||||
assert await count(1) == 3
|
||||
|
||||
|
||||
TRACEPARENT = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
|
||||
|
||||
|
||||
async def check_job_trace_context(new_uow: NewUow) -> None:
|
||||
"""Контекст задания сохраняется вставкой и читается выборками, в том
|
||||
числе relay (design change `job-trace-propagation`, решение 4)."""
|
||||
world = await _world(new_uow)
|
||||
attachment = await _attachment(
|
||||
new_uow,
|
||||
world,
|
||||
status=AttachmentStatus.QUEUED,
|
||||
created_at=at(-10),
|
||||
)
|
||||
traced = replace(
|
||||
job_dto(attachment_id=attachment.id, created_at=at(-10)),
|
||||
traceparent=TRACEPARENT,
|
||||
tracestate="vendor=value",
|
||||
)
|
||||
plain = await _job(new_uow, world, created_at=at(-5))
|
||||
async with new_uow() as uow:
|
||||
await uow.jobs.insert_job(job=traced)
|
||||
await uow.commit()
|
||||
row = await _job_row(new_uow, traced.id)
|
||||
assert row is not None
|
||||
assert row.traceparent == TRACEPARENT
|
||||
assert row.tracestate == "vendor=value"
|
||||
without = await _job_row(new_uow, plain.id)
|
||||
assert without is not None
|
||||
assert without.traceparent is None
|
||||
assert without.tracestate is None
|
||||
async with new_uow() as uow:
|
||||
due = await uow.jobs.lock_due_jobs(now=NOW, limit=10)
|
||||
by_id = {job.id: job for job in due}
|
||||
assert by_id[traced.id].traceparent == TRACEPARENT
|
||||
assert by_id[traced.id].tracestate == "vendor=value"
|
||||
assert by_id[plain.id].traceparent is None
|
||||
|
||||
|
||||
async def check_queue_snapshot(new_uow: NewUow) -> None:
|
||||
world = await _world(new_uow)
|
||||
|
||||
async def snapshot(now: datetime = NOW) -> dataclasses.QueueSnapshotDTO:
|
||||
async with new_uow() as uow:
|
||||
return await uow.jobs.queue_snapshot(now=now)
|
||||
|
||||
empty = await snapshot()
|
||||
assert empty == dataclasses.QueueSnapshotDTO(
|
||||
taken_at=NOW,
|
||||
scheduled=0,
|
||||
outbox=0,
|
||||
published=0,
|
||||
processing=0,
|
||||
oldest_created_at=None,
|
||||
)
|
||||
# завершённые не считаются
|
||||
await _failed_job(new_uow, world)
|
||||
# срок наступил, не опубликовано
|
||||
outbox = await _job(new_uow, world, created_at=at(-30))
|
||||
# ещё не наступил срок приёма — тоже ждёт срока
|
||||
await _job(new_uow, world, created_at=at(20))
|
||||
published = await _job(new_uow, world, created_at=at(-20))
|
||||
retrying = await _started_job(new_uow, world, lease_expires_at=at(100))
|
||||
processing = await _started_job(new_uow, world, lease_expires_at=at(100))
|
||||
async with new_uow() as uow:
|
||||
await uow.jobs.mark_published(job_ids=[published.id], now=at(-1))
|
||||
assert await uow.jobs.return_to_queued(
|
||||
job_id=retrying.id,
|
||||
attempt=retrying.attempt,
|
||||
reason=AttachmentJobReason.STORAGE_UNAVAILABLE,
|
||||
next_attempt_at=at(60),
|
||||
now=at(-1),
|
||||
)
|
||||
await uow.commit()
|
||||
result = await snapshot()
|
||||
assert (
|
||||
result.scheduled,
|
||||
result.outbox,
|
||||
result.published,
|
||||
result.processing,
|
||||
) == (2, 1, 1, 1)
|
||||
assert result.taken_at == NOW
|
||||
assert result.oldest_created_at == outbox.created_at
|
||||
# срок следующей попытки наступил — стадия outbox
|
||||
later = await snapshot(at(61))
|
||||
assert (later.scheduled, later.outbox) == (0, 3)
|
||||
# старейший — среди всех незавершённых: в работе раньше других
|
||||
async with new_uow() as uow:
|
||||
await uow.jobs.finish_job(
|
||||
job_id=outbox.id,
|
||||
state=AttachmentJobState.SUCCEEDED,
|
||||
reason=None,
|
||||
now=at(1),
|
||||
)
|
||||
await uow.commit()
|
||||
rest = await snapshot()
|
||||
assert rest.oldest_created_at == min(
|
||||
published.created_at,
|
||||
processing.created_at,
|
||||
retrying.created_at,
|
||||
)
|
||||
assert rest.outbox == 0
|
||||
|
||||
|
||||
async def check_recover_expired_leases(new_uow: NewUow) -> None:
|
||||
world = await _world(new_uow)
|
||||
expired = await _started_job(new_uow, world, lease_expires_at=at(-5))
|
||||
|
||||
Reference in New Issue
Block a user