From d9096281f484120ca47393aa0038cad39de9dc0f Mon Sep 17 00:00:00 2001 From: Yurii Purdenko Date: Thu, 25 Jul 2024 13:29:24 +0200 Subject: [PATCH] feat: #172 dev version --- bhtom2/middleware.py | 1 + bhtom2/prometheus_metrics.py | 12 ++++++++++++ bhtom2/signals.py | 22 +++++++++++++++------- bhtom2/urls.py | 3 +++ docker/dev/docker-compose.yml | 22 ++++++++++++++++++++++ docker/dev/prometheus.yml | 8 ++++++++ requirements.txt | 2 ++ settings/settings.py | 7 +++++-- 8 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 bhtom2/prometheus_metrics.py create mode 100644 docker/dev/prometheus.yml diff --git a/bhtom2/middleware.py b/bhtom2/middleware.py index dae114ba..19a09472 100644 --- a/bhtom2/middleware.py +++ b/bhtom2/middleware.py @@ -1,6 +1,7 @@ import logging import datetime + from bhtom2.utils.bhtom_logger import BHTOMLogger from bhtom2.utils.access_utils import can_access from django.http import HttpResponseForbidden diff --git a/bhtom2/prometheus_metrics.py b/bhtom2/prometheus_metrics.py new file mode 100644 index 00000000..3479941c --- /dev/null +++ b/bhtom2/prometheus_metrics.py @@ -0,0 +1,12 @@ +from django.http import HttpResponse +from prometheus_client import Counter, Gauge +from prometheus_client import generate_latest, CONTENT_TYPE_LATEST + +USER_REGISTRATION_COUNT = Counter('django_user_registration_total', 'Total user registrations') +USER_LOGIN_COUNT = Counter('django_user_login_total', 'Total user logins') +CAMERA_REGISTRATION_COUNT = Counter('django_camera_registration_total', 'Total camera registrations') +LAST_USER_LOGIN_TIME = Gauge('django_user_last_login_timestamp', 'Timestamp of the last user login') + + +def custom_metrics(request): + return HttpResponse(generate_latest(), content_type=CONTENT_TYPE_LATEST) \ No newline at end of file diff --git a/bhtom2/signals.py b/bhtom2/signals.py index 56a2e49a..dbdd07bd 100644 --- a/bhtom2/signals.py +++ b/bhtom2/signals.py @@ -1,13 +1,14 @@ -from django.db.models.signals import pre_save, post_save -from django.dispatch import receiver -from bhtom_base.bhtom_targets.models import Target -from django.conf import settings -from bhtom2.utils.bhtom_logger import BHTOMLogger -from bhtom2.utils.coordinate_utils import fill_galactic_coordinates from django.contrib.auth.models import User +from django.db.models.signals import pre_save +from django.contrib.auth.signals import user_logged_in from django.dispatch import receiver from django.core.mail import send_mail -from bhtom2.bhtom_observatory.models import Observatory,Camera + +from django.conf import settings +from bhtom2.utils.bhtom_logger import BHTOMLogger +from bhtom_base.bhtom_targets.models import Target +from bhtom2.bhtom_observatory.models import Camera +from bhtom2.prometheus_metrics import USER_LOGIN_COUNT, USER_REGISTRATION_COUNT, LAST_USER_LOGIN_TIME, CAMERA_REGISTRATION_COUNT logger: BHTOMLogger = BHTOMLogger(__name__, 'Bhtom: Signals') @@ -24,6 +25,7 @@ def send_activation_email(sender, instance, **kwargs): except Exception as e: user_old = None logger.info("Created new user : " + instance.username ) + USER_REGISTRATION_COUNT.inc() if user_old is not None: if instance.is_active and not user_old.is_active: @@ -42,6 +44,7 @@ def Camera_pre_save(sender, instance, **kwargs): camera_old = Camera.objects.get(id=instance.pk) except Camera.DoesNotExist: camera_old = None + CAMERA_REGISTRATION_COUNT.inc() if camera_old is not None: if camera_old.active_flg is False and instance.active_flg is True and camera_old.user_id is not None: @@ -53,3 +56,8 @@ def Camera_pre_save(sender, instance, **kwargs): logger.info('Activate camera' + instance.camera_name + ', Send mail: ' + user.email) except Exception as e: logger.info('Activate camera error: ' + str(e)) + +@receiver(user_logged_in) +def user_logged_in_handler(sender, request, user, **kwargs): + USER_LOGIN_COUNT.inc() + LAST_USER_LOGIN_TIME.set_to_current_time() \ No newline at end of file diff --git a/bhtom2/urls.py b/bhtom2/urls.py index 4b7279b2..3d22db1a 100644 --- a/bhtom2/urls.py +++ b/bhtom2/urls.py @@ -20,6 +20,7 @@ from settings import settings from bhtom2.views import BrokerQueryListView +from bhtom2.prometheus_metrics import custom_metrics schema_view = get_schema_view( openapi.Info( @@ -46,6 +47,8 @@ namespace='registration')), path('alerts/query/list/', BrokerQueryListView.as_view(template_name='bhtom_alerts/brokerquery_list.html'), name='alerts:list'), + path('', include('django_prometheus.urls')), + path('metrics/', custom_metrics, name='custom_metrics'), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml index 39c473a4..0742ac13 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -97,6 +97,28 @@ services: KAFKA_CLUSTERS_0_ZOOKEEPER: "zookeeper1:2181" depends_on: - kafka1 + + prometheus: + image: prom/prometheus:latest + container_name: prometheus + volumes: + - ./prometheus.yml:/etc/prometheus/prometheus.yml + ports: + - "9090:9090" + networks: + - bhtom_network + + grafana: + image: grafana/grafana:latest + container_name: grafana + environment: + - GF_SECURITY_ADMIN_PASSWORD=admin + ports: + - "3000:3000" + networks: + - bhtom_network + depends_on: + - prometheus volumes: data_store: diff --git a/docker/dev/prometheus.yml b/docker/dev/prometheus.yml new file mode 100644 index 00000000..af557c11 --- /dev/null +++ b/docker/dev/prometheus.yml @@ -0,0 +1,8 @@ +global: + scrape_interval: 15s + +scrape_configs: + - job_name: 'django' + metrics_path: '/metrics' + static_configs: + - targets: ['bhtom:8000','upload-service:8000'] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5f8d1224..ce6bc37b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -48,3 +48,5 @@ django_guid==3.3.1 gunicorn==21.2.0 gevent==23.9.1 bleach==6.1.0 +django-prometheus==2.3.1 +prometheus_client==0.20.0 \ No newline at end of file diff --git a/settings/settings.py b/settings/settings.py index 474f4269..36bd623c 100644 --- a/settings/settings.py +++ b/settings/settings.py @@ -125,7 +125,8 @@ 'bhtom2.bhtom_calibration.apps.BhtomCalibrationConfig', 'crispy_bootstrap4', 'drf_yasg', - 'django_guid' + 'django_guid', + 'django_prometheus' ] MIDDLEWARE = [ @@ -142,7 +143,9 @@ 'bhtom_custom_registration.bhtom_registration.middleware.RedirectAuthenticatedUsersFromRegisterMiddleware', 'django_guid.middleware.guid_middleware', 'bhtom2.middleware.RequestLogMiddleware', - 'bhtom2.middleware.AccessControlMiddleware' + 'bhtom2.middleware.AccessControlMiddleware', + 'django_prometheus.middleware.PrometheusBeforeMiddleware', + 'django_prometheus.middleware.PrometheusAfterMiddleware', ] ROOT_URLCONF = 'bhtom2.urls'