Python prometheus_client.core.GaugeMetricFamily() Examples

The following are 30 code examples of prometheus_client.core.GaugeMetricFamily(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prometheus_client.core , or try the search function .
Example #1
Source File: collector.py    From aliyun-exporter with Apache License 2.0 6 votes vote down vote up
def parse_rds_performance(self, id, value):
        value_format: str = value['ValueFormat']
        metric_name = value['Key']
        keys = ['value']
        if value_format is not None and '&' in value_format:
            keys = value_format.split('&')
        metric = value['Values']['PerformanceValue']
        if len(metric) < 1:
            return
        values = metric[0]['Value'].split('&')
        for k, v in zip(keys, values):
            gauge = GaugeMetricFamily(
                self.parent.format_metric_name(rds_performance, metric_name + '_' + k),
                '', labels=['instanceId'])
            gauge.add_metric([id], float(v))
            yield gauge 
Example #2
Source File: prometheus.py    From plugins with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def collect(self):
        info = self.rpc.getinfo()
        info_labels = {k.replace('-', '_'): v for k, v in info.items() if isinstance(v, str)}
        node_info_fam = InfoMetricFamily(
            'lightning_node',
            'Static node information',
            labels=info_labels.keys(),
        )
        node_info_fam.add_metric(info_labels, info_labels)
        yield node_info_fam

        blockheight = info['blockheight']
        yield GaugeMetricFamily(
            'lightning_node_blockheight',
            "Current Bitcoin blockheight on this node.",
            value=blockheight,
        )

        fees_msat = info["msatoshi_fees_collected"]
        yield GaugeMetricFamily(
            'lightning_fees_collected_msat',
            'How much have we been paid to route payments?',
            value=fees_msat,
        ) 
Example #3
Source File: stellar-core-prometheus-exporter.py    From blockchain-ops with MIT License 6 votes vote down vote up
def collect(self):
    response = requests.get(args.uri)
    json = response.json()

    metrics   = json['metrics']

    # iterate over all metrics
    for k in metrics:
      underscores = re.sub('\.|-|\s', '_', k).lower()

      if metrics[k]['type'] == 'timer':
        # we have a timer, expose as a Prometheus Summary
        underscores = underscores + '_' + metrics[k]['duration_unit']
        summary = SummaryMetricFamily(underscores, 'libmedida metric type: ' + metrics[k]['type'], count_value=metrics[k]['count'], sum_value=(metrics[k]['mean'] * metrics[k]['count']))
        # add stellar-core calculated quantiles to our summary
        summary.add_sample(underscores, labels={'quantile':'0.75'}, value=metrics[k]['75%'])
        summary.add_sample(underscores, labels={'quantile':'0.95'}, value=metrics[k]['95%'])
        summary.add_sample(underscores, labels={'quantile':'0.99'}, value=metrics[k]['99%'])
        yield summary
      elif metrics[k]['type'] == 'counter':
        # we have a counter, this is a Prometheus Gauge
        yield GaugeMetricFamily(underscores, 'libmedida metric type: ' + metrics[k]['type'], value=metrics[k]['count'])
      elif metrics[k]['type'] == 'meter':
        # we have a meter, this is a Prometheus Counter
        yield CounterMetricFamily(underscores, 'libmedida metric type: ' + metrics[k]['type'], value=metrics[k]['count']) 
Example #4
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_process_metric_filtered(aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config):
    """ Metric absent from the metrics_mapper """
    filtered_gauge = GaugeMetricFamily(
        'process_start_time_seconds', 'Start time of the process since unix epoch in seconds.'
    )
    filtered_gauge.add_metric([], 123456789.0)
    mocked_prometheus_scraper_config['_dry_run'] = False

    check = mocked_prometheus_check
    check.process_metric(filtered_gauge, mocked_prometheus_scraper_config, metric_transformers={})
    check.log.debug.assert_called_with(
        'Skipping metric `%s` as it is not defined in the metrics mapper, '
        'has no transformer function, nor does it match any wildcards.',
        'process_start_time_seconds',
    )
    aggregator.assert_all_metrics_covered() 
Example #5
Source File: prometheus.py    From plugins with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def collect(self):
        peers = self.rpc.listpeers()['peers']

        connected = GaugeMetricFamily(
            'lightning_peer_connected',
            'Is the peer currently connected?',
            labels=['id'],
        )
        count = GaugeMetricFamily(
            'lightning_peer_channels',
            "The number of channels with the peer",
            labels=['id'],
        )

        for p in peers:
            labels = [p['id']]
            count.add_metric(labels, len(p['channels']))
            connected.add_metric(labels, int(p['connected']))

        return [count, connected] 
Example #6
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_submit_gauge_with_labels_and_hostname_already_overridden(
    aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config
):
    """ submitting metrics that contain labels should result in tags on the gauge call """
    ref_gauge = GaugeMetricFamily(
        'process_virtual_memory_bytes', 'Virtual memory size in bytes.', labels=['my_1st_label', 'node']
    )
    ref_gauge.add_metric(['my_1st_label_value', 'foo'], 54927360.0)

    check = mocked_prometheus_check
    mocked_prometheus_scraper_config['label_to_hostname'] = 'node'
    metric_name = mocked_prometheus_scraper_config['metrics_mapper'][ref_gauge.name]
    check.submit_openmetric(metric_name, ref_gauge, mocked_prometheus_scraper_config, hostname='bar')
    aggregator.assert_metric(
        'prometheus.process.vm.bytes',
        54927360.0,
        tags=['my_1st_label:my_1st_label_value', 'node:foo'],
        hostname="bar",
        count=1,
    ) 
Example #7
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_submit_gauge_with_labels_mapper(aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config):
    """
    Submitting metrics that contain labels mappers should result in tags
    on the gauge call with transformed tag names
    """
    ref_gauge = GaugeMetricFamily(
        'process_virtual_memory_bytes', 'Virtual memory size in bytes.', labels=['my_1st_label', 'my_2nd_label']
    )
    ref_gauge.add_metric(['my_1st_label_value', 'my_2nd_label_value'], 54927360.0)

    check = mocked_prometheus_check
    mocked_prometheus_scraper_config['labels_mapper'] = {
        'my_1st_label': 'transformed_1st',
        'non_existent': 'should_not_matter',
        'env': 'dont_touch_custom_tags',
    }
    mocked_prometheus_scraper_config['custom_tags'] = ['env:dev', 'app:my_pretty_app']
    metric = mocked_prometheus_scraper_config['metrics_mapper'][ref_gauge.name]
    check.submit_openmetric(metric, ref_gauge, mocked_prometheus_scraper_config)
    aggregator.assert_metric(
        'prometheus.process.vm.bytes',
        54927360.0,
        tags=['env:dev', 'app:my_pretty_app', 'transformed_1st:my_1st_label_value', 'my_2nd_label:my_2nd_label_value'],
        count=1,
    ) 
Example #8
Source File: app.py    From pelorus with Apache License 2.0 6 votes vote down vote up
def collect(self):
        commit_metric = GaugeMetricFamily('github_commit_timestamp',
                                          'Commit timestamp', labels=['namespace', 'app', 'image_sha'])
        commit_metrics = self.generate_commit_metrics_list(self._namespaces)
        for my_metric in commit_metrics:
            logging.info("Namespace: %s, App: %s, Build: %s, Timestamp: %s"
                         % (
                             my_metric.namespace,
                             my_metric.name,
                             my_metric.build_name,
                             str(float(my_metric.commit_timestamp))
                         )
                         )
            commit_metric.add_metric([my_metric.namespace, my_metric.name, my_metric.image_hash],
                                     my_metric.commit_timestamp)
            yield commit_metric 
Example #9
Source File: 12-3-consul_metrics.py    From examples with Apache License 2.0 6 votes vote down vote up
def collect(self):
    out = urlopen("http://localhost:8500/v1/agent/metrics").read()
    metrics = json.loads(out.decode("utf-8"))

    for g in metrics["Gauges"]:
      yield GaugeMetricFamily(sanitise_name(g["Name"]),
          "Consul metric " + g["Name"], g["Value"])

    for c in metrics["Counters"]:
      yield CounterMetricFamily(sanitise_name(c["Name"]) + "_total",
          "Consul metric " + c["Name"], c["Count"])

    for s in metrics["Samples"]:
      yield SummaryMetricFamily(sanitise_name(s["Name"]) + "_seconds",
          "Consul metric " + s["Name"],
          count_value=c["Count"], sum_value=s["Sum"] / 1000) 
Example #10
Source File: collector.py    From prometheus-pve-exporter with Apache License 2.0 6 votes vote down vote up
def collect(self): # pylint: disable=missing-docstring
        clusters = [entry for entry in self._pve.cluster.status.get() if entry['type'] == 'cluster']

        if clusters:
            # Remove superflous keys.
            for cluster in clusters:
                del cluster['type']

            # Add cluster-prefix to id.
            for cluster in clusters:
                cluster['id'] = 'cluster/{:s}'.format(cluster['name'])
                del cluster['name']

            # Yield remaining data.
            labels = clusters[0].keys()
            info_metrics = GaugeMetricFamily(
                'pve_cluster_info',
                'Cluster info',
                labels=labels)

            for cluster in clusters:
                label_values = [str(cluster[key]) for key in labels]
                info_metrics.add_metric(label_values, 1)

            yield info_metrics 
Example #11
Source File: collector.py    From prometheus-pve-exporter with Apache License 2.0 6 votes vote down vote up
def collect(self): # pylint: disable=missing-docstring
        nodes = [entry for entry in self._pve.cluster.status.get() if entry['type'] == 'node']

        if nodes:
            # Remove superflous keys.
            for node in nodes:
                del node['type']
                del node['online']

            # Yield remaining data.
            labels = nodes[0].keys()
            info_metrics = GaugeMetricFamily(
                'pve_node_info',
                'Node info',
                labels=labels)

            for node in nodes:
                label_values = [str(node[key]) for key in labels]
                info_metrics.add_metric(label_values, 1)

            yield info_metrics 
Example #12
Source File: prometheus.py    From zstack-utility with Apache License 2.0 6 votes vote down vote up
def collect_host_capacity_statistics():
    default_zstack_path = '/usr/local/zstack/apache-tomcat/webapps/zstack'

    zstack_env_path = os.environ.get('ZSTACK_HOME', None)
    if zstack_env_path and zstack_env_path != default_zstack_path:
        default_zstack_path = zstack_env_path

    zstack_dir = ['/var/lib/zstack', '%s/../../../' % default_zstack_path, '/opt/zstack-dvd/',
                  '/var/log/zstack', '/var/lib/mysql', '/var/lib/libvirt', '/tmp/zstack']

    metrics = {
        'zstack_used_capacity_in_bytes': GaugeMetricFamily('zstack_used_capacity_in_bytes',
                                                           'ZStack used capacity in bytes')
    }

    zstack_used_capacity = 0
    for dir in zstack_dir:
        if not os.path.exists(dir):
            continue
        cmd = "du -bs %s | awk {\'print $1\'}" % dir
        res = bash_o(cmd)
        zstack_used_capacity += int(res)

    metrics['zstack_used_capacity_in_bytes'].add_metric([], float(zstack_used_capacity))
    return metrics.values() 
Example #13
Source File: collector.py    From prometheus-pve-exporter with Apache License 2.0 6 votes vote down vote up
def collect(self): # pylint: disable=missing-docstring
        status_metrics = GaugeMetricFamily(
            'pve_up',
            'Node/VM/CT-Status is online/running',
            labels=['id'])

        for entry in self._pve.cluster.status.get():
            if entry['type'] == 'node':
                label_values = [entry['id']]
                status_metrics.add_metric(label_values, entry['online'])
            elif entry['type'] == 'cluster':
                label_values = ['cluster/{:s}'.format(entry['name'])]
                status_metrics.add_metric(label_values, entry['quorate'])
            else:
                raise ValueError('Got unexpected status entry type {:s}'.format(entry['type']))

        for resource in self._pve.cluster.resources.get(type='vm'):
            label_values = [resource['id']]
            status_metrics.add_metric(label_values, resource['status'] == 'running')

        yield status_metrics 
Example #14
Source File: collectors.py    From prometheus-kafka-consumer-group-exporter with MIT License 6 votes vote down vote up
def gauge_generator(metrics):
    metric_dict = group_metrics(metrics)

    for metric_name, (metric_doc, label_keys, value_dict) in metric_dict.items():
        # If we have label keys we may have multiple different values,
        # each with their own label values.
        if label_keys:
            gauge = GaugeMetricFamily(metric_name, metric_doc, labels=label_keys)

            for label_values in sorted(value_dict.keys()):
                value = value_dict[label_values]
                gauge.add_metric(tuple(str(v) for v in label_values), value)

        # No label keys, so we must have only a single value.
        else:
            gauge = GaugeMetricFamily(metric_name, metric_doc, value=list(value_dict.values())[0])

        yield gauge 
Example #15
Source File: prometheus.py    From zstack-utility with Apache License 2.0 6 votes vote down vote up
def collect_lvm_capacity_statistics():
    metrics = {
        'vg_size': GaugeMetricFamily('vg_size',
                                     'volume group size', None, ['vg_name']),
        'vg_avail': GaugeMetricFamily('vg_avail',
                                      'volume group and thin pool free size', None, ['vg_name']),
    }

    r, o, e = bash_roe("vgs --nolocking --noheading -oname")
    if r != 0 or len(o.splitlines()) == 0:
        return metrics.values()

    vg_names = o.splitlines()
    for name in vg_names:
        name = name.strip()
        size, avail = lvm.get_vg_size(name, False)
        metrics['vg_size'].add_metric([name], float(size))
        metrics['vg_avail'].add_metric([name], float(avail))

    return metrics.values() 
Example #16
Source File: metrics.py    From ClusterRunner with Apache License 2.0 6 votes vote down vote up
def collect(self) -> Iterator[GaugeMetricFamily]:
        active, idle, dead = 0, 0, 0
        for slave in self._get_slaves():
            if slave.is_alive(use_cached=True) and slave.current_build_id is not None:
                active += 1
            elif slave.is_alive(use_cached=True) and slave.current_build_id is None:
                idle += 1
            elif not slave.is_alive(use_cached=True) and not slave.is_shutdown():
                # Slave is not alive and was not deliberately put in shutdown mode. Count it as dead.
                dead += 1
            else:
                # If not slave.is_alive() and slave.is_shutdown() = True then we have deliberately
                # and gracefully killed the slave. We do not want to categorize such a slave as 'dead'
                pass

        slaves_gauge = GaugeMetricFamily('slaves', 'Total number of slaves', labels=['state'])
        slaves_gauge.add_metric(['active'], active)
        slaves_gauge.add_metric(['idle'], idle)
        slaves_gauge.add_metric(['dead'], dead)
        yield slaves_gauge 
Example #17
Source File: app.py    From pelorus with Apache License 2.0 5 votes vote down vote up
def collect(self):
        print("Starting Collection")
        options = {
            'server': self.server
        }
        # Connect to Jira
        jira = JIRA(options, basic_auth=(self.user, self.apikey))
        # TODO FIXME This may need to be modified to suit needs and have a time period.
        query_string = "project=" + self.project + " and type=bug and priority=highest"
        # Query Jira and loop results if any are returned.
        critical_issues = jira.search_issues(query_string)
        if len(critical_issues) > 0:
            creation_metric = GaugeMetricFamily('failure_creation_timestamp',
                                                'Failure Creation Timestamp',
                                                labels=['project', 'issue_number'])
            failure_metric = GaugeMetricFamily('failure_resolution_timestamp',
                                               'Failure Resolution Timestamp',
                                               labels=['project', 'issue_number'])
            metrics = self.generate_metrics(self.project, critical_issues)
            for m in metrics:
                if not m.is_resolution:
                    creation_metric.add_metric(m.labels, m.get_value())
                    yield(creation_metric)
                else:
                    failure_metric.add_metric(m.labels, m.get_value())
                    yield(failure_metric) 
Example #18
Source File: app.py    From pelorus with Apache License 2.0 5 votes vote down vote up
def collect(self):
        metric = GaugeMetricFamily('deploy_timestamp', 'Deployment timestamp', labels=['namespace', 'app', 'image_sha'])
        metrics = generate_metrics(self._namespaces)
        for m in metrics:
            print("Namespace: ", m.namespace, ", App: ", m.name, ", Image: ", m.image_sha)
            metric.add_metric([m.namespace, m.name, m.image_sha, m.deploy_time],
                              pelorus.convert_date_time_to_timestamp(m.deploy_time))
            yield(metric) 
Example #19
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_submit_gauge_with_labels_and_hostname_override(
    aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config
):
    """ submitting metrics that contain labels should result in tags on the gauge call """
    ref_gauge = GaugeMetricFamily(
        'process_virtual_memory_bytes', 'Virtual memory size in bytes.', labels=['my_1st_label', 'node']
    )
    ref_gauge.add_metric(['my_1st_label_value', 'foo'], 54927360.0)

    check = mocked_prometheus_check
    mocked_prometheus_scraper_config['label_to_hostname'] = 'node'
    metric_name = mocked_prometheus_scraper_config['metrics_mapper'][ref_gauge.name]
    check.submit_openmetric(metric_name, ref_gauge, mocked_prometheus_scraper_config)
    aggregator.assert_metric(
        'prometheus.process.vm.bytes',
        54927360.0,
        tags=['my_1st_label:my_1st_label_value', 'node:foo'],
        hostname="foo",
        count=1,
    )

    # also test with a hostname suffix
    check2 = mocked_prometheus_check
    mocked_prometheus_scraper_config['label_to_hostname'] = 'node'
    mocked_prometheus_scraper_config['label_to_hostname_suffix'] = '-cluster-blue'
    metric_name = mocked_prometheus_scraper_config['metrics_mapper'][ref_gauge.name]
    check2.submit_openmetric(metric_name, ref_gauge, mocked_prometheus_scraper_config)
    aggregator.assert_metric(
        'prometheus.process.vm.bytes',
        54927360.0,
        tags=['my_1st_label:my_1st_label_value', 'node:foo'],
        hostname="foo-cluster-blue",
        count=1,
    ) 
Example #20
Source File: prometheus_app.py    From postgraas_server with Apache License 2.0 5 votes vote down vote up
def collect(self):
        count = count_postgraas_instances(self.config)
        count_metric = GaugeMetricFamily(self.metric_name, self.help, value=count)
        yield count_metric 
Example #21
Source File: prometheus_app.py    From postgraas_server with Apache License 2.0 5 votes vote down vote up
def describe(self):
        yield GaugeMetricFamily(self.metric_name, self.help) 
Example #22
Source File: reserved_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def _create_gauges(self):
        reserved_vms = GaugeMetricFamily(
            self._metric_name,
            "Number of reserved virtual machines per Azure subscription, location, duration, and vm size",
            labels=_BASE_COLUMNS)
        expirations = GaugeMetricFamily(
            self._metric_name + "_next_expiration",
            "Timestamp of the next expiration of a virtual machine reservation of certain location, size, duration, and subscription",
            labels=_BASE_COLUMNS)
        return reserved_vms, expirations 
Example #23
Source File: allocated_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def _create_gauge(self):
        description = "Number of virtual machines per Azure subscription, location, resource group, and vm size"
        allocated_vms = GaugeMetricFamily(self._metric_name, description, labels=_BASE_COLUMNS)
        return allocated_vms 
Example #24
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ref_gauge():
    ref_gauge = GaugeMetricFamily('process_virtual_memory_bytes', 'Virtual memory size in bytes.')
    ref_gauge.add_metric([], 54927360.0)

    return ref_gauge 
Example #25
Source File: metrics.py    From prometheus-es-exporter with MIT License 5 votes vote down vote up
def gauge_generator(metric_dict):
    """
    Generates GaugeMetricFamily instances for a list of metrics.

    Takes metrics as a dict keyed by metric name. Each metric name maps to a
    tuple containing:
    * metric documentation
    * label keys tuple,
    * dict of label values tuple -> metric value.

    Yields a GaugeMetricFamily instance for each unique metric name, containing
    children for the various label combinations. Suitable for use in a collect()
    method of a Prometheus collector.
    """

    for metric_name, (metric_doc, label_keys, value_dict) in metric_dict.items():
        # If we have label keys we may have multiple different values,
        # each with their own label values.
        if label_keys:
            gauge = GaugeMetricFamily(metric_name, metric_doc, labels=label_keys)

            for label_values in sorted(value_dict.keys()):
                value = value_dict[label_values]
                gauge.add_metric(label_values, value)

        # No label keys, so we must have only a single value.
        else:
            gauge = GaugeMetricFamily(metric_name, metric_doc, value=list(value_dict.values())[0])

        yield gauge 
Example #26
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_submit_gauge_with_exclude_labels(aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config):
    """
    Submitting metrics when filtering with exclude_labels should end up with
    a filtered tags list
    """
    ref_gauge = GaugeMetricFamily(
        'process_virtual_memory_bytes', 'Virtual memory size in bytes.', labels=['my_1st_label', 'my_2nd_label']
    )
    ref_gauge.add_metric(['my_1st_label_value', 'my_2nd_label_value'], 54927360.0)

    check = mocked_prometheus_check
    mocked_prometheus_scraper_config['labels_mapper'] = {
        'my_1st_label': 'transformed_1st',
        'non_existent': 'should_not_matter',
        'env': 'dont_touch_custom_tags',
    }
    mocked_prometheus_scraper_config['custom_tags'] = ['env:dev', 'app:my_pretty_app']
    mocked_prometheus_scraper_config['exclude_labels'] = [
        'my_2nd_label',
        'whatever_else',
        'env',
    ]  # custom tags are not filtered out
    metric = mocked_prometheus_scraper_config['metrics_mapper'][ref_gauge.name]
    check.submit_openmetric(metric, ref_gauge, mocked_prometheus_scraper_config)
    aggregator.assert_metric(
        'prometheus.process.vm.bytes',
        54927360.0,
        tags=['env:dev', 'app:my_pretty_app', 'transformed_1st:my_1st_label_value'],
        count=1,
    ) 
Example #27
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_submit_rate(aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config):
    _rate = GaugeMetricFamily('my_rate', 'Random rate')
    _rate.add_metric([], 42)
    check = mocked_prometheus_check
    check.submit_openmetric('custom.rate', _rate, mocked_prometheus_scraper_config)
    aggregator.assert_metric('prometheus.custom.rate', 42, tags=[], count=1) 
Example #28
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_filter_sample_on_gauge(p_check, mocked_prometheus_scraper_config):
    """
    Add a filter blacklist on the check matching one line and make sure
    only the two other lines are parsed and sent downstream.
    """
    text_data = (
        '# HELP kube_deployment_status_replicas The number of replicas per deployment.\n'
        '# TYPE kube_deployment_status_replicas gauge\n'
        'kube_deployment_status_replicas{deployment="event-exporter-v0.1.7"} 1\n'
        'kube_deployment_status_replicas{deployment="heapster-v1.4.3"} 1\n'
        'kube_deployment_status_replicas{deployment="kube-dns"} 2\n'
    )

    expected_metric = GaugeMetricFamily(
        'kube_deployment_status_replicas', 'The number of replicas per deployment.', labels=['deployment']
    )
    expected_metric.add_metric(['event-exporter-v0.1.7'], 1)
    expected_metric.add_metric(['heapster-v1.4.3'], 1)

    # Iter on the generator to get all metrics
    response = MockResponse(text_data, text_content_type)
    check = p_check
    mocked_prometheus_scraper_config['_text_filter_blacklist'] = ["deployment=\"kube-dns\""]
    metrics = [k for k in check.parse_metric_family(response, mocked_prometheus_scraper_config)]

    assert 1 == len(metrics)
    current_metric = metrics[0]
    assert expected_metric == current_metric 
Example #29
Source File: test_openmetrics.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_parse_one_gauge(p_check, mocked_prometheus_scraper_config):
    """
    name: "etcd_server_has_leader"
    help: "Whether or not a leader exists. 1 is existence, 0 is not."
    type: GAUGE
    metric {
      gauge {
        value: 1.0
      }
    }
    """
    text_data = (
        "# HELP etcd_server_has_leader Whether or not a leader exists. 1 is existence, 0 is not.\n"
        "# TYPE etcd_server_has_leader gauge\n"
        "etcd_server_has_leader 1\n"
    )

    expected_etcd_metric = GaugeMetricFamily(
        'etcd_server_has_leader', 'Whether or not a leader exists. 1 is existence, 0 is not.'
    )
    expected_etcd_metric.add_metric([], 1)

    # Iter on the generator to get all metrics
    response = MockResponse(text_data, text_content_type)
    check = p_check
    metrics = [k for k in check.parse_metric_family(response, mocked_prometheus_scraper_config)]

    assert 1 == len(metrics)
    current_metric = metrics[0]
    assert expected_etcd_metric == current_metric 
Example #30
Source File: prometheus.py    From zstack-utility with Apache License 2.0 5 votes vote down vote up
def collect_raid_state():
    metrics = {
        'raid_state': GaugeMetricFamily('raid_state',
                                        'raid state', None, ['target_id']),
        'physical_disk_state': GaugeMetricFamily('physical_disk_state',
                                                 'physical disk state', None,
                                                 ['slot_number', 'disk_group']),
    }
    if bash_r("/opt/MegaRAID/MegaCli/MegaCli64 -LDInfo -LALL -aAll") != 0:
        return metrics.values()

    raid_info = bash_o("/opt/MegaRAID/MegaCli/MegaCli64 -LDInfo -LALL -aAll | grep -E 'Target Id|State'").strip().splitlines()
    target_id = state = None
    for info in raid_info:
        if "Target Id" in info:
            target_id = info.strip().strip(")").split(" ")[-1]
        else:
            state = info.strip().split(" ")[-1]
            metrics['raid_state'].add_metric([target_id], convert_raid_state_to_int(state))

    disk_info = bash_o(
        "/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll | grep -E 'Slot Number|DiskGroup|Firmware state'").strip().splitlines()
    slot_number = state = disk_group = None
    for info in disk_info:
        if "Slot Number" in info:
            slot_number = info.strip().split(" ")[-1]
        elif "DiskGroup" in info:
            kvs = info.replace("Drive's position: ", "").split(",")
            disk_group = filter(lambda x: "DiskGroup" in x, kvs)[0]
            disk_group = disk_group.split(" ")[-1]
        else:
            state = info.strip().split(":")[-1]
            metrics['physical_disk_state'].add_metric([slot_number, disk_group], convert_disk_state_to_int(state))

    return metrics.values()