From 933ee617ff5a93e9cc7cc71cec3d79c78839f2e9 Mon Sep 17 00:00:00 2001 From: lagertonne Date: Sat, 21 Dec 2024 12:51:38 +0100 Subject: [PATCH 1/2] metrics: Add the first (simple) custom metrics --- core/core/management/__init__.py | 0 core/core/management/commands/__init__.py | 0 core/core/management/commands/update_metrics.py | 8 ++++++++ core/core/metrics.py | 11 +++++++++++ core/core/settings.py | 1 + core/core/urls.py | 2 ++ deploy/ansible/playbooks/deploy-c3lf-sys3.yml | 16 ++++++++++++++++ .../templates/c3lf-sys3-cron.service.j2 | 13 +++++++++++++ .../playbooks/templates/c3lf-sys3-cron.timer.j2 | 9 +++++++++ 9 files changed, 60 insertions(+) create mode 100644 core/core/management/__init__.py create mode 100644 core/core/management/commands/__init__.py create mode 100644 core/core/management/commands/update_metrics.py create mode 100644 core/core/metrics.py create mode 100644 deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 create mode 100644 deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 diff --git a/core/core/management/__init__.py b/core/core/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/core/management/commands/__init__.py b/core/core/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/core/management/commands/update_metrics.py b/core/core/management/commands/update_metrics.py new file mode 100644 index 0000000..a51cfea --- /dev/null +++ b/core/core/management/commands/update_metrics.py @@ -0,0 +1,8 @@ +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() \ No newline at end of file diff --git a/core/core/metrics.py b/core/core/metrics.py new file mode 100644 index 0000000..4f79754 --- /dev/null +++ b/core/core/metrics.py @@ -0,0 +1,11 @@ +from prometheus_client import Gauge +from django_prometheus import exports +from inventory.models import * + +g_items_total = Gauge('c3lf_items_total', 'Current Total items') + +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) \ No newline at end of file diff --git a/core/core/settings.py b/core/core/settings.py index 5a8f20f..31a024f 100644 --- a/core/core/settings.py +++ b/core/core/settings.py @@ -70,6 +70,7 @@ INSTALLED_APPS = [ 'inventory', 'mail', 'notify_sessions', + 'core' ] REST_FRAMEWORK = { diff --git a/core/core/urls.py b/core/core/urls.py index 1c5f158..2386891 100644 --- a/core/core/urls.py +++ b/core/core/urls.py @@ -19,6 +19,8 @@ from django.urls import path, include from .version import get_info +from .metrics import * + urlpatterns = [ path('djangoadmin/', admin.site.urls), path('api/2/', include('inventory.api_v2')), diff --git a/deploy/ansible/playbooks/deploy-c3lf-sys3.yml b/deploy/ansible/playbooks/deploy-c3lf-sys3.yml index 544b4e4..679f85a 100644 --- a/deploy/ansible/playbooks/deploy-c3lf-sys3.yml +++ b/deploy/ansible/playbooks/deploy-c3lf-sys3.yml @@ -311,6 +311,16 @@ notify: - restart c3lf-sys3 + - name: add c3lf-sys3-cron service + template: + src: templates/c3lf-sys3-cron.service.j2 + dest: /etc/systemd/system/c3lf-sys3-cron.service + + - name: add c3lf-sys3-cron timer + template: + src: templates/c3lf-sys3-cron.timer.j2 + dest: /etc/systemd/system/c3lf-sys3-cron.timer + - name: reload systemd systemd: daemon_reload: yes @@ -321,6 +331,12 @@ state: started enabled: yes + - name: start c3lf-sys3 timer + service: + name: c3lf-sys3-cron.timer + state: started + enabled: yes + - name: add postfix to www-data group user: name: postfix diff --git a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 new file mode 100644 index 0000000..0df787e --- /dev/null +++ b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 @@ -0,0 +1,13 @@ +[Unit] +Description=c3lf sys3 background service +After=network.target + +[Service] +User=www-data +Group=www-data +WorkingDirectory=/var/www/c3lf-sys3 +ExecStart=/var/www/c3lf-sys3/venv/bin/python3 /var/www/c3lf-sys3/repo/core/manage.py update_metrics +Environment=DJANGO_SETTINGS_MODULE=core.settings + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 new file mode 100644 index 0000000..6d888b4 --- /dev/null +++ b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Run Nextcloud cron.php every 5 minutes + +[Timer] +OnCalendar=minutely +Unit=c3lf-sys3-cron.service + +[Install] +WantedBy=timers.target \ No newline at end of file -- 2.39.5 From cf14a029bb79624ae5086d4eb9e5bfa1c9b141a7 Mon Sep 17 00:00:00 2001 From: lagertonne Date: Mon, 23 Dec 2024 11:42:27 +0100 Subject: [PATCH 2/2] metrics: Make metrics actually usable With this commit, we should get stats for the item count per event per returned-state. --- core/core/management/__init__.py | 0 core/core/management/commands/__init__.py | 0 .../management/commands/update_metrics.py | 8 ---- core/core/metrics.py | 42 +++++++++++++++---- core/core/settings.py | 1 - deploy/ansible/playbooks/deploy-c3lf-sys3.yml | 16 ------- .../templates/c3lf-sys3-cron.service.j2 | 13 ------ .../templates/c3lf-sys3-cron.timer.j2 | 9 ---- 8 files changed, 33 insertions(+), 56 deletions(-) delete mode 100644 core/core/management/__init__.py delete mode 100644 core/core/management/commands/__init__.py delete mode 100644 core/core/management/commands/update_metrics.py delete mode 100644 deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 delete mode 100644 deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 diff --git a/core/core/management/__init__.py b/core/core/management/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/core/management/commands/__init__.py b/core/core/management/commands/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/core/management/commands/update_metrics.py b/core/core/management/commands/update_metrics.py deleted file mode 100644 index a51cfea..0000000 --- a/core/core/management/commands/update_metrics.py +++ /dev/null @@ -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() \ No newline at end of file diff --git a/core/core/metrics.py b/core/core/metrics.py index 4f79754..16a826d 100644 --- a/core/core/metrics.py +++ b/core/core/metrics.py @@ -1,11 +1,35 @@ -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 +from django.db.models import Case, Value, When, BooleanField, Count +from inventory.models import Item -g_items_total = Gauge('c3lf_items_total', 'Current Total items') +class ItemCountCollector(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) \ No newline at end of file + def collect(self): + counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'state']) + + yield counter + + if not apps.models_ready or not apps.apps_ready: + return + + queryset = ( + Item.all_objects + .annotate( + returned=Case( + When(returned_at__isnull=False, then=Value(False)), + default=Value(True), + output_field=BooleanField() + ) + ) + .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"]) + + yield counter + +REGISTRY.register(ItemCountCollector()) \ No newline at end of file diff --git a/core/core/settings.py b/core/core/settings.py index 31a024f..5a8f20f 100644 --- a/core/core/settings.py +++ b/core/core/settings.py @@ -70,7 +70,6 @@ INSTALLED_APPS = [ 'inventory', 'mail', 'notify_sessions', - 'core' ] REST_FRAMEWORK = { diff --git a/deploy/ansible/playbooks/deploy-c3lf-sys3.yml b/deploy/ansible/playbooks/deploy-c3lf-sys3.yml index 679f85a..544b4e4 100644 --- a/deploy/ansible/playbooks/deploy-c3lf-sys3.yml +++ b/deploy/ansible/playbooks/deploy-c3lf-sys3.yml @@ -311,16 +311,6 @@ notify: - restart c3lf-sys3 - - name: add c3lf-sys3-cron service - template: - src: templates/c3lf-sys3-cron.service.j2 - dest: /etc/systemd/system/c3lf-sys3-cron.service - - - name: add c3lf-sys3-cron timer - template: - src: templates/c3lf-sys3-cron.timer.j2 - dest: /etc/systemd/system/c3lf-sys3-cron.timer - - name: reload systemd systemd: daemon_reload: yes @@ -331,12 +321,6 @@ state: started enabled: yes - - name: start c3lf-sys3 timer - service: - name: c3lf-sys3-cron.timer - state: started - enabled: yes - - name: add postfix to www-data group user: name: postfix diff --git a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 deleted file mode 100644 index 0df787e..0000000 --- a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.service.j2 +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=c3lf sys3 background service -After=network.target - -[Service] -User=www-data -Group=www-data -WorkingDirectory=/var/www/c3lf-sys3 -ExecStart=/var/www/c3lf-sys3/venv/bin/python3 /var/www/c3lf-sys3/repo/core/manage.py update_metrics -Environment=DJANGO_SETTINGS_MODULE=core.settings - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 b/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 deleted file mode 100644 index 6d888b4..0000000 --- a/deploy/ansible/playbooks/templates/c3lf-sys3-cron.timer.j2 +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Run Nextcloud cron.php every 5 minutes - -[Timer] -OnCalendar=minutely -Unit=c3lf-sys3-cron.service - -[Install] -WantedBy=timers.target \ No newline at end of file -- 2.39.5