@@ -49,6 +49,8 @@ class Timestamp(object):
49
49
def __init__ (self , sec , nsec ):
50
50
if nsec < 0 or nsec >= 1e9 :
51
51
raise ValueError ("Invalid value for nanoseconds in Timestamp: {}" .format (nsec ))
52
+ if sec < 0 :
53
+ nsec = - nsec
52
54
self .sec = int (sec )
53
55
self .nsec = int (nsec )
54
56
@@ -64,6 +66,12 @@ def __float__(self):
64
66
def __eq__ (self , other ):
65
67
return type (self ) == type (other ) and self .sec == other .sec and self .nsec == other .nsec
66
68
69
+ def __ne__ (self , other ):
70
+ return not self == other
71
+
72
+ def __gt__ (self , other ):
73
+ return self .sec > other .sec or self .nsec > other .nsec
74
+
67
75
68
76
Exemplar = namedtuple ('Exemplar' , ['labels' , 'value' , 'timestamp' ])
69
77
Exemplar .__new__ .__defaults__ = (None , )
@@ -122,6 +130,7 @@ def _get_names(self, collector):
122
130
'counter' : ['_total' , '_created' ],
123
131
'summary' : ['' , '_sum' , '_count' , '_created' ],
124
132
'histogram' : ['_bucket' , '_sum' , '_count' , '_created' ],
133
+ 'gaugehistogram' : ['_bucket' , '_gsum' , '_gcount' ],
125
134
'info' : ['_info' ],
126
135
}
127
136
for metric in desc_func ():
@@ -391,29 +400,33 @@ class GaugeHistogramMetricFamily(Metric):
391
400
392
401
For use by custom collectors.
393
402
'''
394
- def __init__ (self , name , documentation , buckets = None , labels = None , unit = '' ):
403
+ def __init__ (self , name , documentation , buckets = None , gsum_value = None , labels = None , unit = '' ):
395
404
Metric .__init__ (self , name , documentation , 'gaugehistogram' , unit )
396
405
if labels is not None and buckets is not None :
397
406
raise ValueError ('Can only specify at most one of buckets and labels.' )
398
407
if labels is None :
399
408
labels = []
400
409
self ._labelnames = tuple (labels )
401
410
if buckets is not None :
402
- self .add_metric ([], buckets )
411
+ self .add_metric ([], buckets , gsum_value )
403
412
404
- def add_metric (self , labels , buckets , timestamp = None ):
413
+ def add_metric (self , labels , buckets , gsum_value , timestamp = None ):
405
414
'''Add a metric to the metric family.
406
415
407
416
Args:
408
417
labels: A list of label values
409
418
buckets: A list of pairs of bucket names and values.
410
419
The buckets must be sorted, and +Inf present.
420
+ gsum_value: The sum value of the metric.
411
421
'''
412
422
for bucket , value in buckets :
413
423
self .samples .append (Sample (
414
424
self .name + '_bucket' ,
415
425
dict (list (zip (self ._labelnames , labels )) + [('le' , bucket )]),
416
426
value , timestamp ))
427
+ # +Inf is last and provides the count value.
428
+ self .samples .append (Sample (self .name + '_gcount' , dict (zip (self ._labelnames , labels )), buckets [- 1 ][1 ], timestamp ))
429
+ self .samples .append (Sample (self .name + '_gsum' , dict (zip (self ._labelnames , labels )), gsum_value , timestamp ))
417
430
418
431
419
432
class InfoMetricFamily (Metric ):
@@ -465,7 +478,7 @@ def add_metric(self, labels, value, timestamp=None):
465
478
value: A dict of string state names to booleans
466
479
'''
467
480
labels = tuple (labels )
468
- for state , enabled in value .items ():
481
+ for state , enabled in sorted ( value .items () ):
469
482
v = (1 if enabled else 0 )
470
483
self .samples .append (Sample (self .name ,
471
484
dict (zip (self ._labelnames + (self .name ,), labels + (state ,))), v , timestamp ))
0 commit comments