Skip to content

Commit 0497442

Browse files
authored
Improve multiprocessing documentation (#524)
* Improve multiproc documentation Signed-off-by: Sinclert Pérez <[email protected]>
1 parent 2d85f4a commit 0497442

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

README.md

+29-18
Original file line numberDiff line numberDiff line change
@@ -489,31 +489,25 @@ This comes with a number of limitations:
489489

490490
There's several steps to getting this working:
491491

492-
**One**: Gunicorn deployment
492+
**1. Gunicorn deployment**:
493493

494494
The `prometheus_multiproc_dir` environment variable must be set to a directory
495495
that the client library can use for metrics. This directory must be wiped
496496
between Gunicorn runs (before startup is recommended).
497497

498-
Put the following in the config file:
499-
```python
500-
from prometheus_client import multiprocess
498+
This environment variable should be set from a start-up shell script,
499+
and not directly from Python (otherwise it may not propagate to child processes).
501500

502-
def child_exit(server, worker):
503-
multiprocess.mark_process_dead(worker.pid)
504-
```
501+
**2. Metrics collector**:
502+
503+
The application must initialize a new `CollectorRegistry`,
504+
and store the multi-process collector inside.
505505

506-
**Two**: Inside the application
507506
```python
508507
from prometheus_client import multiprocess
509-
from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST, Gauge
510-
511-
# Example gauge.
512-
IN_PROGRESS = Gauge("inprogress_requests", "help", multiprocess_mode='livesum')
513-
508+
from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST
514509

515510
# Expose metrics.
516-
@IN_PROGRESS.track_inprogress()
517511
def app(environ, start_response):
518512
registry = CollectorRegistry()
519513
multiprocess.MultiProcessCollector(registry)
@@ -527,19 +521,36 @@ def app(environ, start_response):
527521
return iter([data])
528522
```
529523

530-
**Three**: Instrumentation
524+
**3. Gunicorn configuration**:
525+
526+
The `gunicorn` configuration file needs to include the following function:
527+
528+
```python
529+
from prometheus_client import multiprocess
530+
531+
def child_exit(server, worker):
532+
multiprocess.mark_process_dead(worker.pid)
533+
```
531534

532-
Counters, Summarys and Histograms work as normal.
535+
**4. Metrics tuning (Gauge)**:
533536

534-
Gauges have several modes they can run in, which can be selected with the
535-
`multiprocess_mode` parameter.
537+
When `Gauge` metrics are used, additional tuning needs to be performed.
538+
Gauges have several modes they can run in, which can be selected with the `multiprocess_mode` parameter.
536539

537540
- 'all': Default. Return a timeseries per process alive or dead.
538541
- 'liveall': Return a timeseries per process that is still alive.
539542
- 'livesum': Return a single timeseries that is the sum of the values of alive processes.
540543
- 'max': Return a single timeseries that is the maximum of the values of all processes, alive or dead.
541544
- 'min': Return a single timeseries that is the minimum of the values of all processes, alive or dead.
542545

546+
```python
547+
from prometheus_client import Gauge
548+
549+
# Example gauge
550+
IN_PROGRESS = Gauge("inprogress_requests", "help", multiprocess_mode='livesum')
551+
```
552+
553+
543554
## Parser
544555

545556
The Python client supports parsing the Prometheus text format.

0 commit comments

Comments
 (0)