diff options
author | 2021-09-04 10:04:15 +0100 | |
---|---|---|
committer | 2021-09-04 10:04:15 +0100 | |
commit | 2ce7bdd88bb943b7a10eb409bfb1802076762541 (patch) | |
tree | 49895c3c85a2c8cec2d033b51bd0028c548a4e22 | |
parent | Merge pull request #579 from python-discord/jb3/update-k8s-path (diff) | |
parent | Merge branch 'main' into jb3/django-prometheus (diff) |
Merge pull request #575 from python-discord/jb3/django-prometheus
Add Django Prometheus
-rw-r--r-- | .flake8 | 2 | ||||
-rw-r--r-- | Dockerfile | 5 | ||||
-rw-r--r-- | gunicorn.conf.py | 10 | ||||
-rwxr-xr-x | manage.py | 1 | ||||
-rw-r--r-- | poetry.lock | 32 | ||||
-rw-r--r-- | pydis_site/apps/home/urls.py | 1 | ||||
-rw-r--r-- | pydis_site/settings.py | 12 | ||||
-rw-r--r-- | pyproject.toml | 1 |
8 files changed, 60 insertions, 4 deletions
@@ -3,7 +3,7 @@ max-line-length=100 docstring-convention=all import-order-style=pycharm application_import_names=pydis_site -exclude=__pycache__, venv, .venv, **/migrations/**, .cache/ +exclude=__pycache__, venv, .venv, **/migrations/**, .cache/, gunicorn.conf.py ignore= B311,W503,E226,S311,T000 # Missing Docstrings @@ -26,6 +26,11 @@ COPY . . # Set dummy variables so collectstatic can load settings.py RUN \ + # Set BUILDING_DOCKER to anything but undefined so settings.py + # does not insert django_prometheus into the list of installed apps. + # This prevents django_prometheus from attempting to connect to the database + # when the collectstatic task is ran. + BUILDING_DOCKER=yes \ SECRET_KEY=dummy_value \ DATABASE_URL=postgres://localhost \ METRICITY_DB_URL=postgres://localhost \ diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 00000000..4930ae5b --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,10 @@ +""" +Configuration file for gunicorn. + +Code taken from https://github.com/prometheus/client_python#multiprocess-mode-eg-gunicorn +""" +from prometheus_client import multiprocess + + +def child_exit(server, worker) -> None: + multiprocess.mark_process_dead(worker.pid) @@ -179,6 +179,7 @@ class SiteManager: "-w", "2", "--statsd-host", "graphite.default.svc.cluster.local:8125", "--statsd-prefix", "site", + "--config", "file:gunicorn.conf.py" ] # Run gunicorn for the production server. diff --git a/poetry.lock b/poetry.lock index 76c8890b..8e66cb57 100644 --- a/poetry.lock +++ b/poetry.lock @@ -158,6 +158,17 @@ optional = false python-versions = ">=3.5" [[package]] +name = "django-prometheus" +version = "2.1.0" +description = "Django middlewares to monitor your application with Prometheus.io." +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +prometheus-client = ">=0.7" + +[[package]] name = "django-simple-bulma" version = "2.2.0" description = "Django application to add the Bulma CSS framework and its extensions" @@ -456,6 +467,17 @@ toml = "*" virtualenv = ">=20.0.8" [[package]] +name = "prometheus-client" +version = "0.11.0" +description = "Python client for the Prometheus monitoring system." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" + +[package.extras] +twisted = ["twisted"] + +[[package]] name = "psutil" version = "5.8.0" description = "Cross-platform lib for process and system monitoring in Python." @@ -706,7 +728,7 @@ brotli = ["brotli"] [metadata] lock-version = "1.1" python-versions = "3.9.*" -content-hash = "741444c18250124e2d998506b0643fb26240d7481389a13648b02fa1794435e3" +content-hash = "cb345745569d389fcb35f72e34d65c1f9e860f110e521d2f634286bc9127ea2f" [metadata.files] appdirs = [ @@ -819,6 +841,10 @@ django-hosts = [ {file = "django-hosts-4.0.tar.gz", hash = "sha256:59a870d453f113c889a7888bae5408888870350e83e362740f382dad569c2281"}, {file = "django_hosts-4.0-py2.py3-none-any.whl", hash = "sha256:136ac225f34e7f2c007294441a38663ec2bba9637d870ad001def81bca87e390"}, ] +django-prometheus = [ + {file = "django-prometheus-2.1.0.tar.gz", hash = "sha256:dd3f8da1399140fbef5c00d1526a23d1ade286b144281c325f8e409a781643f2"}, + {file = "django_prometheus-2.1.0-py2.py3-none-any.whl", hash = "sha256:c338d6efde1ca336e90c540b5e87afe9287d7bcc82d651a778f302b0be17a933"}, +] django-simple-bulma = [ {file = "django-simple-bulma-2.2.0.tar.gz", hash = "sha256:dfc34839e050d5e4749498806ed7ee8c77794021efa54ab224a2de925c644fea"}, {file = "django_simple_bulma-2.2.0-py3-none-any.whl", hash = "sha256:38530d787b2b6a091b480f7cbb8841a3b3709733ebfa5e543ae339c3d8fece03"}, @@ -935,6 +961,10 @@ pre-commit = [ {file = "pre_commit-2.12.1-py2.py3-none-any.whl", hash = "sha256:70c5ec1f30406250b706eda35e868b87e3e4ba099af8787e3e8b4b01e84f4712"}, {file = "pre_commit-2.12.1.tar.gz", hash = "sha256:900d3c7e1bf4cf0374bb2893c24c23304952181405b4d88c9c40b72bda1bb8a9"}, ] +prometheus-client = [ + {file = "prometheus_client-0.11.0-py2.py3-none-any.whl", hash = "sha256:b014bc76815eb1399da8ce5fc84b7717a3e63652b0c0f8804092c9363acab1b2"}, + {file = "prometheus_client-0.11.0.tar.gz", hash = "sha256:3a8baade6cb80bcfe43297e33e7623f3118d660d41387593758e2fb1ea173a86"}, +] psutil = [ {file = "psutil-5.8.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:0066a82f7b1b37d334e68697faba68e5ad5e858279fd6351c8ca6024e8d6ba64"}, {file = "psutil-5.8.0-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:0ae6f386d8d297177fd288be6e8d1afc05966878704dad9847719650e44fc49c"}, diff --git a/pydis_site/apps/home/urls.py b/pydis_site/apps/home/urls.py index 1e2af8f3..bb77220b 100644 --- a/pydis_site/apps/home/urls.py +++ b/pydis_site/apps/home/urls.py @@ -7,6 +7,7 @@ app_name = 'home' urlpatterns = [ path('', HomeView.as_view(), name='home'), path('', include('pydis_site.apps.redirect.urls')), + path('', include('django_prometheus.urls')), path('admin/', admin.site.urls), path('resources/', include('pydis_site.apps.resources.urls')), path('pages/', include('pydis_site.apps.content.urls')), diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 7df7ad85..6f49763b 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -23,7 +23,8 @@ from pydis_site.constants import GIT_SHA env = environ.Env( DEBUG=(bool, False), - SITE_DSN=(str, "") + SITE_DSN=(str, ""), + BUILDING_DOCKER=(bool, False) ) sentry_sdk.init( @@ -84,10 +85,15 @@ INSTALLED_APPS = [ 'django_filters', 'django_simple_bulma', 'rest_framework', - 'rest_framework.authtoken' + 'rest_framework.authtoken', ] +if not env("BUILDING_DOCKER"): + INSTALLED_APPS.append("django_prometheus") + +# Ensure that Prometheus middlewares are first and last here. MIDDLEWARE = [ + 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'django_hosts.middleware.HostsRequestMiddleware', 'django.middleware.security.SecurityMiddleware', @@ -100,7 +106,9 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django_hosts.middleware.HostsResponseMiddleware', + 'django_prometheus.middleware.PrometheusAfterMiddleware' ] + ROOT_URLCONF = 'pydis_site.urls' TEMPLATES = [ diff --git a/pyproject.toml b/pyproject.toml index 5bbf86ee..c99f1e0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ gunicorn = "~=20.0.4" sentry-sdk = "~=0.19" markdown = "~=3.3.4" python-frontmatter = "~=1.0" +django-prometheus = "~=2.1" [tool.poetry.dev-dependencies] coverage = "~=5.0" |