@@ -256,12 +256,25 @@ def __init__(self, wrappedClass, name, labelnames, **kwargs):
256256 def labels (self , * labelvalues ):
257257 '''Return the child for the given labelset.
258258
259- Labels can be provided as a tuple or as a dict:
260- c = Counter('c', 'counter', ['l', 'm'])
261- # Set labels by position
262- c.labels('0', '1').inc()
263- # Set labels by name
264- c.labels({'l': '0', 'm': '1'}).inc()
259+ All metrics can have labels, allowing grouping of related time series.
260+ Taking a counter as an example:
261+
262+ from prometheus_client import Counter
263+
264+ c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
265+ c.labels('get', '/').inc()
266+ c.labels('post', '/submit').inc()
267+
268+ Labels can also be provided as a dict:
269+
270+ from prometheus_client import Counter
271+
272+ c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
273+ c.labels({'method': 'get', 'endpoint': '/'}).inc()
274+ c.labels({'method': 'post', 'endpoint': '/submit'}).inc()
275+
276+ See the best practices on [naming](http://prometheus.io/docs/practices/naming/)
277+ and [labels](http://prometheus.io/docs/practices/instrumentation/#use-labels).
265278 '''
266279 if len (labelvalues ) == 1 and type (labelvalues [0 ]) == dict :
267280 if sorted (labelvalues [0 ].keys ()) != sorted (self ._labelnames ):
@@ -347,10 +360,24 @@ class Counter(object):
347360
348361 An example for a Counter:
349362
350- from prometheus_client import Counter
351- c = Counter('my_failures_total', 'Description of counter')
352- c.inc() # Increment by 1
353- c.inc(1.6) # Increment by given value
363+ from prometheus_client import Counter
364+
365+ c = Counter('my_failures_total', 'Description of counter')
366+ c.inc() # Increment by 1
367+ c.inc(1.6) # Increment by given value
368+
369+ There are utilities to count exceptions raised:
370+
371+ @c.count_exceptions()
372+ def f():
373+ pass
374+
375+ with c.count_exceptions():
376+ pass
377+
378+ # Count only one type of exception
379+ with c.count_exceptions(ValueError):
380+ pass
354381 '''
355382 _type = 'counter'
356383 _reserved_labelnames = []
@@ -401,19 +428,38 @@ class Gauge(object):
401428 '''Gauge metric, to report instantaneous values.
402429
403430 Examples of Gauges include:
404- Inprogress requests
405- Number of items in a queue
406- Free memory
407- Total memory
408- Temperature
431+ - Inprogress requests
432+ - Number of items in a queue
433+ - Free memory
434+ - Total memory
435+ - Temperature
409436
410437 Gauges can go both up and down.
411438
412439 from prometheus_client import Gauge
440+
413441 g = Gauge('my_inprogress_requests', 'Description of gauge')
414442 g.inc() # Increment by 1
415443 g.dec(10) # Decrement by given value
416444 g.set(4.2) # Set to a given value
445+
446+ There are utilities for common use cases:
447+
448+ g.set_to_current_time() # Set to current unixtime
449+
450+ # Increment when entered, decrement when exited.
451+ @g.track_inprogress()
452+ def f():
453+ pass
454+
455+ with g.track_inprogress():
456+ pass
457+
458+ A Gauge can also take its value from a callback:
459+
460+ d = Gauge('data_objects', 'Number of objects')
461+ my_dict = {}
462+ d.set_function(lambda: len(my_dict))
417463 '''
418464 _type = 'gauge'
419465 _reserved_labelnames = []
@@ -494,8 +540,7 @@ def set_function(self, f):
494540 '''Call the provided function to return the Gauge value.
495541
496542 The function must return a float, and may be called from
497- multiple threads.
498- All other methods of the Gauge become NOOPs.
543+ multiple threads. All other methods of the Gauge become NOOPs.
499544 '''
500545 def samples (self ):
501546 return (('' , {}, float (f ())), )
@@ -515,19 +560,26 @@ class Summary(object):
515560
516561 Example for a Summary:
517562
518- from prometheus_client import Summary
519- s = Summary('request_size_bytes', 'Request size (bytes)')
520- s.observe(512) # Observe 512 (bytes)
563+ from prometheus_client import Summary
564+
565+ s = Summary('request_size_bytes', 'Request size (bytes)')
566+ s.observe(512) # Observe 512 (bytes)
521567
522568 Example for a Summary using time:
523- from prometheus_client import Summary
524- REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)')
525569
526- @REQUEST_TIME.time()
527- def create_response(request):
528- """A dummy function"""
529- time.sleep(1)
570+ from prometheus_client import Summary
571+
572+ REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)')
573+
574+ @REQUEST_TIME.time()
575+ def create_response(request):
576+ """A dummy function"""
577+ time.sleep(1)
530578
579+ Example for using the same Summary object as a context manager:
580+
581+ with REQUEST_TIME.time():
582+ pass # Logic to be timed
531583 '''
532584 _type = 'summary'
533585 _reserved_labelnames = ['quantile' ]
@@ -596,22 +648,31 @@ class Histogram(object):
596648
597649 Example for a Histogram:
598650
599- from prometheus_client import Histogram
600- h = Histogram('request_size_bytes', 'Request size (bytes)')
601- h.observe(512) # Observe 512 (bytes)
651+ from prometheus_client import Histogram
602652
653+ h = Histogram('request_size_bytes', 'Request size (bytes)')
654+ h.observe(512) # Observe 512 (bytes)
603655
604656 Example for a Histogram using time:
605- from prometheus_client import Histogram
606- REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)')
607657
608- @REQUEST_TIME.time()
609- def create_response(request):
610- """A dummy function"""
611- time.sleep(1)
658+ from prometheus_client import Histogram
659+
660+ REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)')
661+
662+ @REQUEST_TIME.time()
663+ def create_response(request):
664+ """A dummy function"""
665+ time.sleep(1)
666+
667+ Example of using the same Histogram object as a context manager:
668+
669+ with REQUEST_TIME.time():
670+ pass # Logic to be timed
612671
613672 The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
614673 They can be overridden by passing `buckets` keyword argument to `Histogram`.
674+
675+ **NB** The Python client doesn't store or expose quantile information at this time.
615676 '''
616677 _type = 'histogram'
617678 _reserved_labelnames = ['histogram' ]
0 commit comments