Add example for initializing metrics with labels - #589
Merged
Merged
Conversation
Signed-off-by: Michele Pittoni <michele@pittoni.org>
| ```python | ||
| from prometheus_client import Counter | ||
| c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) | ||
| # when using labels, metrics are not initialized at creation |
Contributor
There was a problem hiding this comment.
This doesn't really fit here, as a less common use case is being wedged in front of the simple normal usage.
This would be better as a separate thing down the send of this section.
Contributor
Author
There was a problem hiding this comment.
Fair enough, I thought it was a strong enough recommendation to highlight it in the normal use case. Pushed a revision.
Signed-off-by: Michele Pittoni <michele@pittoni.org>
| # initialize the label values | ||
| c.labels('get', '/') | ||
| c.labels('post', '/submit') | ||
| # then use the metrics as normal |
Contributor
There was a problem hiding this comment.
You don't need from this bit onwards.
|
|
||
| Metrics with labels are not initialized when declared, because the client can't | ||
| know what values the label can have. It is recommended to initialize the label | ||
| values by calling the `.label()` method alone: |
| ```python | ||
| from prometheus_client import Counter | ||
| c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) | ||
| # initialize the label values |
Contributor
There was a problem hiding this comment.
This comment isn't needed.
Signed-off-by: Michele Pittoni <michele@pittoni.org>
Contributor
|
Thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I know metrics should be initialized, but I wasn't able to find how to do it if they have labels in this doc. I eventually found the answer in Brian's blog, which is always an amazing source.