From 6b6c05f07d8e6902f1341de69920d22c9444c508 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 13 Jan 2025 18:09:48 +0930 Subject: [PATCH] feat(base): Add django-prometheus for metrics export ref: #156 #469 --- .vscode/launch.json | 1 + app/app/settings.py | 16 ++++++++++++++-- includes/etc/itsm/settings.py | 3 +++ requirements.txt | 4 ++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 77384920..955cd203 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -23,6 +23,7 @@ "request": "launch", "module": "gunicorn", "args": [ + "--config=../includes/etc/gunicorn.conf.py", "--access-logfile", "-", "--workers", diff --git a/app/app/settings.py b/app/app/settings.py index 5ed82700..22f5c0c4 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -64,9 +64,18 @@ CELERY_WORKER_CONCURRENCY = 2 # worker_concurrency - Default: Number of CPU cor CELERY_WORKER_DEDUPLICATE_SUCCESSFUL_TASKS = True # worker_deduplicate_successful_tasks CELERY_WORKER_MAX_TASKS_PER_CHILD = 1 # worker_max_tasks_per_child # CELERY_WORKER_MAX_MEMORY_PER_CHILD = 10000 # 10000=10mb worker_max_memory_per_child - Default: No limit. Type: int (kilobytes) -# CELERY_TASK_SEND_SENT_EVENT = True +CELERY_TASK_SEND_SENT_EVENT = True CELERY_WORKER_SEND_TASK_EVENTS = True # worker_send_task_events + +# PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range(8010, 8010) +# PROMETHEUS_METRICS_EXPORT_PORT = 8010 +# PROMETHEUS_METRICS_EXPORT_ADDRESS = '' + +METRICS_ENABLED = True # Enable Metrics +METRICS_EXPORT_PORT = 8080 # Port to serve metrics on +METRICS_MULTIPROC_DIR = '/tmp/prometheus' # path the metrics from multiple-process' save to + # django setting. CACHES = { 'default': { @@ -125,9 +134,11 @@ INSTALLED_APPS = [ 'drf_spectacular_sidecar', 'config_management.apps.ConfigManagementConfig', 'project_management.apps.ProjectManagementConfig', + 'django_prometheus', ] MIDDLEWARE = [ + 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -138,6 +149,7 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'core.middleware.get_request.RequestMiddleware', 'app.middleware.timezone.TimezoneMiddleware', + 'django_prometheus.middleware.PrometheusAfterMiddleware', ] @@ -170,7 +182,7 @@ WSGI_APPLICATION = 'app.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', + 'ENGINE': 'django_prometheus.db.backends.sqlite3', 'NAME': str(BASE_DIR / 'db.sqlite3'), } } diff --git a/includes/etc/itsm/settings.py b/includes/etc/itsm/settings.py index 47f2601c..7da37ce1 100644 --- a/includes/etc/itsm/settings.py +++ b/includes/etc/itsm/settings.py @@ -1,5 +1,8 @@ # ITSM Docker Settings +# If metrics enabled, see https://nofusscomputing.com/projects/centurion_erp/administration/monitoring/#django-exporter-setup) +# to configure the database metrics. + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', diff --git a/requirements.txt b/requirements.txt index 3ee9ca18..ed0535cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,3 +30,7 @@ Pygments==2.18.0 celery==5.4.0 django-celery-results==2.5.1 sqlalchemy==2.0.31 + +# metrics +prometheus-client==0.21.1 +django-prometheus==2.3.1