-
Notifications
You must be signed in to change notification settings - Fork 807
Guidance to avoid duplicate metrics in multiprocess #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -494,7 +494,7 @@ implement a proper `describe`, or if that's not practical have `describe` | |
return an empty list. | ||
|
||
|
||
## Multiprocess Mode (Gunicorn) | ||
## Multiprocess Mode (E.g. Gunicorn) | ||
|
||
Prometheus client libraries presume a threaded model, where metrics are shared | ||
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. | |
This comes with a number of limitations: | ||
|
||
- Registries can not be used as normal, all instantiated metrics are exported | ||
- Registering metrics to a registry later used by a `MultiProcessCollector` | ||
may cause duplicate metrics to be exported | ||
- Custom collectors do not work (e.g. cpu and memory metrics) | ||
- Info and Enum metrics do not work | ||
- The pushgateway cannot be used | ||
- Gauges cannot use the `pid` label | ||
|
||
There's several steps to getting this working: | ||
|
||
**1. Gunicorn deployment**: | ||
**1. Deployment**: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
The `prometheus_multiproc_dir` environment variable must be set to a directory | ||
that the client library can use for metrics. This directory must be wiped | ||
between Gunicorn runs (before startup is recommended). | ||
between process/Gunicorn runs (before startup is recommended). | ||
|
||
This environment variable should be set from a start-up shell script, | ||
and not directly from Python (otherwise it may not propagate to child processes). | ||
|
||
**2. Metrics collector**: | ||
|
||
The application must initialize a new `CollectorRegistry`, | ||
and store the multi-process collector inside. | ||
The application must initialize a new `CollectorRegistry`, and store the | ||
multi-process collector inside. It is a best practice to create this registry | ||
inside the context of a request to avoid metrics registering themselves to a | ||
collector used by a `MultiProcessCollector`. If a registry with metrics | ||
registered is used by a `MultiProcessCollector` duplicate metrics may be | ||
exported, one for multiprocess, and one for the process serving the request. | ||
csmarchbanks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```python | ||
from prometheus_client import multiprocess | ||
from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST | ||
from prometheus_client import generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST, Counter | ||
|
||
MY_COUNTER = Counter('my_counter', 'Description of my counter') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
# Expose metrics. | ||
def app(environ, start_response): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is under a section called
(Gunicorn)
however this seems more generic and not tied to Gunicorn, right? I hit this issue with UWSGI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this also begs the question of, what is the case someone does want to register a metric to a registry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Gunicorn specification is a good point, perhaps
(E.g. Gunicorn)
instead to make it clear that is an example?There are lots of valid use cases to register a metric when in non-multiprocess mode, it just doesn't work for multi-process right now. For example, when building a custom exporter you might have one registry for internal metrics, and a second registry for the metrics coming from something being probed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roger that