Реализовать уровень 4: сквозные трассы, SLO, отказы и отчёт
This commit is contained in:
+12
-3
@@ -22,6 +22,8 @@ from app.storage import ObjectMissing, storage
|
||||
from app.telemetry import (configure, emit, install_termination_handler, jobs_duration,
|
||||
jobs_processed, shutdown, span)
|
||||
from opentelemetry.trace import SpanKind, Status, StatusCode
|
||||
from app import advanced
|
||||
from app.trace_context import carrier, parent_context
|
||||
|
||||
logger = logging.getLogger("messenger.worker")
|
||||
|
||||
@@ -46,9 +48,11 @@ async def claim(job: dict) -> dict | None:
|
||||
result = await db.execute(text("""
|
||||
UPDATE attachments SET status='processing',attempts_run=attempts_run+1,
|
||||
lease_token=:token,lease_until=now()+make_interval(secs => :lease),
|
||||
traceparent=:traceparent,tracestate=:tracestate,
|
||||
updated_at=now()
|
||||
WHERE id=:id AND next_attempt_at<=now() RETURNING *
|
||||
"""), {"id": row["id"], "token": uuid4(), "lease": LEASE_SECONDS})
|
||||
"""), {"id": row["id"], "token": uuid4(), "lease": LEASE_SECONDS,
|
||||
"traceparent": carrier().get("traceparent"), "tracestate": carrier().get("tracestate")})
|
||||
claimed = result.mappings().first()
|
||||
if claimed is None:
|
||||
await db.rollback()
|
||||
@@ -107,7 +111,8 @@ async def build_objects(row: dict) -> list[dict]:
|
||||
await chunks.aclose()
|
||||
if size != declared_length or size != row["size_bytes"] or digest.hexdigest() != row["sha256"]:
|
||||
raise PermanentFailure("source_checksum_mismatch")
|
||||
variants = await generate(source, directory)
|
||||
with span("image.generate"):
|
||||
variants = await generate(source, directory)
|
||||
# API never publishes directly to this final prefix. Repeating one
|
||||
# v1 job writes the same bytes to the same four keys, never appends.
|
||||
with source.open("rb") as body:
|
||||
@@ -146,6 +151,8 @@ async def finish(row: dict, *, variants=None, code: str | None = None,
|
||||
"""), {"id": row["id"], "variants": json.dumps(variants)})
|
||||
await event(db, current, "ready")
|
||||
await db.commit()
|
||||
if code is None:
|
||||
advanced.observe_ready(current)
|
||||
return True
|
||||
|
||||
|
||||
@@ -195,13 +202,15 @@ async def handle(message) -> None:
|
||||
pass
|
||||
started = time.perf_counter()
|
||||
outcome = "error"
|
||||
with span("attachments process", kind=SpanKind.CONSUMER, attributes={
|
||||
with span("attachments process", kind=SpanKind.CONSUMER, parent=parent_context(job), attributes={
|
||||
"messaging.system": "rabbitmq", "messaging.destination.name": "attachments",
|
||||
"messaging.operation": "process", "job_type": "attachment.process.v1",
|
||||
}) as active:
|
||||
try:
|
||||
advanced.busy = 1
|
||||
outcome = await _handle(message)
|
||||
finally:
|
||||
advanced.busy = 0
|
||||
duration = time.perf_counter() - started
|
||||
attrs = {"job_type": "attachment.process.v1", "outcome": outcome}
|
||||
jobs_processed.add(1, attrs)
|
||||
|
||||
Reference in New Issue
Block a user