From 031a29039001bcbafce075bf5b3089020f453968 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Wed, 17 Jan 2024 18:03:10 +0000 Subject: [PATCH] metrics account and websites --- orchestra/contrib/metrics/__init__.py | 1 + orchestra/contrib/metrics/admin.py | 3 ++ orchestra/contrib/metrics/apps.py | 7 ++++ orchestra/contrib/metrics/custom_metrics.py | 42 +++++++++++++++++++ .../contrib/metrics/migrations/__init__.py | 0 orchestra/contrib/metrics/models.py | 3 ++ orchestra/contrib/metrics/tests.py | 3 ++ orchestra/contrib/metrics/views.py | 16 +++++++ orchestra/urls.py | 5 ++- 9 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 orchestra/contrib/metrics/__init__.py create mode 100644 orchestra/contrib/metrics/admin.py create mode 100644 orchestra/contrib/metrics/apps.py create mode 100644 orchestra/contrib/metrics/custom_metrics.py create mode 100644 orchestra/contrib/metrics/migrations/__init__.py create mode 100644 orchestra/contrib/metrics/models.py create mode 100644 orchestra/contrib/metrics/tests.py create mode 100644 orchestra/contrib/metrics/views.py diff --git a/orchestra/contrib/metrics/__init__.py b/orchestra/contrib/metrics/__init__.py new file mode 100644 index 00000000..6fbbb002 --- /dev/null +++ b/orchestra/contrib/metrics/__init__.py @@ -0,0 +1 @@ +default_app_config = 'orchestra.contrib.apps.MetricsConfig' diff --git a/orchestra/contrib/metrics/admin.py b/orchestra/contrib/metrics/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/orchestra/contrib/metrics/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/orchestra/contrib/metrics/apps.py b/orchestra/contrib/metrics/apps.py new file mode 100644 index 00000000..89383d58 --- /dev/null +++ b/orchestra/contrib/metrics/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig + + +class MetricsConfig(AppConfig): + name = 'orchestra.contrib.metrics' + + diff --git a/orchestra/contrib/metrics/custom_metrics.py b/orchestra/contrib/metrics/custom_metrics.py new file mode 100644 index 00000000..63e18a82 --- /dev/null +++ b/orchestra/contrib/metrics/custom_metrics.py @@ -0,0 +1,42 @@ +from prometheus_client import start_http_server, Gauge +import random + +from orchestra.contrib.accounts.models import Account +from orchestra.contrib.websites.models import Website + + +# Crear métricas de tipo Gauge con etiquetas +usuarios_metrica = Gauge('usuarios', 'Número total de usuarios', ['estado']) +websites_metrica = Gauge('websites_server', 'Número total de websites en server', ['target_server', 'estado']) + +def actualizar_metrica_usuarios(): + # Generar una lista de usuarios aleatorios para el ejemplo + usuarios = Account.objects.all() + + # Contar usuarios activos, no activos y actualizar la métrica + usuarios_activos = sum(1 for usuario in usuarios if usuario.is_active) + usuarios_no_activos = len(usuarios) - usuarios_activos + + usuarios_metrica.labels(estado='activo').set(usuarios_activos) + usuarios_metrica.labels(estado='no_activo').set(usuarios_no_activos) + usuarios_metrica.labels(estado='total').set(len(usuarios)) + + +def actualizar_metrica_websites(): + websites = Website.objects.all() + + website_dict = {} + for website in websites: + if website.target_server.name not in website_dict.keys(): + website_dict[website.target_server.name] = {'activo':0, 'inactivo':0} + + if website.is_active: + website_dict[website.target_server.name]['activo'] += 1 + else: + website_dict[website.target_server.name]['inactivo'] += 1 + + for server, value in website_dict.items(): + websites_metrica.labels(target_server=server, estado='activo').set(value['activo']) + websites_metrica.labels(target_server=server, estado='no_activo').set(value['inactivo']) + + \ No newline at end of file diff --git a/orchestra/contrib/metrics/migrations/__init__.py b/orchestra/contrib/metrics/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/orchestra/contrib/metrics/models.py b/orchestra/contrib/metrics/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/orchestra/contrib/metrics/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/orchestra/contrib/metrics/tests.py b/orchestra/contrib/metrics/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/orchestra/contrib/metrics/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/orchestra/contrib/metrics/views.py b/orchestra/contrib/metrics/views.py new file mode 100644 index 00000000..2ea45583 --- /dev/null +++ b/orchestra/contrib/metrics/views.py @@ -0,0 +1,16 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.views.decorators.http import require_GET +from prometheus_client.exposition import generate_latest +from prometheus_client import REGISTRY, CONTENT_TYPE_LATEST +from .custom_metrics import actualizar_metrica_usuarios, actualizar_metrica_websites + +@require_GET +def metrics_view(request): + # Actualizar métricas antes de generar el contenido + actualizar_metrica_usuarios() + actualizar_metrica_websites() + + # Devolver las métricas exportadas como respuesta HTTP + output = generate_latest(REGISTRY) + return HttpResponse(output, content_type=CONTENT_TYPE_LATEST) diff --git a/orchestra/urls.py b/orchestra/urls.py index dd9872fc..a579bcfb 100644 --- a/orchestra/urls.py +++ b/orchestra/urls.py @@ -6,6 +6,7 @@ from orchestra.views import serve_private_media from . import api from .utils.apps import isinstalled +from orchestra.contrib.metrics.views import metrics_view admin.autodiscover() @@ -22,8 +23,8 @@ urlpatterns = [ url(r'^api-token-auth/', obtain_auth_token, name='api-token-auth'), url(r'^media/(.+)/(.+)/(.+)/(.+)/(.+)$', serve_private_media, name='private-media'), # url(r'search', 'orchestra.views.search', name='search'), - # prometheus metrics - url(r'^metrics/', include('django_prometheus.urls')), + # METRICS Prometheus + url(r'^metrics/', metrics_view, name='metrics'), ]