From 87255cafd868741acb1e82de8dde76274a8e441b Mon Sep 17 00:00:00 2001 From: Tahmid Ahmed Date: Wed, 14 Oct 2026 01:45:50 -0400 Subject: [PATCH] DINO-1093: fix counters in LASS to initialize all counters to 0 (must follow initialization rules, and unlike java, this is not automatic) --- .../leetcode-auth-stealer-service/src/prom.py | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/internal/leetcode-auth-stealer-service/src/prom.py b/internal/leetcode-auth-stealer-service/src/prom.py index eafed3cb4..0e79d7408 100644 --- a/internal/leetcode-auth-stealer-service/src/prom.py +++ b/internal/leetcode-auth-stealer-service/src/prom.py @@ -1,9 +1,34 @@ """Prometheus collectors, typed metric updates, and exposition.""" -from typing import Literal +from collections.abc import Iterable, Mapping, Sequence +from typing import Literal, get_args from prometheus_client import CONTENT_TYPE_LATEST, Counter, generate_latest + +def _counter( + name: str, + documentation: str, + labelnames: Sequence[str] = (), + initial_labels: Iterable[Mapping[str, str]] = (), +) -> Counter: + """ + create a counter whose label combinations are exported at 0 before first use. + + https://prometheus.io/docs/practices/the_zen/#avoid-missing-metrics + """ + initial = list(initial_labels) + if labelnames and not initial: + raise ValueError(f"{name}: labeled counters must declare initial_labels") + + counter = Counter(name, documentation, labelnames) + + for labels in initial: + counter.labels(**labels) + + return counter + + type AuthFailureReason = Literal[ "invalid_argument", "bad_credentials", @@ -15,10 +40,15 @@ "incomplete_authentication", ] -_AUTH_CALLS = Counter( +_AUTH_CALLS = _counter( "leetcode_auth_calls_total", "Completed authentication calls by outcome and failure reason.", ("outcome", "reason"), + [{"outcome": "success", "reason": ""}] + + [ + {"outcome": "failure", "reason": reason} + for reason in get_args(AuthFailureReason.__value__) + ], )