8000 Allow passing label values as keyword arguments · sonlinux/client_python@0e5696b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e5696b

Browse files
committed
Allow passing label values as keyword arguments
1 parent 42671e2 commit 0e5696b

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ c.labels('get', '/').inc()
185185
c.labels('post', '/submit').inc()
186186
```
187187

188-
Labels can also be provided as a dict:
188+
Labels can also be passed as keyword-arguments:
189+
190+
```python
191+
from prometheus_client import Counter
192+
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
193+
c.labels(method='get', endpoint='/').inc()
194+
c.labels(method='post', endpoint='/submit').inc()
195+
```
196+
197+
Or as a dict:
189198

190199
```python
191200
from prometheus_client import Counter
@@ -369,7 +378,7 @@ REGISTRY.register(CustomCollector())
369378
## Parser
370379

371380
The Python client supports parsing the Promeheus text format.
372-
This is intended for advanced use cases where you have servers
381+
This is intended for advanced use cases where you have servers
373382
exposing Prometheus metrics and need to get them into some other
374383
system.
375384

@@ -379,4 +388,3 @@ for family in text_string_to_metric_families("my_gauge 1.0\n"):
379388
for sample in family.samples:
380389
print("Name: {0} Labels: {1} Value: {2}".format(*sample))
381390
```
382-

prometheus_client/core.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def __init__(self, wrappedClass, name, labelnames, **kwargs):
253253
if l.startswith('__'):
254254
raise ValueError('Invalid label metric name: ' + l)
255255

256-
def labels(self, *labelvalues):
256+
def labels(self, *labelvalues, **labelkwargs):
257257
'''Return the child for the given labelset.
258258
259259
All metrics can have labels, allowing grouping of related time series.
@@ -276,10 +276,17 @@ def labels(self, *labelvalues):
276276
See the best practices on [naming](http://prometheus.io/docs/practices/naming/)
277277
and [labels](http://prometheus.io/docs/practices/instrumentation/#use-labels).
278278
'''
279+
if labelvalues and labelkwargs:
280+
raise ValueError("Can't pass both *args and **kwargs")
281+
279282
if len(labelvalues) == 1 and type(labelvalues[0]) == dict:
280283
if sorted(labelvalues[0].keys()) != sorted(self._labelnames):
281284
raise ValueError('Incorrect label names')
282285
labelvalues = tuple([unicode(labelvalues[0][l]) for l in self._labelnames])
286+
elif labelkwargs:
287+
if sorted(labelkwargs) != sorted(self._labelnames):
288+
raise ValueError('Incorrect label names')
289+
labelvalues = tuple([unicode(labelkwargs[l]) for l in self._labelnames])
283290
else:
284291
if len(labelvalues) != len(self._labelnames):
285292
raise ValueError('Incorrect label count')

tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ def test_labels_by_dict(self):
280280
self.assertRaises(ValueError, self.two_labels.labels, {'c': 'z'})
281281
self.assertRaises(ValueError, self.two_labels.labels, {})
282282

283+
def test_labels_by_kwarg(self):
284+
self.counter.labels(l='x').inc()
285+
self.assertEqual(1, self.registry.get_sample_value('c', {'l': 'x'}))
286+
self.assertRaises(ValueError, self.counter.labels, l='x', m='y')
287+
self.assertRaises(ValueError, self.counter.labels, m='y')
288+
self.assertRaises(ValueError, self.counter.labels)
289+
self.two_labels.labels(a='x', b='y').inc()
290+
self.assertEqual(1, self.registry.get_sample_value('two', {'a': 'x', 'b': 'y'}))
291+
self.assertRaises(ValueError, self.two_labels.labels, a='x', b='y', c='z')
292+
self.assertRaises(ValueError, self.two_labels.labels, a='x', c='z')
293+
self.assertRaises(ValueError, self.two_labels.labels, b='y', c='z')
294+
self.assertRaises(ValueError, self.two_labels.labels, c='z')
295+
self.assertRaises(ValueError, self.two_labels.labels)
296+
self.assertRaises(ValueError, self.two_labels.labels, {'a': 'x'}, b='y')
297+
283298
def test_invalid_names_raise(self):
284299
self.assertRaises(ValueError, Counter, '', 'help')
285300
self.assertRaises(ValueError, Counter, '^', 'help')

0 commit comments

Comments
 (0)
0