feat: add OpenTelemetry SDK, collector and telemetry stores
Collector (contrib), Prometheus, Loki, Tempo and Grafana with provisioning from git; SDK in API and worker with level 3 metrics, traces and logs carrying trace_id/span_id; telemetry off by default outside the stand; broker healthcheck runs as the broker user so a fresh stand starts once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,7 @@ from src.schemas.enums import (
|
||||
AttachmentJobState,
|
||||
AttachmentStatus,
|
||||
AttachmentVariantName,
|
||||
JobDeliveryOutcome,
|
||||
JobOutcome,
|
||||
)
|
||||
from src.schemas.jobs import build_job_body
|
||||
@@ -34,6 +35,8 @@ from src.utils.utc import utcnow
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
Outcome = JobDeliveryOutcome
|
||||
|
||||
SOURCE = b"source-bytes"
|
||||
|
||||
|
||||
@@ -102,10 +105,27 @@ def _body(job: dataclasses.AttachmentJobDTO) -> bytes:
|
||||
async def _handle(
|
||||
service: AttachmentProcessingService,
|
||||
job: dataclasses.AttachmentJobDTO,
|
||||
) -> JobOutcome:
|
||||
) -> JobDeliveryOutcome:
|
||||
return await service.handle(body=_body(job), redelivered=False)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("outcome", "action"),
|
||||
[
|
||||
(Outcome.READY, JobOutcome.ACK),
|
||||
(Outcome.FAILED, JobOutcome.ACK),
|
||||
(Outcome.RETRY, JobOutcome.ACK),
|
||||
(Outcome.SKIPPED, JobOutcome.ACK),
|
||||
(Outcome.REQUEUED, JobOutcome.REQUEUE),
|
||||
(Outcome.DROPPED, JobOutcome.DROP),
|
||||
(Outcome.ERROR, JobOutcome.REQUEUE),
|
||||
],
|
||||
)
|
||||
def test_outcome_action(outcome: Outcome, action: JobOutcome) -> None:
|
||||
"""Исход доставки → действие с сообщением: прежние ack, requeue, drop."""
|
||||
assert outcome.action == action
|
||||
|
||||
|
||||
def _set_job(database: FakeDatabase, job_id: UUID, **fields) -> None:
|
||||
database.jobs[job_id] = replace(database.jobs[job_id], **fields)
|
||||
|
||||
@@ -117,7 +137,7 @@ async def test_success_commits_ready_with_variants(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
) -> None:
|
||||
outcome = await _handle(service, queued)
|
||||
assert outcome == JobOutcome.ACK
|
||||
assert outcome == Outcome.READY
|
||||
row = database.attachments[queued.attachment_id]
|
||||
job = database.jobs[queued.id]
|
||||
assert row.status == AttachmentStatus.READY
|
||||
@@ -167,7 +187,7 @@ async def test_malformed_message_dropped(
|
||||
database: FakeDatabase,
|
||||
) -> None:
|
||||
outcome = await service.handle(body=b"not json", redelivered=False)
|
||||
assert outcome == JobOutcome.DROP
|
||||
assert outcome == Outcome.DROPPED
|
||||
assert database.events == []
|
||||
|
||||
|
||||
@@ -177,7 +197,7 @@ async def test_unknown_job_acked_without_attempt(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
) -> None:
|
||||
other = replace(queued, id=uuid4())
|
||||
assert await _handle(service, other) == JobOutcome.ACK
|
||||
assert await _handle(service, other) == Outcome.SKIPPED
|
||||
assert database.jobs[queued.id].attempt == 0
|
||||
|
||||
|
||||
@@ -187,7 +207,7 @@ async def test_foreign_attachment_acked_without_attempt(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
) -> None:
|
||||
other = replace(queued, attachment_id=uuid4())
|
||||
assert await _handle(service, other) == JobOutcome.ACK
|
||||
assert await _handle(service, other) == Outcome.SKIPPED
|
||||
assert database.jobs[queued.id].attempt == 0
|
||||
|
||||
|
||||
@@ -199,12 +219,12 @@ async def test_finished_jobs_acked_without_write(
|
||||
await _handle(service, queued)
|
||||
database.events.clear()
|
||||
# повторная доставка завершённого
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.SKIPPED
|
||||
assert database.jobs[queued.id].attempt == 1
|
||||
assert "commit" not in database.events
|
||||
_set_job(database, queued.id, state=AttachmentJobState.FAILED)
|
||||
database.events.clear()
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.SKIPPED
|
||||
assert "commit" not in database.events
|
||||
|
||||
|
||||
@@ -218,7 +238,7 @@ async def test_not_due_acked_without_attempt(
|
||||
queued.id,
|
||||
next_attempt_at=utcnow() + timedelta(seconds=30),
|
||||
)
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.SKIPPED
|
||||
assert database.jobs[queued.id].attempt == 0
|
||||
|
||||
|
||||
@@ -250,7 +270,7 @@ async def test_live_lease_acked_without_attempt(
|
||||
attempt=1,
|
||||
lease_expires_at=utcnow() + timedelta(seconds=30),
|
||||
)
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.SKIPPED
|
||||
assert database.jobs[queued.id].attempt == 1
|
||||
assert "renderer.render" not in database.events
|
||||
|
||||
@@ -280,7 +300,7 @@ async def test_budget_spent_fails_without_attempt(
|
||||
) -> None:
|
||||
attempts = settings.worker.max_attempts
|
||||
_set_job(database, queued.id, attempt=attempts)
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.FAILED
|
||||
job = database.jobs[queued.id]
|
||||
row = database.attachments[queued.attachment_id]
|
||||
assert job.state == AttachmentJobState.FAILED
|
||||
@@ -333,7 +353,7 @@ async def test_permanent_error_fails_at_once(
|
||||
database.fail_on["renderer.render"] = exceptions.ImageRejected(
|
||||
reason=AttachmentJobReason.UNDECODABLE,
|
||||
)
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.FAILED
|
||||
row = database.attachments[queued.attachment_id]
|
||||
job = database.jobs[queued.id]
|
||||
assert row.status == AttachmentStatus.FAILED
|
||||
@@ -365,7 +385,7 @@ async def test_damaged_source_fails_at_once(
|
||||
storage.seed(key, b"other size bytes!")
|
||||
else:
|
||||
storage.seed(key, b"X" * len(SOURCE))
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.FAILED
|
||||
row = database.attachments[queued.attachment_id]
|
||||
assert row.status == AttachmentStatus.FAILED
|
||||
assert row.error_code == AttachmentErrorCode.PROCESSING_FAILED
|
||||
@@ -410,7 +430,7 @@ async def test_temporary_error_returns_to_queue(
|
||||
) -> None:
|
||||
database.fail_on[event] = failure
|
||||
before = utcnow()
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.RETRY
|
||||
job = database.jobs[queued.id]
|
||||
row = database.attachments[queued.attachment_id]
|
||||
assert job.state == AttachmentJobState.QUEUED
|
||||
@@ -428,7 +448,7 @@ async def test_temporary_error_after_budget_is_failed(
|
||||
) -> None:
|
||||
_set_job(database, queued.id, attempt=settings.worker.max_attempts - 1)
|
||||
database.fail_on["storage.get"] = exceptions.ObjectStorageUnavailable()
|
||||
assert await _handle(service, queued) == JobOutcome.ACK
|
||||
assert await _handle(service, queued) == Outcome.FAILED
|
||||
row = database.attachments[queued.attachment_id]
|
||||
assert row.error_code == AttachmentErrorCode.RETRY_EXHAUSTED
|
||||
assert database.jobs[queued.id].state == AttachmentJobState.FAILED
|
||||
@@ -441,7 +461,7 @@ async def _with_supersede(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
*,
|
||||
failure: BaseException | None = None,
|
||||
) -> tuple[JobOutcome, dataclasses.AttachmentJobDTO]:
|
||||
) -> tuple[JobDeliveryOutcome, dataclasses.AttachmentJobDTO]:
|
||||
"""Пока попытка работает, задание взяла следующая попытка."""
|
||||
|
||||
async def render(data: bytes) -> dataclasses.RenderedVariantsDTO:
|
||||
@@ -463,7 +483,7 @@ async def test_superseded_on_ready_no_effect(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
) -> None:
|
||||
outcome, job = await _with_supersede(service, database, renderer, queued)
|
||||
assert outcome == JobOutcome.ACK
|
||||
assert outcome == Outcome.SKIPPED
|
||||
assert job.state == AttachmentJobState.PROCESSING
|
||||
assert database.attachments[queued.attachment_id].status == (
|
||||
AttachmentStatus.PROCESSING
|
||||
@@ -486,7 +506,7 @@ async def test_superseded_on_failed_no_effect(
|
||||
reason=AttachmentJobReason.UNDECODABLE,
|
||||
),
|
||||
)
|
||||
assert outcome == JobOutcome.ACK
|
||||
assert outcome == Outcome.SKIPPED
|
||||
assert job.state == AttachmentJobState.PROCESSING
|
||||
assert database.attachments[queued.attachment_id].error_code is None
|
||||
|
||||
@@ -506,7 +526,7 @@ async def test_superseded_on_return_no_effect(
|
||||
reason=AttachmentJobReason.RENDER_KILLED,
|
||||
),
|
||||
)
|
||||
assert outcome == JobOutcome.ACK
|
||||
assert outcome == Outcome.SKIPPED
|
||||
assert job.state == AttachmentJobState.PROCESSING
|
||||
assert database.attachments[queued.attachment_id].status == (
|
||||
AttachmentStatus.PROCESSING
|
||||
@@ -519,7 +539,7 @@ async def test_database_failure_requeues_and_pauses(
|
||||
queued: dataclasses.AttachmentJobDTO,
|
||||
) -> None:
|
||||
database.fail_on["jobs.lock_job"] = exceptions.ServiceUnavailable()
|
||||
assert await _handle(service, queued) == JobOutcome.REQUEUE
|
||||
assert await _handle(service, queued) == Outcome.REQUEUED
|
||||
assert database.jobs[queued.id].attempt == 0
|
||||
|
||||
|
||||
@@ -537,7 +557,7 @@ async def test_stop_interrupts_pause(
|
||||
service = AttachmentProcessingService(uow, storage, renderer, stop)
|
||||
database.fail_on["jobs.lock_job"] = exceptions.ServiceUnavailable()
|
||||
async with asyncio.timeout(1):
|
||||
assert await _handle(service, queued) == JobOutcome.REQUEUE
|
||||
assert await _handle(service, queued) == Outcome.REQUEUED
|
||||
|
||||
|
||||
def test_retry_delay_bounds() -> None:
|
||||
|
||||
Reference in New Issue
Block a user