Реализовать observability для лабораторной 6

This commit is contained in:
2026-11-20 17:55:23 +03:00
parent 58f3d4c457
commit 9e6428de05
50 changed files with 3573 additions and 98 deletions
+4
View File
@@ -89,6 +89,8 @@ class AttachmentRepository:
height: int,
max_attempts: int,
now: datetime,
traceparent: str | None = None,
tracestate: str | None = None,
) -> Attachment:
row = AttachmentRow(
id=attachment_id,
@@ -128,6 +130,8 @@ class AttachmentRepository:
event_type='attachment.process.v1',
job_id=job_id,
payload=job_payload(job_id, attachment_id, 1, 1, now),
traceparent=traceparent,
tracestate=tracestate,
available_at=now,
published_at=None,
claim_owner=None,
+24 -2
View File
@@ -3,13 +3,14 @@ from __future__ import annotations
from datetime import datetime, timedelta
from uuid import UUID, uuid4
from sqlalchemy import or_, select, update
from sqlalchemy import func, or_, select, update
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from ..domain.models import Attachment, AttachmentJob, AttachmentVariant, OutboxEvent
from ..models.attachment import AttachmentRow, AttachmentVariantRow
from ..models.job import AttachmentJobRow, JobEventRow, OutboxEventRow
from ..observability.propagation import inject_trace_context
from .attachments import AttachmentRepository, job_payload
@@ -220,6 +221,7 @@ class JobRepository:
job.next_attempt_at = available_at
attachment.status = 'queued'
attachment.error_code = None
trace_context = inject_trace_context()
session.add(
OutboxEventRow(
id=uuid4(),
@@ -232,6 +234,8 @@ class JobRepository:
job.attempt,
now,
),
traceparent=trace_context.get('traceparent'),
tracestate=trace_context.get('tracestate'),
available_at=available_at,
published_at=None,
claim_owner=None,
@@ -260,6 +264,17 @@ class JobRepository:
await session.flush()
return job.state
async def queue_stats(self, now: datetime) -> tuple[int, float]:
async with self._sessions() as session:
depth, oldest = (
await session.execute(
select(func.count(AttachmentJobRow.id), func.min(AttachmentJobRow.created_at))
.where(AttachmentJobRow.state.in_(('queued', 'processing')))
)
).one()
age = max(0.0, (now - oldest).total_seconds()) if oldest is not None else 0.0
return int(depth or 0), age
async def recover_expired(self, now: datetime) -> int:
async with self._sessions() as session:
ids = tuple(
@@ -306,6 +321,7 @@ class JobRepository:
OutboxEventRow(
id=uuid4(), event_type='attachment.process.v1', job_id=job.id,
payload=job_payload(job.id, job.attachment_id, job.pipeline_version, job.attempt, now),
traceparent=None, tracestate=None,
available_at=now, published_at=None, claim_owner=None, claim_until=None,
publish_attempts=0, last_error_code=None, created_at=now, updated_at=now,
),
@@ -341,7 +357,13 @@ class OutboxRepository:
row.claim_until = until
row.publish_attempts += 1
row.updated_at = now
return tuple(OutboxEvent(row.id, row.job_id, row.payload, row.available_at) for row in rows)
return tuple(
OutboxEvent(
row.id, row.job_id, row.payload, row.available_at,
row.traceparent, row.tracestate,
)
for row in rows
)
async def published(self, event_id: UUID, owner: str, now: datetime) -> None:
async with self._sessions() as session: