From 1e3ff8d59e3811d27fa96fe85ec66448393081e6 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Mon, 9 Dec 2024 12:47:44 +0100 Subject: [PATCH 1/9] add django-prometheus integration --- contrib/common/gunicorn/gunicorn.ini | 6 ++++++ docker/Dockerfile-prod | 6 ++++++ requirements/base.txt | 1 + src/ralph/settings/base.py | 11 +++++++++++ src/ralph/settings/prod.py | 9 +++++++++ src/ralph/urls/base.py | 11 +++++++++++ 6 files changed, 44 insertions(+) diff --git a/contrib/common/gunicorn/gunicorn.ini b/contrib/common/gunicorn/gunicorn.ini index ac392e06fe..d78a0e01b2 100644 --- a/contrib/common/gunicorn/gunicorn.ini +++ b/contrib/common/gunicorn/gunicorn.ini @@ -1,6 +1,7 @@ """gunicorn WSGI server configuration.""" import os +from prometheus_client import multiprocess bind = os.environ.get('GUNICORN_BIND', '0.0.0.0:' + os.environ.get('PORT', '8000')) workers = os.environ.get('GUNICORN_WORKERS', 4) @@ -10,3 +11,8 @@ loglevel = os.environ.get('GUNICORN_LOGLEVEL', 'info') user = os.environ.get('GUNICORN_USER', 'root') group = os.environ.get('GUNICORN_GROUP', 'root') timeout = os.environ.get('GUNICORN_TIMEOUT', 30) + + +# Multiprocess mode: https://prometheus.github.io/client_python/multiprocess/ +def child_exit(server, worker): + multiprocess.mark_process_dead(worker.pid) diff --git a/docker/Dockerfile-prod b/docker/Dockerfile-prod index 83f3a817a6..f9617a399a 100644 --- a/docker/Dockerfile-prod +++ b/docker/Dockerfile-prod @@ -22,6 +22,12 @@ RUN apt-get clean && \ apt-get -y install apt-transport-https ca-certificates gnupg2 locales curl && \ rm -rf /var/lib/apt/lists/* + +# Multiprocess mode: https://prometheus.github.io/client_python/multiprocess/ +ENV PROMETHEUS_MULTIPROC_DIR="/tmp/prometheus-metrics" +RUN mkdir -p /tmp/prometheus-metrics + + # set UTF-8 locale RUN locale-gen en_US.UTF-8 ENV LANG en_US.UTF-8 diff --git a/requirements/base.txt b/requirements/base.txt index 123a32a4c1..d4071868bd 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -12,6 +12,7 @@ django-filter==2.0.0 django-import-export==1.2.0 django-money==0.12.3 django-mptt==0.11 +django-prometheus==2.2.0 django-reversion==3.0.5 django-rq==2.0 django-sitetree==1.13.0 diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 2fcaa41df1..7ceee970bf 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -185,6 +185,11 @@ def get_sentinels(sentinels_string): } } +TRANSITION_TEMPLATES = ( + ('transitions/release_asset.html', 'Release asset (Metrum)'), + ('transitions/return_asset.html', 'Return asset (Metrum)'), +) + AUTH_USER_MODEL = 'accounts.RalphUser' LOGIN_URL = '/login/' @@ -682,6 +687,7 @@ def get_sentinels(sentinels_string): # METRICS COLLECT_METRICS = False ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = False +PROMETHEUS_METRICS_ENABLED = False STATSD_GRAPHS_PREFIX = 'ralph.graphs' ENABLE_REQUESTS_AND_QUERIES_METRICS = True @@ -693,6 +699,11 @@ def get_sentinels(sentinels_string): CONVERT_TO_DATACENTER_ASSET_DEFAULT_STATUS_ID = 1 CONVERT_TO_BACKOFFICE_ASSET_DEFAULT_STATUS_ID = 1 +if PROMETHEUS_METRICS_ENABLED: + INSTALLED_APPS += ( + 'django_prometheus', + ) + # Currency choices for django-money DEFAULT_CURRENCY_CODE = 'XXX' CURRENCY_CHOICES = [ diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index 33a590ba34..d350daa47e 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -79,6 +79,15 @@ 'ralph.lib.metrics.middlewares.RequestMetricsMiddleware', ) + MIDDLEWARE + if bool_from_env('PROMETHEUS_METRICS_ENABLED'): + PROMETHEUS_METRICS_ENABLED = True + MIDDLEWARE = ( + 'django_prometheus.middleware.PrometheusBeforeMiddleware', + ) + MIDDLEWARE + MIDDLEWARE = MIDDLEWARE + ( + 'django_prometheus.middleware.PrometheusAfterMiddleware', + ) + ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = bool_from_env( 'ALLOW_PUSH_GRAPHS_DATA_TO_STATSD' ) diff --git a/src/ralph/urls/base.py b/src/ralph/urls/base.py index 056a3e72ba..90c8393ed9 100644 --- a/src/ralph/urls/base.py +++ b/src/ralph/urls/base.py @@ -1,5 +1,7 @@ from django.conf import settings from django.conf.urls import include, url +from django.urls import path +from django_prometheus import exports from rest_framework.authtoken import views from sitetree.sitetreeapp import SiteTree # noqa @@ -71,3 +73,12 @@ if getattr(settings, 'ENABLE_HERMES_INTEGRATION', False): urlpatterns += url(r'^hermes/', include('pyhermes.apps.django.urls')), + +if getattr(settings, 'PROMETHEUS_METRICS_ENABLED', False): + urlpatterns += [ + path( + "status/prometheus", + exports.ExportToDjangoView, + name="status-prometheus", + ) + ] From c3447adb8484bb511a1448484bb7f546cd31c028 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Mon, 9 Dec 2024 13:40:12 +0100 Subject: [PATCH 2/9] cleanup --- src/ralph/settings/base.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 7ceee970bf..15ccc6a608 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -185,11 +185,6 @@ def get_sentinels(sentinels_string): } } -TRANSITION_TEMPLATES = ( - ('transitions/release_asset.html', 'Release asset (Metrum)'), - ('transitions/return_asset.html', 'Return asset (Metrum)'), -) - AUTH_USER_MODEL = 'accounts.RalphUser' LOGIN_URL = '/login/' From b013e6bfbbc00fc16e28cc83d8d15fa5b4160f61 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 08:48:04 +0100 Subject: [PATCH 3/9] refactor prod settings --- src/ralph/settings/prod.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index d350daa47e..e56c894a4a 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -79,15 +79,6 @@ 'ralph.lib.metrics.middlewares.RequestMetricsMiddleware', ) + MIDDLEWARE - if bool_from_env('PROMETHEUS_METRICS_ENABLED'): - PROMETHEUS_METRICS_ENABLED = True - MIDDLEWARE = ( - 'django_prometheus.middleware.PrometheusBeforeMiddleware', - ) + MIDDLEWARE - MIDDLEWARE = MIDDLEWARE + ( - 'django_prometheus.middleware.PrometheusAfterMiddleware', - ) - ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = bool_from_env( 'ALLOW_PUSH_GRAPHS_DATA_TO_STATSD' ) @@ -95,3 +86,12 @@ STATSD_GRAPHS_PREFIX = os.environ.get( 'STATSD_GRAPHS_PREFIX', 'ralph.graphs' ) + +if bool_from_env('PROMETHEUS_METRICS_ENABLED'): + PROMETHEUS_METRICS_ENABLED = True + MIDDLEWARE = ( + 'django_prometheus.middleware.PrometheusBeforeMiddleware', + ) + MIDDLEWARE + MIDDLEWARE = MIDDLEWARE + ( + 'django_prometheus.middleware.PrometheusAfterMiddleware', + ) From 26c47b758ce6c26198394a0ee055730daafcbdbc Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 11:09:52 +0100 Subject: [PATCH 4/9] refactor prod settings --- src/ralph/settings/base.py | 2 +- src/ralph/settings/prod.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 15ccc6a608..6ca87fc646 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -682,7 +682,6 @@ def get_sentinels(sentinels_string): # METRICS COLLECT_METRICS = False ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = False -PROMETHEUS_METRICS_ENABLED = False STATSD_GRAPHS_PREFIX = 'ralph.graphs' ENABLE_REQUESTS_AND_QUERIES_METRICS = True @@ -694,6 +693,7 @@ def get_sentinels(sentinels_string): CONVERT_TO_DATACENTER_ASSET_DEFAULT_STATUS_ID = 1 CONVERT_TO_BACKOFFICE_ASSET_DEFAULT_STATUS_ID = 1 +PROMETHEUS_METRICS_ENABLED = True if PROMETHEUS_METRICS_ENABLED: INSTALLED_APPS += ( 'django_prometheus', diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index e56c894a4a..d57d4bccb9 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -87,7 +87,7 @@ 'STATSD_GRAPHS_PREFIX', 'ralph.graphs' ) -if bool_from_env('PROMETHEUS_METRICS_ENABLED'): +if bool_from_env('PROMETHEUS_METRICS_ENABLED', True): PROMETHEUS_METRICS_ENABLED = True MIDDLEWARE = ( 'django_prometheus.middleware.PrometheusBeforeMiddleware', From c0d179f43d6b5003406b4226035769842e2f7a72 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 11:45:07 +0100 Subject: [PATCH 5/9] fix settings --- src/ralph/settings/base.py | 6 ------ src/ralph/settings/prod.py | 3 +++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ralph/settings/base.py b/src/ralph/settings/base.py index 6ca87fc646..2fcaa41df1 100644 --- a/src/ralph/settings/base.py +++ b/src/ralph/settings/base.py @@ -693,12 +693,6 @@ def get_sentinels(sentinels_string): CONVERT_TO_DATACENTER_ASSET_DEFAULT_STATUS_ID = 1 CONVERT_TO_BACKOFFICE_ASSET_DEFAULT_STATUS_ID = 1 -PROMETHEUS_METRICS_ENABLED = True -if PROMETHEUS_METRICS_ENABLED: - INSTALLED_APPS += ( - 'django_prometheus', - ) - # Currency choices for django-money DEFAULT_CURRENCY_CODE = 'XXX' CURRENCY_CHOICES = [ diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index d57d4bccb9..0abd6806e7 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -95,3 +95,6 @@ MIDDLEWARE = MIDDLEWARE + ( 'django_prometheus.middleware.PrometheusAfterMiddleware', ) + INSTALLED_APPS += ( + 'django_prometheus', + ) From 158240442f2067a77db3cc0288047b6f9ce889c6 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 12:16:04 +0100 Subject: [PATCH 6/9] fix settings --- src/ralph/settings/prod.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index 0abd6806e7..126ea7a306 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -68,6 +68,10 @@ 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } +try: + MIDDLEWARE +except NameError: + MIDDLEWARE = () if bool_from_env('COLLECT_METRICS'): COLLECT_METRICS = True From 547fd8ad50ca0bb1aceee27cf11e40f3a70b9167 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 12:32:06 +0100 Subject: [PATCH 7/9] test settings --- src/ralph/settings/prod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index 126ea7a306..5af5287e15 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -91,7 +91,7 @@ 'STATSD_GRAPHS_PREFIX', 'ralph.graphs' ) -if bool_from_env('PROMETHEUS_METRICS_ENABLED', True): +if bool_from_env('PROMETHEUS_METRICS_ENABLED', False): PROMETHEUS_METRICS_ENABLED = True MIDDLEWARE = ( 'django_prometheus.middleware.PrometheusBeforeMiddleware', From 3f019bc9cb65c1c1efb55be290195f02344a392a Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 12:38:10 +0100 Subject: [PATCH 8/9] test settings --- src/ralph/settings/prod.py | 2 +- src/ralph/settings/test.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index 5af5287e15..126ea7a306 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -91,7 +91,7 @@ 'STATSD_GRAPHS_PREFIX', 'ralph.graphs' ) -if bool_from_env('PROMETHEUS_METRICS_ENABLED', False): +if bool_from_env('PROMETHEUS_METRICS_ENABLED', True): PROMETHEUS_METRICS_ENABLED = True MIDDLEWARE = ( 'django_prometheus.middleware.PrometheusBeforeMiddleware', diff --git a/src/ralph/settings/test.py b/src/ralph/settings/test.py index 72aa5910ca..950b4e42d2 100644 --- a/src/ralph/settings/test.py +++ b/src/ralph/settings/test.py @@ -82,3 +82,5 @@ def __getitem__(self, item): ENABLE_HERMES_INTEGRATION = True HERMES['ENABLED'] = ENABLE_HERMES_INTEGRATION + +PROMETHEUS_METRICS_ENABLED = False From 4089ada765921f6a043a69cfe0e730f8d51c2755 Mon Sep 17 00:00:00 2001 From: awieckowski Date: Tue, 10 Dec 2024 13:13:36 +0100 Subject: [PATCH 9/9] fix settings --- src/ralph/settings/prod.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ralph/settings/prod.py b/src/ralph/settings/prod.py index 126ea7a306..3a55cb4004 100644 --- a/src/ralph/settings/prod.py +++ b/src/ralph/settings/prod.py @@ -68,11 +68,6 @@ 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } -try: - MIDDLEWARE -except NameError: - MIDDLEWARE = () - if bool_from_env('COLLECT_METRICS'): COLLECT_METRICS = True STATSD_HOST = os.environ.get('STATSD_HOST') @@ -93,6 +88,7 @@ if bool_from_env('PROMETHEUS_METRICS_ENABLED', True): PROMETHEUS_METRICS_ENABLED = True + PROMETHEUS_EXPORT_MIGRATIONS = False MIDDLEWARE = ( 'django_prometheus.middleware.PrometheusBeforeMiddleware', ) + MIDDLEWARE