From c83eb1a42b1785ca911c8c193360bff42ade26bf Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 5 Jun 2021 14:41:32 +0200 Subject: [PATCH] prometheus basics and aws fix --- .env.template | 8 +++++++- recipes/settings.py | 7 +++++++ recipes/urls.py | 4 ++++ requirements.txt | 3 ++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.env.template b/.env.template index 09bd6f74..b3bd7fe8 100644 --- a/.env.template +++ b/.env.template @@ -57,8 +57,9 @@ GUNICORN_MEDIA=0 # S3_ACCESS_KEY= # S3_SECRET_ACCESS_KEY= # S3_BUCKET_NAME= +# S3_REGION_NAME= # default none, set your region might be required # S3_QUERYSTRING_AUTH=1 # default true, set to 0 to serve media from a public bucket without signed urls -# AWS_QUERYSTRING_EXPIRE=3600 # number of seconds querystring are valid for +# S3_QUERYSTRING_EXPIRE=3600 # number of seconds querystring are valid for # S3_ENDPOINT_URL= # when using a custom endpoint like minio # Email Settings, see https://docs.djangoproject.com/en/3.2/ref/settings/#email-host @@ -86,6 +87,11 @@ REVERSE_PROXY_AUTH=0 # when unset: 0 (false) # ENABLE_SIGNUP=0 +# enable serving of prometheus metrics under the /metrics path +# ATTENTION: view is not secured (as per the prometheus default way) so make sure to secure it +# trough your web server (or leave it open of you dont care if the stats are exposed) +# ENABLE_METRICS=0 + # allows you to setup OAuth providers # see docs for more information https://vabene1111.github.io/recipes/features/authentication/ # SOCIAL_PROVIDERS = allauth.socialaccount.providers.github, allauth.socialaccount.providers.nextcloud, diff --git a/recipes/settings.py b/recipes/settings.py index 2dc1299a..31928ad0 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -81,6 +81,7 @@ INSTALLED_APPS = [ 'django.contrib.sites', 'django.contrib.staticfiles', 'django.contrib.postgres', + 'django_prometheus', 'django_tables2', 'corsheaders', 'django_filters', @@ -105,6 +106,8 @@ SOCIALACCOUNT_PROVIDERS = ast.literal_eval( ENABLE_SIGNUP = bool(int(os.getenv('ENABLE_SIGNUP', False))) +ENABLE_METRICS = bool(int(os.getenv('ENABLE_METRICS', False))) + MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', @@ -119,6 +122,9 @@ MIDDLEWARE = [ 'cookbook.helper.scope_middleware.ScopeMiddleware', ] +if ENABLE_METRICS: + MIDDLEWARE += 'django_prometheus.middleware.PrometheusAfterMiddleware', + # Auth related settings AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', @@ -308,6 +314,7 @@ if os.getenv('S3_ACCESS_KEY', ''): AWS_QUERYSTRING_AUTH = bool(int(os.getenv('S3_QUERYSTRING_AUTH', True))) AWS_QUERYSTRING_EXPIRE = int(os.getenv('S3_QUERYSTRING_EXPIRE', 3600)) AWS_S3_SIGNATURE_VERSION = os.getenv('S3_SIGNATURE_VERSION', 's3v4') + AWS_S3_REGION_NAME = os.getenv('S3_REGION_NAME', None) if os.getenv('S3_ENDPOINT_URL', ''): AWS_S3_ENDPOINT_URL = os.getenv('S3_ENDPOINT_URL', '') diff --git a/recipes/urls.py b/recipes/urls.py index 29af6332..34bf6542 100644 --- a/recipes/urls.py +++ b/recipes/urls.py @@ -15,6 +15,7 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf import settings +from django.conf.urls import url from django.contrib import admin from django.urls import include, path, re_path from django.views.i18n import JavaScriptCatalog @@ -33,6 +34,9 @@ urlpatterns = [ ), ] +if settings.ENABLE_METRICS: + urlpatterns += url('', include('django_prometheus.urls')), + if settings.GUNICORN_MEDIA or settings.DEBUG: urlpatterns += re_path(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT}), urlpatterns += re_path(r'^jsreverse.json$', reverse_views.urls_js, name='js_reverse'), diff --git a/requirements.txt b/requirements.txt index baaf3fd5..21fecd8e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,4 +37,5 @@ pytest==6.2.4 pytest-django==4.3.0 django-cors-headers==3.7.0 django-storages==1.11.1 -boto3==1.17.84 \ No newline at end of file +boto3==1.17.84 +django-prometheus==2.1.0 \ No newline at end of file