@@ -256,12 +256,25 @@ def __init__(self, wrappedClass, name, labelnames, **kwargs):
256
256
def labels (self , * labelvalues ):
257
257
'''Return the child for the given labelset.
258
258
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).
265
278
'''
266
279
if len (labelvalues ) == 1 and type (labelvalues [0 ]) == dict :
267
280
if sorted (labelvalues [0 ].keys ()) != sorted (self ._labelnames ):
@@ -347,10 +360,24 @@ class Counter(object):
347
360
348
361
An example for a Counter:
349
362
350
- from prometheus_client import Counter
351
- c = Counter('my_failures_total', 'Description of counter')
352
-
A93C
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
354
381
'''
355
382
_type = 'counter'
356
383
_reserved_labelnames = []
@@ -401,19 +428,38 @@ class Gauge(object):
401
428
'''Gauge metric, to report instantaneous values.
402
429
403
430
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
409
436
410
437
Gauges can go both up and down.
411
438
412
439
from prometheus_client import Gauge
440
+
413
441
g = Gauge('my_inprogress_requests', 'Description of gauge')
414
442
g.inc() # Increment by 1
415
443
g.dec(10) # Decrement by given value
416
444
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))
417
463
'''
418
464
_type = 'gauge'
419
465
_reserved_labelnames = []
@@ -494,8 +540,7 @@ def set_function(self, f):
494
540
'''Call the provided function to return the Gauge value.
495
541
496
542
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.
499
544
'''
500
545
def samples (self ):
501
546
return (('' , {}, float (f ())), )
@@ -515,19 +560,26 @@ class Summary(object):
515
560
516
561
Example for a Summary:
517
562
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)
521
567
522
568
Example for a Summary using time:
523
- from prometheus_client import Summary
524
- REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)')
525
569
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)
530
578
579
+ Example for using the same Summary object as a context manager:
580
+
581
+ with REQUEST_TIME.time():
582
+ pass # Logic to be timed
531
583
'''
532
584
_type = 'summary'
533
585
_reserved_labelnames = ['quantile' ]
@@ -596,22 +648,31 @@ class Histogram(object):
596
648
597
649
Example for a Histogram:
598
650
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
602
652
653
+ h = Histogram('request_size_bytes', 'Request size (bytes)')
654
+ h.observe(512) # Observe 512 (bytes)
603
655
604
656
Example for a Histogram using time:
605
- from prometheus_client import Histogram
606
- REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)')
607
657
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
612
671
613
672
The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
614
673
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.
615
676
'''
616
677
_type = 'histogram'
617
678
_reserved_labelnames = ['histogram' ]
0 commit comments