Skip to content

Commit

Permalink
feat: #172 dev version
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Purdenko committed Jul 25, 2024
1 parent 61d77ad commit d909628
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions bhtom2/middleware.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions bhtom2/prometheus_metrics.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 15 additions & 7 deletions bhtom2/signals.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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()
3 changes: 3 additions & 0 deletions bhtom2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)

22 changes: 22 additions & 0 deletions docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions docker/dev/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global:
scrape_interval: 15s

scrape_configs:
- job_name: 'django'
metrics_path: '/metrics'
static_configs:
- targets: ['bhtom:8000','upload-service:8000']
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
'bhtom2.bhtom_calibration.apps.BhtomCalibrationConfig',
'crispy_bootstrap4',
'drf_yasg',
'django_guid'
'django_guid',
'django_prometheus'
]

MIDDLEWARE = [
Expand All @@ -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'
Expand Down

0 comments on commit d909628

Please sign in to comment.