Skip to content

Commit 33454a0

Browse files
geobeaukorfuri
authored andcommitted
Update documentation (#76)
Provide some information for running a big django app with django prometheus using uwsgi
1 parent 71a0021 commit 33454a0

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,46 @@ pull requests are welcome. Make sure to read the Prometheus best
144144
practices on
145145
[instrumentation](http://prometheus.io/docs/practices/instrumentation/)
146146
and [naming](http://prometheus.io/docs/practices/naming/).
147+
148+
## Importing Django Prometheus using only local settings
149+
150+
If you wish to use Django Prometheus but are not able to change
151+
the code base, it's possible to have all the default metrics by
152+
modifying only the settings.
153+
154+
First step is to inject prometheus' middlewares and to add
155+
django_prometheus in INSTALLED_APPS
156+
157+
```python
158+
MIDDLEWARE = (
159+
('django_prometheus.middleware.PrometheusBeforeMiddleware',) +
160+
MIDDLEWARE +
161+
('django_prometheus.middleware.PrometheusAfterMiddleware',)
162+
)
163+
164+
INSTALLED_APPS = INSTALLED_APPS + ('django_prometheus',)
165+
```
166+
167+
Second step is to create the /metrics end point, for that we need
168+
another file (called urls_prometheus_wrapper.py in this example) that
169+
will wraps the apps URLs and add one on top:
170+
171+
172+
```python
173+
from django.conf.urls import include, url
174+
175+
176+
urlpatterns = []
177+
178+
urlpatterns.append(url('^prometheus/', include('django_prometheus.urls')))
179+
urlpatterns.append(url('', include('myapp.urls')))
180+
```
181+
182+
This file will add a "/prometheus/metrics" end point to the URLs of django
183+
that will export the metrics (replace myapp by your project name).
184+
185+
Then we inject the wrapper in settings:
186+
187+
```python
188+
ROOT_URLCONF = "graphite.urls_prometheus_wrapper"
189+
```

documentation/exports.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ option is always active:
5656
execute_from_command_line(sys.argv + ['--noreload'])
5757
```
5858

59-
## Exporting /metrics in a WSGI application with multiple processes
59+
## Exporting /metrics in a WSGI application with multiple processes per process
6060

6161
If you're using WSGI (e.g. with uwsgi or with gunicorn) and multiple
6262
Django processes, using either option above won't work, as requests
@@ -75,3 +75,40 @@ This will make Django-Prometheus try to export /metrics on port
7575

7676
You can then configure Prometheus to collect metrics on as many
7777
targets as you have workers, using each port separately.
78+
79+
## Exporting /metrics in a WSGI application with multiple processes globally
80+
81+
In some WSGI application, worker are short lived (less than a minute) so some
82+
are never scrapped by prometheus by default. Prometheus client already provide
83+
a nice system to aggregate them using the env variable: ```prometheus_multiproc_dir```
84+
that will contain the directory where metrics will be stored.
85+
86+
Configuration in uwsgi will look like:
87+
88+
```
89+
env = prometheus_multiproc_dir=/path/to/django_metrics
90+
```
91+
92+
By default this will create four files (one for counters, one for summaries, ...etc)
93+
for each pid used. In uwsgi, the number of different pids used can be quite large
94+
(the pid change every time a worker respawn). To prevent having thousand of files
95+
created, it's possible to create file using worker ids rather than pids.
96+
97+
It should be in settings, before any metric are created:
98+
99+
```python
100+
from prometheus_client import core
101+
import uwsgi
102+
# Use uwsgi's worker_id rather than system pids
103+
core._ValueClass = core._MultiProcessValue(_pidFunc=uwsgi.worker_id)
104+
```
105+
106+
The number of resulting files will be:
107+
number of processes * 4 (counter, histogram, gauge, summary)
108+
109+
Be aware that by default this will generate a large amount of file descriptors:
110+
Each worker will keep 3 file descriptors for each files it created.
111+
112+
If uwsgi is not using lazy-apps (lazy-apps = true), there will be a
113+
file descriptors leak (tens to hundreds of fds on a single file) due
114+
to the way uwsgi forks processes to create workers.

0 commit comments

Comments
 (0)