'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):

    _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]
    _newlbl = _m
    if (obj_map[_m] == 'histogram'):
        _newlbl = '{}_bucket'.format(_m)
    for _metric in messages[_newlbl]:
        if ((obj_map[_m] in ['summary', 'histogram']) and (len(_obj.metric) > 0)):
            _label_exists = False
            _metric_minus = {k: v for (k, v) in _metric['labels'].items() 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(float(messages['{}_count'.format(_m)][0]['value']))
            if ('{}_sum'.format(_m) in messages):
                _g.summary.sample_sum = float(messages['{}_sum'.format(_m)][0]['value'])
        elif (obj_map[_m] == 'histogram'):
            if ('{}_count'.format(_m) in messages):
                _g.histogram.sample_count = long(float(messages['{}_count'.format(_m)][0]['value']))
            if ('{}_sum'.format(_m) in messages):
                _g.histogram.sample_sum = float(messages['{}_sum'.format(_m)][0]['value'])
        for lbl in _metric['labels']:
            if (lbl == 'quantile'):
                _q = _g.summary.quantile.add()
                _q.quantile = float(_metric['labels'][lbl])
                _q.value = float(_metric['value'])
            elif ((obj_map[_m] == 'histogram') and (lbl == 'le')):
                _q = _g.histogram.bucket.add()
                _q.upper_bound = float(_metric['labels'][lbl])
                _q.cumulative_count = long(float(_metric['value']))
            else:
                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
