8000 Add example for initializing metrics with labels by mpitt · Pull Request #589 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Add example for initializing metrics with labels #589

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 4 commits into from
Oct 21, 2020

Conversation

mpitt
Copy link
Contributor
@mpitt mpitt commented Oct 21, 2020

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.

Signed-off-by: Michele Pittoni <michele@pittoni.org>
README.md Outdated
@@ -207,6 +207,11 @@ Taking a counter as an example:
```python
from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
# when using labels, metrics are not initialized at creation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I thought it was a strong enough recommendation to highlight it in the normal use case. Pushed a revision.

mpitt added 2 commits October 21, 2020 15:57
Signed-off-by: Michele Pittoni <michele@pittoni.org>
Signed-off-by: Michele Pittoni <michele@pittoni.org>
README.md Outdated
# initialize the label values
c.labels('get', '/')
c.labels('post', '/submit')
# then use the metrics as normal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need from this bit onwards.

README.md Outdated
@@ -220,6 +220,21 @@ c.labels(method='get', endpoint='/').inc()
c.labels(method='post', endpoint='/submit').inc()
```

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

labels

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

README.md Outdated
```python
from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
# initialize the label values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn't needed.

Signed-off-by: Michele Pittoni <michele@pittoni.org>
@brian-brazil brian-brazil merged commit 02be07a into prometheus:master Oct 21, 2020
@brian-brazil
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0