Python prometheus_client.core.CounterMetricFamily() Examples

The following are 8 code examples of prometheus_client.core.CounterMetricFamily(). 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: collectors.py    From prometheus-kafka-consumer-group-exporter with MIT License 6 votes vote down vote up
def counter_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:
            counter = CounterMetricFamily(metric_name, metric_doc, labels=label_keys)

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

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

        yield counter 
Example #2
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 #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_submit_counter(
    aggregator,
    mocked_prometheus_check,
    mocked_prometheus_scraper_config,
    config,
    counter_metric_monotonic,
    counter_with_gauge,
):
    # Determine expected metric types for counter metrics
    counter_type = aggregator.GAUGE
    if counter_metric_monotonic:
        counter_type = aggregator.MONOTONIC_COUNT

    metric_name = 'prometheus.custom.counter'
    _counter = CounterMetricFamily('my_counter', 'Random counter')
    _counter.add_metric([], 42)
    mocked_prometheus_scraper_config.update(config)
    check = mocked_prometheus_check
    check.submit_openmetric('custom.counter', _counter, mocked_prometheus_scraper_config)
    aggregator.assert_metric(metric_name, 42, tags=[], count=1, metric_type=counter_type)

    if counter_with_gauge:
        aggregator.assert_metric(metric_name + '.total', 42, tags=[], count=1, metric_type=aggregator.MONOTONIC_COUNT)

    aggregator.assert_all_metrics_covered() 
Example #5
Source File: collector.py    From prometheus-pgbouncer-exporter with MIT License 5 votes vote down vote up
def _instanceMetric(self, data):
        if data["type"] is "counter":
            return CounterMetricFamily(data["name"], data["help"], labels=data["labels"].keys())
        elif data["type"] is "gauge":
            return GaugeMetricFamily(data["name"], data["help"], labels=data["labels"].keys())
        else:
            raise Exception("Unsupported metric type: {type}".format(type=data['type'])) 
Example #6
Source File: test_prometheus_exporter.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_counter_to_prometheus(self):
        meter = get_meter_provider().get_meter(__name__)
        metric = meter.create_metric(
            "test@name",
            "testdesc",
            "unit",
            int,
            metrics.Counter,
            ["environment@", "os"],
        )
        labels = {"environment@": "staging", "os": "Windows"}
        key_labels = metrics.get_labels_as_key(labels)
        aggregator = SumAggregator()
        aggregator.update(123)
        aggregator.take_checkpoint()
        record = MetricRecord(metric, key_labels, aggregator)
        collector = CustomCollector("testprefix")
        collector.add_metrics_data([record])

        for prometheus_metric in collector.collect():
            self.assertEqual(type(prometheus_metric), CounterMetricFamily)
            self.assertEqual(prometheus_metric.name, "testprefix_test_name")
            self.assertEqual(prometheus_metric.documentation, "testdesc")
            self.assertTrue(len(prometheus_metric.samples) == 1)
            self.assertEqual(prometheus_metric.samples[0].value, 123)
            self.assertTrue(len(prometheus_metric.samples[0].labels) == 2)
            self.assertEqual(
                prometheus_metric.samples[0].labels["environment_"], "staging"
            )
            self.assertEqual(
                prometheus_metric.samples[0].labels["os"], "Windows"
            )

    # TODO: Add unit test once GaugeAggregator is available
    # TODO: Add unit test once Measure Aggregators are available 
Example #7
Source File: enterprise_billing_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def _create_counter(self):
        """
        Create a counter instance.
        
        :return: prometheus_client counter instance
        """
        description = "Costs billed to Azure Enterprise Agreement {}".format(self._enrollment)
        c = CounterMetricFamily(self._metric_name, description, labels=base_columns)
        return c 
Example #8
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_counter(p_check, mocked_prometheus_scraper_config):
    """
    name: "go_memstats_mallocs_total"
    help: "Total number of mallocs."
    type: COUNTER
    metric {
      counter {
        value: 18713.0
      }
    }
    """
    text_data = (
        "# HELP go_memstats_mallocs_total Total number of mallocs.\n"
        "# TYPE go_memstats_mallocs_total counter\n"
        "go_memstats_mallocs_total 18713\n"
    )

    expected_etcd_metric = CounterMetricFamily('go_memstats_mallocs_total', 'Total number of mallocs.')
    expected_etcd_metric.add_metric([], 18713)
    # Fix up the _total change
    expected_etcd_metric.name = 'go_memstats_mallocs_total'

    # 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