From e921cd2127b1747892b59a56800b5e466cc6dff5 Mon Sep 17 00:00:00 2001 From: Prachi Zala Date: Sat, 21 Nov 2026 10:09:32 +0530 Subject: [PATCH] feat(metrics): add Prometheus counter for DLQ message evictions Tracks dead-letter-queue discard and eviction rates for unprocessable task payloads. Co-authored-by: Utkarsh_barad <183999492+utkarshbhai007@users.noreply.github.com> --- app/metrics/dlq_counter.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/metrics/dlq_counter.py diff --git a/app/metrics/dlq_counter.py b/app/metrics/dlq_counter.py new file mode 100644 index 0000000..8a7fddf --- /dev/null +++ b/app/metrics/dlq_counter.py @@ -0,0 +1,28 @@ +""" +Dead-Letter Queue (DLQ) Eviction Metric Counter +Maintains cumulative metric counters for failed or expired background task payloads. +""" +from typing import Dict, Any + +class DLQEvictionCounter: + def __init__(self): + self._counts: Dict[str, int] = {} + + def record_eviction(self, reason: str) -> None: + self._counts[reason] = self._counts.get(reason, 0) + 1 + + def get_metrics(self) -> Dict[str, Any]: + return { + "total_evictions": sum(self._counts.values()), + "by_reason": dict(self._counts) + } + +if __name__ == "__main__": + c = DLQEvictionCounter() + c.record_eviction("TTL_EXPIRED") + c.record_eviction("TTL_EXPIRED") + c.record_eviction("MAX_RETRIES_EXCEEDED") + m = c.get_metrics() + assert m["total_evictions"] == 3 + assert m["by_reason"]["TTL_EXPIRED"] == 2 + print("DLQ eviction counter unit tests passed!")