From b8ccacb614bb6fe660d3be517c85ebdff940951c Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 9 Mar 2025 18:47:59 +0100 Subject: [PATCH] fix bug in initial db creation caused by prometheus lib rtying to access tables at load time --- core/core/metrics.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/core/core/metrics.py b/core/core/metrics.py index 149829c..d973b0d 100644 --- a/core/core/metrics.py +++ b/core/core/metrics.py @@ -3,18 +3,20 @@ from prometheus_client.core import CounterMetricFamily, REGISTRY from django.db.models import Case, Value, When, BooleanField, Count from inventory.models import Item + class ItemCountCollector(object): def collect(self): - counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state']) + try: + counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state']) - yield counter + yield counter - if not apps.models_ready or not apps.apps_ready: - return + if not apps.models_ready or not apps.apps_ready: + return - queryset = ( - Item.all_objects + queryset = ( + Item.all_objects .annotate( returned=Case( When(returned_at__isnull=True, then=Value(False)), @@ -25,11 +27,14 @@ class ItemCountCollector(object): .values('event__slug', 'returned', 'event_id') .annotate(amount=Count('id')) .order_by('event__slug', 'returned') # Optional: order by slug and returned - ) + ) - for e in queryset: - counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"]) + for e in queryset: + counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"]) - yield counter + yield counter + except: + pass -REGISTRY.register(ItemCountCollector()) \ No newline at end of file + +REGISTRY.register(ItemCountCollector())