@@ -494,7 +494,7 @@ implement a proper `describe`, or if that's not practical have `describe`
494
494
return an empty list.
495
495
496
496
497
- ## Multiprocess Mode (Gunicorn)
497
+ ## Multiprocess Mode (E.g. Gunicorn)
498
498
499
499
Prometheus client libraries presume a threaded model, where metrics are shared
500
500
across workers. This doesn't work so well for languages such as Python where
@@ -504,30 +504,38 @@ To handle this the client library can be put in multiprocess mode.
504
504
This comes with a number of limitations:
505
505
506
506
- Registries can not be used as normal, all instantiated metrics are exported
507
+ - Registering metrics to a registry later used by a ` MultiProcessCollector `
508
+ may cause duplicate metrics to be exported
507
509
- Custom collectors do not work (e.g. cpu and memory metrics)
508
510
- Info and Enum metrics do not work
509
511
- The pushgateway cannot be used
510
512
- Gauges cannot use the ` pid ` label
511
513
512
514
There's several steps to getting this working:
513
515
514
- ** 1. Gunicorn deployment ** :
516
+ ** 1. Deployment ** :
515
517
516
518
The ` PROMETHEUS_MULTIPROC_DIR ` environment variable must be set to a directory
517
519
that the client library can use for metrics. This directory must be wiped
518
- between Gunicorn runs (before startup is recommended).
520
+ between process/ Gunicorn runs (before startup is recommended).
519
521
520
522
This environment variable should be set from a start-up shell script,
521
523
and not directly from Python (otherwise it may not propagate to child processes).
522
524
523
525
** 2. Metrics collector** :
524
526
525
- The application must initialize a new ` CollectorRegistry ` ,
526
- and store the multi-process collector inside.
527
+ The application must initialize a new ` CollectorRegistry ` , and store the
528
+ multi-process collector inside. It is a best practice to create this registry
529
+ inside the context of a request to avoid metrics registering themselves to a
530
+ collector used by a ` MultiProcessCollector ` . If a registry with metrics
531
+ registered is used by a ` MultiProcessCollector ` duplicate metrics may be
532
+ exported, one for multiprocess, and one for the process serving the request.
527
533
528
534
``` python
529
535
from prometheus_client import multiprocess
530
- from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST
536
+ from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST , Counter
537
+
538
+ MY_COUNTER = Counter(' my_counter' , ' Description of my counter' )
531
539
532
540
# Expose metrics.
533
541
def app (environ , start_response ):
0 commit comments