Add RedisQueueCollector for gathering information about Celery queues

Trying to extract this information from Celery itself is painful and
Celery itself recommends gathering this information from the broker (in
srht case Redis) directly.
This commit is contained in:
Ignas Kiela 2021-12-08 10:18:06 +02:00 committed by Drew DeVault
parent df82eaf726
commit 5484f11f39
1 changed files with 32 additions and 0 deletions

32
srht/metrics.py Normal file
View File

@ -0,0 +1,32 @@
import time
from prometheus_client.metrics_core import GaugeMetricFamily
from redis import Redis, ResponseError
class RedisQueueCollector:
def __init__(self, broker, name, documentation, queue_name="celery"):
self.redis = Redis.from_url(broker)
self.queue_name = queue_name
self.name = name
self.documentation = documentation
def collect(self):
start = time.time()
errors = 0
try:
queue_size = self.redis.llen(self.queue_name)
except ResponseError: # Key is not a list
queue_size = 0
errors += 1
duration = time.time() - start
yield GaugeMetricFamily(self.name + "_queue_length", self.documentation, value=queue_size)
yield GaugeMetricFamily(
self.name + "_queue_length_collection_time_seconds",
"Time to collect queue metrics",
value=duration,
)
yield GaugeMetricFamily(
self.name + "_queue_length_collection_error_count",
"Errors encountered while collecting queue metrics",
value=errors,
)