@@ -494,7 +494,7 @@ implement a proper `describe`, or if that's not practical have `describe`
494494return an empty list.
495495
496496
497- ## Multiprocess Mode (Gunicorn)
497+ ## Multiprocess Mode (E.g. Gunicorn)
498498
499499Prometheus client libraries presume a threaded model, where metrics are shared
500500across 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.
504504This comes with a number of limitations:
505505
506506- 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
507509- Custom collectors do not work (e.g. cpu and memory metrics)
508510- Info and Enum metrics do not work
509511- The pushgateway cannot be used
510512- Gauges cannot use the ` pid ` label
511513
512514There's several steps to getting this working:
513515
514- ** 1. Gunicorn deployment ** :
516+ ** 1. Deployment ** :
515517
516518The ` PROMETHEUS_MULTIPROC_DIR ` environment variable must be set to a directory
517519that 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).
519521
520522This environment variable should be set from a start-up shell script,
521523and not directly from Python (otherwise it may not propagate to child processes).
522524
523525** 2. Metrics collector** :
524526
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.
527533
528534``` python
529535from 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' )
531539
532540# Expose metrics.
533541def app (environ , start_response ):
0 commit comments