@@ -78,37 +78,46 @@ targets as you have workers, using each port separately.
78
78
79
79
## Exporting /metrics in a WSGI application with multiple processes globally
80
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:
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.
87
85
86
+ Configuration in uwsgi would look like:
88
87
```
89
88
env = prometheus_multiproc_dir=/path/to/django_metrics
90
89
```
90
+ You can also set this environment variable elsewhere such as in a kubernetes manifest.
91
+ Note that the environment variable is lower_case.
91
92
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)
93
94
for each pid used. In uwsgi, the number of different pids used can be quite large
94
95
(the pid change every time a worker respawn). To prevent having thousand of files
95
96
created, it's possible to create file using worker ids rather than pids.
96
97
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:
99
100
``` 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
104
108
```
109
+ Note that this code uses internal interfaces of prometheus_client.
110
+ The underlying implementation may change.
105
111
106
112
The number of resulting files will be:
107
113
number of processes * 4 (counter, histogram, gauge, summary)
108
114
109
115
Be aware that by default this will generate a large amount of file descriptors:
110
116
Each worker will keep 3 file descriptors for each files it created.
111
117
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
+
112
121
If uwsgi is not using lazy-apps (lazy-apps = true), there will be a
113
122
file descriptors leak (tens to hundreds of fds on a single file) due
114
123
to the way uwsgi forks processes to create workers.
0 commit comments