metrics account and websites
This commit is contained in:
parent
1716ea36d0
commit
031a290390
1
orchestra/contrib/metrics/__init__.py
Normal file
1
orchestra/contrib/metrics/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
default_app_config = 'orchestra.contrib.apps.MetricsConfig'
|
3
orchestra/contrib/metrics/admin.py
Normal file
3
orchestra/contrib/metrics/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
7
orchestra/contrib/metrics/apps.py
Normal file
7
orchestra/contrib/metrics/apps.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MetricsConfig(AppConfig):
|
||||
name = 'orchestra.contrib.metrics'
|
||||
|
||||
|
42
orchestra/contrib/metrics/custom_metrics.py
Normal file
42
orchestra/contrib/metrics/custom_metrics.py
Normal 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'])
|
||||
|
||||
|
0
orchestra/contrib/metrics/migrations/__init__.py
Normal file
0
orchestra/contrib/metrics/migrations/__init__.py
Normal file
3
orchestra/contrib/metrics/models.py
Normal file
3
orchestra/contrib/metrics/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
orchestra/contrib/metrics/tests.py
Normal file
3
orchestra/contrib/metrics/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
16
orchestra/contrib/metrics/views.py
Normal file
16
orchestra/contrib/metrics/views.py
Normal 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)
|
|
@ -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'),
|
||||
]
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue