Debian Codegraph @

docs/content/multiprocess/_index.md

raw symbols SCIP
1 ---
2 title: Multiprocess Mode
3 weight: 5
4 ---
5
6 Prometheus client libraries presume a threaded model, where metrics are shared
7 across workers. This doesn't work so well for languages such as Python where
8 it's common to have processes rather than threads to handle large workloads.
9
10 To handle this the client library can be put in multiprocess mode.
11 This comes with a number of limitations:
12
13 - Registries can not be used as normal, all instantiated metrics are exported
14 - Registering metrics to a registry later used by a `MultiProcessCollector`
15 may cause duplicate metrics to be exported
16 - Custom collectors do not work (e.g. cpu and memory metrics)
17 - Info and Enum metrics do not work
18 - The pushgateway cannot be used
19 - Gauges cannot use the `pid` label
20 - Exemplars are not supported
21 - Remove and Clear of labels are currently not supported in multiprocess mode.
22
23 There's several steps to getting this working:
24
25 **1. Deployment**:
26
27 The `PROMETHEUS_MULTIPROC_DIR` environment variable must be set to a directory
28 that the client library can use for metrics. This directory must be wiped
29 between process/Gunicorn runs (before startup is recommended).
30
31 This environment variable should be set from a start-up shell script,
32 and not directly from Python (otherwise it may not propagate to child processes).
33
34 **2. Metrics collector**:
35
36 The application must initialize a new `CollectorRegistry`, and store the
37 multi-process collector inside. It is a best practice to create this registry
38 inside the context of a request to avoid metrics registering themselves to a
39 collector used by a `MultiProcessCollector`. If a registry with metrics
40 registered is used by a `MultiProcessCollector` duplicate metrics may be
41 exported, one for multiprocess, and one for the process serving the request.
42
43 ```python
44 from prometheus_client import multiprocess
45 from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST, Counter
46
47 MY_COUNTER = Counter('my_counter', 'Description of my counter')
48
49 # Expose metrics.
50 def app(environ, start_response):
51 registry = CollectorRegistry()
52 multiprocess.MultiProcessCollector(registry)
53 data = generate_latest(registry)
54 status = '200 OK'
55 response_headers = [
56 ('Content-type', CONTENT_TYPE_LATEST),
57 ('Content-Length', str(len(data)))
58 ]
59 start_response(status, response_headers)
60 return iter([data])
61 ```
62
63 **3. Gunicorn configuration**:
64
65 The `gunicorn` configuration file needs to include the following function:
66
67 ```python
68 from prometheus_client import multiprocess
69
70 def child_exit(server, worker):
71 multiprocess.mark_process_dead(worker.pid)
72 ```
73
74 **4. Metrics tuning (Gauge)**:
75
76 When `Gauge`s are used in multiprocess applications,
77 you must decide how to handle the metrics reported by each process.
78 Gauges have several modes they can run in, which can be selected with the `multiprocess_mode` parameter.
79
80 - 'all': Default. Return a timeseries per process (alive or dead), labelled by the process's `pid` (the label is added internally).
81 - 'min': Return a single timeseries that is the minimum of the values of all processes (alive or dead).
82 - 'max': Return a single timeseries that is the maximum of the values of all processes (alive or dead).
83 - 'sum': Return a single timeseries that is the sum of the values of all processes (alive or dead).
84 - 'mostrecent': Return a single timeseries that is the most recent value among all processes (alive or dead).
85
86 Prepend 'live' to the beginning of the mode to return the same result but only considering living processes
87 (e.g., 'liveall, 'livesum', 'livemax', 'livemin', 'livemostrecent').
88
89 ```python
90 from prometheus_client import Gauge
91
92 # Example gauge
93 IN_PROGRESS = Gauge("inprogress_requests", "help", multiprocess_mode='livesum')
94 ```