Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus integration #3876

Merged
merged 9 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contrib/common/gunicorn/gunicorn.ini
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
6 changes: 6 additions & 0 deletions docker/Dockerfile-prod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/ralph/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}


if bool_from_env('COLLECT_METRICS'):
COLLECT_METRICS = True
STATSD_HOST = os.environ.get('STATSD_HOST')
Expand All @@ -86,3 +85,16 @@
STATSD_GRAPHS_PREFIX = os.environ.get(
'STATSD_GRAPHS_PREFIX', 'ralph.graphs'
)

if bool_from_env('PROMETHEUS_METRICS_ENABLED', True):
PROMETHEUS_METRICS_ENABLED = True
PROMETHEUS_EXPORT_MIGRATIONS = False
MIDDLEWARE = (
'django_prometheus.middleware.PrometheusBeforeMiddleware',
) + MIDDLEWARE
MIDDLEWARE = MIDDLEWARE + (
'django_prometheus.middleware.PrometheusAfterMiddleware',
)
INSTALLED_APPS += (
'django_prometheus',
)
2 changes: 2 additions & 0 deletions src/ralph/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ def __getitem__(self, item):

ENABLE_HERMES_INTEGRATION = True
HERMES['ENABLED'] = ENABLE_HERMES_INTEGRATION

PROMETHEUS_METRICS_ENABLED = False
11 changes: 11 additions & 0 deletions src/ralph/urls/base.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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",
)
]
Loading