metrics: Make metrics actually usable
All checks were successful
/ test (push) Successful in 2m52s
/ test (pull_request) Successful in 2m38s

This commit is contained in:
lagertonne 2024-12-21 22:14:53 +01:00
parent 933ee617ff
commit 2e4629d45e
8 changed files with 14 additions and 50 deletions

View file

@ -1,8 +0,0 @@
from django.core.management.base import BaseCommand, CommandError
from core.metrics import update_item_count
class Command(BaseCommand):
help = "Refresh the metrics"
def handle(self, *args, **options):
update_item_count()

View file

@ -1,11 +1,16 @@
from prometheus_client import Gauge
from django_prometheus import exports
from inventory.models import *
from django.apps import apps
from prometheus_client.core import CounterMetricFamily, REGISTRY
g_items_total = Gauge('c3lf_items_total', 'Current Total items')
class ModelCountCollector(object):
def update_item_count():
# Get the count of MyModel objects
count = Item.objects.count()
# Set the gauge to the current count
g_items_total.set(count)
def collect(self):
counter = CounterMetricFamily('model_count', 'Number of objects per model', labels=['model', 'app'])
yield counter
if not apps.models_ready or not apps.apps_ready:
return
for app_name, app_config in apps.app_configs.items():
for model in app_config.get_models():
counter.add_metric([model.__name__, app_name], model.objects.count())
yield counter
REGISTRY.register(ModelCountCollector())

View file

@ -70,7 +70,6 @@ INSTALLED_APPS = [
'inventory',
'mail',
'notify_sessions',
'core'
]
REST_FRAMEWORK = {