Skip to content

Commit 6df81ae

Browse files
authored
Merge pull request #651 from prometheus/clarify-multiprocess-registry-use
Guidance to avoid duplicate metrics in multiprocess
2 parents fea6964 + 6d572df commit 6df81ae

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ implement a proper `describe`, or if that's not practical have `describe`
494494
return an empty list.
495495

496496

497-
## Multiprocess Mode (Gunicorn)
497+
## Multiprocess Mode (E.g. Gunicorn)
498498

499499
Prometheus client libraries presume a threaded model, where metrics are shared
500500
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.
504504
This 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

512514
There's several steps to getting this working:
513515

514-
**1. Gunicorn deployment**:
516+
**1. Deployment**:
515517

516518
The `PROMETHEUS_MULTIPROC_DIR` environment variable must be set to a directory
517519
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).
519521

520522
This environment variable should be set from a start-up shell script,
521523
and 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
529535
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')
531539

532540
# Expose metrics.
533541
def app(environ, start_response):

0 commit comments

Comments
 (0)