Skip to content

Commit

Permalink
add django-prometheus integration
Browse files Browse the repository at this point in the history
  • Loading branch information
awieckowski committed Dec 9, 2024
1 parent 74f660e commit 1e3ff8d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
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
11 changes: 11 additions & 0 deletions src/ralph/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/'

Expand Down Expand Up @@ -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
Expand All @@ -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 = [
Expand Down
9 changes: 9 additions & 0 deletions src/ralph/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
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",
)
]

0 comments on commit 1e3ff8d

Please sign in to comment.