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
@@ -0,0 +1,40 @@
|
||||
from opentelemetry import metrics, trace
|
||||
from opentelemetry.metrics import MeterProvider
|
||||
from opentelemetry.trace import StatusCode
|
||||
|
||||
from messenger.attachments.domain.jobs import JOB_TYPE, Job
|
||||
|
||||
DURATION_BUCKETS = (0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10.0, 30.0, 60.0)
|
||||
FAILURES = frozenset({"retry_scheduled", "failed", "error"})
|
||||
|
||||
|
||||
class OtelJobObserver:
|
||||
__slots__ = ("_duration", "_processed")
|
||||
|
||||
def __init__(self, provider: MeterProvider | None = None) -> None:
|
||||
meter = metrics.get_meter("messenger.attachments", meter_provider=provider)
|
||||
self._processed = meter.create_counter(
|
||||
"messenger.jobs.processed", unit="{job}", description="Доставки заданий по исходу"
|
||||
)
|
||||
self._duration = meter.create_histogram(
|
||||
"messenger.job.duration",
|
||||
unit="s",
|
||||
description="Длительность обработки доставки задания",
|
||||
explicit_bucket_boundaries_advisory=DURATION_BUCKETS,
|
||||
)
|
||||
|
||||
def observe(self, job: Job, outcome: str, seconds: float) -> None:
|
||||
labels = {"job.type": JOB_TYPE, "outcome": outcome}
|
||||
self._processed.add(1, labels)
|
||||
self._duration.record(seconds, labels)
|
||||
span = trace.get_current_span()
|
||||
span.set_attributes(
|
||||
{
|
||||
"messenger.job_id": str(job.id),
|
||||
"messenger.attachment_id": str(job.attachment_id),
|
||||
"messenger.job.attempt": job.attempt,
|
||||
"messenger.job.outcome": outcome,
|
||||
}
|
||||
)
|
||||
if outcome in FAILURES:
|
||||
span.set_status(StatusCode.ERROR, outcome)
|
||||
Reference in New Issue
Block a user