metrics account and websites

This commit is contained in:
Jorge Pastor 2024-01-17 18:03:10 +00:00
parent 1716ea36d0
commit 031a290390
9 changed files with 78 additions and 2 deletions

View File

@ -0,0 +1 @@
default_app_config = 'orchestra.contrib.apps.MetricsConfig'

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,7 @@
from django.apps import AppConfig
class MetricsConfig(AppConfig):
name = 'orchestra.contrib.metrics'

View File

@ -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'])

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -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)

View File

@ -6,6 +6,7 @@ from orchestra.views import serve_private_media
from . import api from . import api
from .utils.apps import isinstalled from .utils.apps import isinstalled
from orchestra.contrib.metrics.views import metrics_view
admin.autodiscover() admin.autodiscover()
@ -22,8 +23,8 @@ urlpatterns = [
url(r'^api-token-auth/', obtain_auth_token, name='api-token-auth'), url(r'^api-token-auth/', obtain_auth_token, name='api-token-auth'),
url(r'^media/(.+)/(.+)/(.+)/(.+)/(.+)$', serve_private_media, name='private-media'), url(r'^media/(.+)/(.+)/(.+)/(.+)/(.+)$', serve_private_media, name='private-media'),
# url(r'search', 'orchestra.views.search', name='search'), # url(r'search', 'orchestra.views.search', name='search'),
# prometheus metrics # METRICS Prometheus
url(r'^metrics/', include('django_prometheus.urls')), url(r'^metrics/', metrics_view, name='metrics'),
] ]