@@ -49,6 +49,8 @@ class Timestamp(object):
4949 def __init__ (self , sec , nsec ):
5050 if nsec < 0 or nsec >= 1e9 :
5151 raise ValueError ("Invalid value for nanoseconds in Timestamp: {}" .format (nsec ))
52+ if sec < 0 :
53+ nsec = - nsec
5254 self .sec = int (sec )
5355 self .nsec = int (nsec )
5456
@@ -64,6 +66,12 @@ def __float__(self):
6466 def __eq__ (self , other ):
6567 return type (self ) == type (other ) and self .sec == other .sec and self .nsec == other .nsec
6668
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+
6775
6876Exemplar = namedtuple ('Exemplar' , ['labels' , 'value' , 'timestamp' ])
6977Exemplar .__new__ .__defaults__ = (None , )
@@ -122,6 +130,7 @@ def _get_names(self, collector):
122130 'counter' : ['_total' , '_created' ],
123131 'summary' : ['' , '_sum' , '_count' , '_created' ],
124132 'histogram' : ['_bucket' , '_sum' , '_count' , '_created' ],
133+ 'gaugehistogram' : ['_bucket' , '_gsum' , '_gcount' ],
125134 'info' : ['_info' ],
126135 }
127136 for metric in desc_func ():
@@ -391,29 +400,33 @@ class GaugeHistogramMetricFamily(Metric):
391400
392401 For use by custom collectors.
393402 '''
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 = '' ):
395404 Metric .__init__ (self , name , documentation , 'gaugehistogram' , unit )
396405 if labels is not None and buckets is not None :
397406 raise ValueError ('Can only specify at most one of buckets and labels.' )
398407 if labels is None :
399408 labels = []
400409 self ._labelnames = tuple (labels )
401410 if buckets is not None :
402- self .add_metric ([], buckets )
411+ self .add_metric ([], buckets , gsum_value )
403412
404- def add_metric (self , labels , buckets , timestamp = None ):
413+ def add_metric (self , labels , buckets , gsum_value , timestamp = None ):
405414 '''Add a metric to the metric family.
406415
407416 Args:
408417 labels: A list of label values
409418 buckets: A list of pairs of bucket names and values.
410419 The buckets must be sorted, and +Inf present.
420+ gsum_value: The sum value of the metric.
411421 '''
412422 for bucket , value in buckets :
413423 self .samples .append (Sample (
414424 self .name + '_bucket' ,
415425 dict (list (zip (self ._labelnames , labels )) + [('le' , bucket )]),
416426 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 ))
417430
418431
419432class InfoMetricFamily (Metric ):
@@ -465,7 +478,7 @@ def add_metric(self, labels, value, timestamp=None):
465478 value: A dict of string state names to booleans
466479 '''
467480 labels = tuple (labels )
468- for state , enabled in value .items ():
481+ for state , enabled in sorted ( value .items () ):
469482 v = (1 if enabled else 0 )
470483 self .samples .append (Sample (self .name ,
471484 dict (zip (self ._labelnames + (self .name ,), labels + (state ,))), v , timestamp ))
0 commit comments