(self, _m, messages, obj_map, obj_help)
return _obj
Extracts MetricFamily objects from the maps generated by parsing the
        strings in _extract_metrics_from_string
Extracts MetricFamily objects from the maps generated by parsing the
        strings in _extract_metrics_from_string
Extracts MetricFamily objects from the maps generated by parsing the strings in _extract_metrics_from_string
def _extract_metric_from_map(self, _m, messages, obj_map, obj_help):
        """
        Extracts MetricFamily objects from the maps generated by parsing the
        strings in _extract_metrics_from_string
        """
        _obj = metrics_pb2.MetricFamily()
        _obj.name = _m
        _obj.type = self.METRIC_TYPES.index(obj_map[_m])
        if _m in obj_help:
            _obj.help = obj_help[_m]
        # trick for histograms
        _newlbl = _m
        if obj_map[_m] == 'histogram':
            _newlbl = '{}_bucket'.format(_m)
        # Loop through the array of metrics ({labels, value}) built earlier
        for _metric in messages[_newlbl]:
            # in the case of quantiles and buckets, they need to be grouped by labels
            if obj_map[_m] in ['summary', 'histogram'] and len(_obj.metric) > 0:
                _label_exists = False
                _metric_minus = {k: v for k, v in list(iteritems(_metric['labels'])) if k not in ['quantile', 'le']}
                _metric_idx = 0
                for mls in _obj.metric:
                    _tmp_lbl = {idx.name: idx.value for idx in mls.label}
                    if _metric_minus == _tmp_lbl:
                        _label_exists = True
                        break
                    _metric_idx = _metric_idx + 1
                if _label_exists:
                    _g = _obj.metric[_metric_idx]
                else:
                    _g = _obj.metric.add()
            else:
                _g = _obj.metric.add()
            if obj_map[_m] == 'counter':
                _g.counter.value = float(_metric['value'])
            elif obj_map[_m] == 'gauge':
                _g.gauge.value = float(_metric['value'])
            elif obj_map[_m] == 'summary':
                if '{}_count'.format(_m) in messages:
                    _g.summary.sample_count = long(self.get_metric_value_by_labels(messages, _metric, _m, 'count'))
                if '{}_sum'.format(_m) in messages:
                    _g.summary.sample_sum = self.get_metric_value_by_labels(messages, _metric, _m, 'sum')
            # TODO: see what can be done with the untyped metrics
            elif obj_map[_m] == 'histogram':
                if '{}_count'.format(_m) in messages:
                    _g.histogram.sample_count = long(self.get_metric_value_by_labels(messages, _metric, _m, 'count'))
                if '{}_sum'.format(_m) in messages:
                    _g.histogram.sample_sum = self.get_metric_value_by_labels(messages, _metric, _m, 'sum')
            # last_metric = len(_obj.metric) - 1
            # if last_metric >= 0:
            for lbl in _metric['labels']:
                # In the string format, the quantiles are in the labels
                if lbl == 'quantile':
                    # _q = _obj.metric[last_metric].summary.quantile.add()
                    _q = _g.summary.quantile.add()
                    _q.quantile = float(_metric['labels'][lbl])
                    _q.value = float(_metric['value'])
                # The upper_bounds are stored as "le" labels on string format
                elif obj_map[_m] == 'histogram' and lbl == 'le':
                    # _q = _obj.metric[last_metric].histogram.bucket.add()
                    _q = _g.histogram.bucket.add()
                    _q.upper_bound = float(_metric['labels'][lbl])
                    _q.cumulative_count = long(float(_metric['value']))
                else:
                    # labels deduplication
                    is_in_labels = False
                    for _existing_lbl in _g.label:
                        if lbl == _existing_lbl.name:
                            is_in_labels = True
                    if not is_in_labels:
                        _l = _g.label.add()
                        _l.name = lbl
                        _l.value = _metric['labels'][lbl]
        return _obj
def _extract_metric_from_map ( self , _m , messages , obj_map , obj_help ) : _obj = metrics_pb2 . MetricFamily ( ) _obj . name = _m _obj . type = self . METRIC_TYPES . index ( obj_map [ _m ] ) if _m in obj_help : _obj . help = obj_help [ _m ] # trick for histograms _newlbl = _m if obj_map [ _m ] == 'histogram' : _newlbl = '{}_bucket' . format ( _m ) # Loop through the array of metrics ({labels, value}) built earlier for _metric in messages [ _newlbl ] : # in the case of quantiles and buckets, they need to be grouped by labels if obj_map [ _m ] in [ 'summary' , 'histogram' ] and len ( _obj . metric ) > 0 : _label_exists = False _metric_minus = { k : v for k , v in list ( iteritems ( _metric [ 'labels' ] ) ) if k not in [ 'quantile' , 'le' ] } _metric_idx = 0 for mls in _obj . metric : _tmp_lbl = { idx . name : idx . value for idx in mls . label } if _metric_minus == _tmp_lbl : _label_exists = True break _metric_idx = _metric_idx + 1 if _label_exists : _g = _obj . metric [ _metric_idx ] else : _g = _obj . metric . add ( ) else : _g = _obj . metric . add ( ) if obj_map [ _m ] == 'counter' : _g . counter . value = float ( _metric [ 'value' ] ) elif obj_map [ _m ] == 'gauge' : _g . gauge . value = float ( _metric [ 'value' ] ) elif obj_map [ _m ] == 'summary' : if '{}_count' . format ( _m ) in messages : _g . summary . sample_count = long ( self . get_metric_value_by_labels ( messages , _metric , _m , 'count' ) ) if '{}_sum' . format ( _m ) in messages : _g . summary . sample_sum = self . get_metric_value_by_labels ( messages , _metric , _m , 'sum' ) # TODO: see what can be done with the untyped metrics elif obj_map [ _m ] == 'histogram' : if '{}_count' . format ( _m ) in messages : _g . histogram . sample_count = long ( self . get_metric_value_by_labels ( messages , _metric , _m , 'count' ) ) if '{}_sum' . format ( _m ) in messages : _g . histogram . sample_sum = self . get_metric_value_by_labels ( messages , _metric , _m , 'sum' ) # last_metric = len(_obj.metric) - 1 # if last_metric >= 0: for lbl in _metric [ 'labels' ] : # In the string format, the quantiles are in the labels if lbl == 'quantile' : # _q = _obj.metric[last_metric].summary.quantile.add() _q = _g . summary . quantile . add ( ) _q . quantile = float ( _metric [ 'labels' ] [ lbl ] ) _q . value = float ( _metric [ 'value' ] ) # The upper_bounds are stored as "le" labels on string format elif obj_map [ _m ] == 'histogram' and lbl == 'le' : # _q = _obj.metric[last_metric].histogram.bucket.add() _q = _g . histogram . bucket . add ( ) _q . upper_bound = float ( _metric [ 'labels' ] [ lbl ] ) _q . cumulative_count = long ( float ( _metric [ 'value' ] ) ) else : # labels deduplication is_in_labels = False for _existing_lbl in _g . label : if lbl == _existing_lbl . name : is_in_labels = True if not is_in_labels : _l = _g . label . add ( ) _l . name = lbl _l . value = _metric [ 'labels' ] [ lbl ] return _obj