@@ -56,7 +56,7 @@ option is always active:
56
56
execute_from_command_line(sys.argv + [' --noreload' ])
57
57
```
58
58
59
- ## Exporting /metrics in a WSGI application with multiple processes
59
+ ## Exporting /metrics in a WSGI application with multiple processes per process
60
60
61
61
If you're using WSGI (e.g. with uwsgi or with gunicorn) and multiple
62
62
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
75
75
76
76
You can then configure Prometheus to collect metrics on as many
77
77
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