Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
class Metrics:
|
|
"""Counters plus a queue depth gauge that is kept by hand and reconciled on flush."""
|
|
|
|
def __init__(self, log):
|
|
self.log = log
|
|
self.counters = {}
|
|
self.depth = 0
|
|
|
|
def inc(self, name, n=1):
|
|
self.counters[name] = self.counters.get(name, 0) + n
|
|
|
|
def get(self, name):
|
|
return self.counters.get(name, 0)
|
|
|
|
def flush(self, actual_depth, leased):
|
|
if self.depth != actual_depth:
|
|
self.log.warn(
|
|
"metrics",
|
|
"queue_depth counter drifted (counter=%d actual=%d), reconciling",
|
|
self.depth,
|
|
actual_depth,
|
|
)
|
|
self.depth = actual_depth
|
|
self.log.info(
|
|
"metrics",
|
|
"queue_depth=%d leased=%d submitted=%d done=%d dead=%d retries=%d dedupe_hits=%d",
|
|
actual_depth,
|
|
leased,
|
|
self.get("submitted"),
|
|
self.get("done"),
|
|
self.get("dead"),
|
|
self.get("retries"),
|
|
self.get("dedupe_hits"),
|
|
)
|