forked from mtusi2026/mtusi2026lab6
Count and trace attachment jobs through a job observer port
Add the JobObserver port to the attachments zone and its adapter on the OpenTelemetry API: every delivery adds one to messenger.jobs.processed and records messenger.job.duration in seconds with buckets from 50 ms to 60 s, both labelled only by job.type and outcome, and puts the job id, the attachment id, the attempt and the outcome on the current consumer span, which turns to ERROR on retry_scheduled, failed and error. The worker entrypoint measures each delivery, reports the outcome of the handler or error when it raised, and writes a job.processed record. Without the SDK the adapter runs on the no-op providers of the API. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e60b4eac98
commit
e2ec3fe9af
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from dishka import FromDishka
|
||||
from faststream import AckPolicy
|
||||
@@ -8,6 +9,8 @@ from pydantic import ValidationError
|
||||
|
||||
from messenger.attachments.application.bus import AttachmentsCommandBus
|
||||
from messenger.attachments.application.commands import ProcessJob
|
||||
from messenger.attachments.application.ports import JobObserver
|
||||
from messenger.attachments.domain.jobs import Job
|
||||
from messenger.attachments.entrypoints.schemas import JobMessage
|
||||
from messenger.attachments.infrastructure.rabbitmq import QUEUE
|
||||
from messenger.shared.application.ports import Logger
|
||||
@@ -24,6 +27,7 @@ async def handle_job(
|
||||
message: RabbitMessage,
|
||||
bus: FromDishka[AttachmentsCommandBus],
|
||||
logger: FromDishka[Logger],
|
||||
observer: FromDishka[JobObserver],
|
||||
) -> None:
|
||||
try:
|
||||
job = JobMessage.model_validate_json(message.body).job()
|
||||
@@ -31,16 +35,32 @@ async def handle_job(
|
||||
logger.warning("job.rejected", reason="message does not match jobs.schema.json")
|
||||
await message.reject(requeue=False)
|
||||
return
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
await bus.execute(ProcessJob(job))
|
||||
outcome = (await bus.execute(ProcessJob(job))).value
|
||||
except Exception as error:
|
||||
observed(job, "error", started, observer, logger)
|
||||
logger.warning("job.requeued", job_id=str(job.id), error=type(error).__name__)
|
||||
await asyncio.sleep(REQUEUE_DELAY_SECONDS)
|
||||
await message.nack(requeue=True)
|
||||
return
|
||||
observed(job, outcome, started, observer, logger)
|
||||
await message.ack()
|
||||
|
||||
|
||||
def observed(job: Job, outcome: str, started: float, observer: JobObserver, logger: Logger) -> None:
|
||||
seconds = time.perf_counter() - started
|
||||
observer.observe(job, outcome, seconds)
|
||||
logger.info(
|
||||
"job.processed",
|
||||
job_id=str(job.id),
|
||||
attachment_id=str(job.attachment_id),
|
||||
attempt=job.attempt,
|
||||
outcome=outcome,
|
||||
duration_ms=round(seconds * 1000, 3),
|
||||
)
|
||||
|
||||
|
||||
def subscribe(broker: RabbitBroker, concurrency: int) -> None:
|
||||
broker.subscriber(
|
||||
JOBS,
|
||||
|
||||
Reference in New Issue
Block a user