@@ -78,37 +78,46 @@ targets as you have workers, using each port separately.
7878
7979## Exporting /metrics in a WSGI application with multiple processes globally
8080
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:
81+ In some WSGI applications, workers are short lived (less than a minute), so some
82+ are never scraped by prometheus by default. Prometheus client already provides
83+ a nice system to aggregate them using the env variable: ` prometheus_multiproc_dir `
84+ which will configure the directory where metrics will be stored as files per process.
8785
86+ Configuration in uwsgi would look like:
8887```
8988env = prometheus_multiproc_dir=/path/to/django_metrics
9089```
90+ You can also set this environment variable elsewhere such as in a kubernetes manifest.
91+ Note that the environment variable is lower_case.
9192
92- By default this will create four files (one for counters, one for summaries, ...etc)
93+ Setting this will create four files (one for counters, one for summaries, ...etc)
9394for each pid used. In uwsgi, the number of different pids used can be quite large
9495(the pid change every time a worker respawn). To prevent having thousand of files
9596created, it's possible to create file using worker ids rather than pids.
9697
97- It should be in settings, before any metric are created:
98-
98+ You can change the function used for identifying the process to use the uwsgi worker_id.
99+ Modify this in settings before any metrics are created:
99100``` 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)
101+ try :
102+ import prometheus_client
103+ import uwsgi
104+ prometheus_client.values.ValueClass = prometheus_client.values.MultiProcessValue(
105+ _pidFunc = uwsgi.worker_id)
106+ except ImportError :
107+ pass # not running in uwsgi
104108```
109+ Note that this code uses internal interfaces of prometheus_client.
110+ The underlying implementation may change.
105111
106112The number of resulting files will be:
107113number of processes * 4 (counter, histogram, gauge, summary)
108114
109115Be aware that by default this will generate a large amount of file descriptors:
110116Each worker will keep 3 file descriptors for each files it created.
111117
118+ Since these files will be written often, you should consider mounting this directory
119+ as a ` tmpfs ` or using a subdir of an existing one such as ` /run/ ` or ` /var/run/ ` .
120+
112121If uwsgi is not using lazy-apps (lazy-apps = true), there will be a
113122file descriptors leak (tens to hundreds of fds on a single file) due
114123to the way uwsgi forks processes to create workers.
0 commit comments