Labels
All metrics can have labels, allowing grouping of related time series.
See the best practices on naming and labels.
Taking a counter as an example:
from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels('get', '/').inc()
c.labels('post', '/submit').inc()
Labels can also be passed as keyword-arguments:
from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
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 .labels() method alone:
from prometheus_client import Counter
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels('get', '/')
c.labels('post', '/submit')
Remove a specific labelset from the metric. Values must be passed in the same
order as labelnames were declared.
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels('get', '/').inc()
c.remove('get', '/')
Remove all labelsets that partially match the given dict of label names and values.
c.remove_by_labels({'method': 'get'}) # removes all labelsets where method='get'
Remove all labelsets from the metric at once.
c.clear()