Skip to content

Commit

Permalink
Fix histogram & summaries float tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mfpierre committed Aug 29, 2018
1 parent 99f2a1d commit 14faa49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
17 changes: 13 additions & 4 deletions datadog_checks_base/datadog_checks/checks/openmetrics/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,15 @@ def _submit_gauges_from_summary(self, metric_name, metric, scraper_config, hostn
self.log.debug("Metric value is not supported for metric {}".format(sample[self.SAMPLE_NAME]))
continue
custom_hostname = self._get_hostname(hostname, sample, scraper_config)
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
if sample[self.SAMPLE_NAME].endswith("_sum"):
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
self.gauge("{}.{}.sum".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)
if sample[self.SAMPLE_NAME].endswith("_count"):
elif sample[self.SAMPLE_NAME].endswith("_count"):
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
self.gauge("{}.{}.count".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)
else:
sample[self.SAMPLE_LABELS]["quantile"] = float(sample[self.SAMPLE_LABELS]["quantile"])
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
self.gauge("{}.{}.quantile".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)

def _submit_gauges_from_histogram(self, metric_name, metric, scraper_config, hostname=None):
Expand All @@ -514,10 +517,16 @@ def _submit_gauges_from_histogram(self, metric_name, metric, scraper_config, hos
self.log.debug("Metric value is not supported for metric {}".format(sample[self.SAMPLE_NAME]))
continue
custom_hostname = self._get_hostname(hostname, sample, scraper_config)
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname)
if sample[self.SAMPLE_NAME].endswith("_sum"):
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname)
self.gauge("{}.{}.sum".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)
elif sample[self.SAMPLE_NAME].endswith("_count") or (sample[self.SAMPLE_NAME].endswith("_bucket") and scraper_config['send_histograms_buckets']):
elif sample[self.SAMPLE_NAME].endswith("_count"):
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname)
self.gauge("{}.{}.count".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)
elif (scraper_config['send_histograms_buckets'] and sample[self.SAMPLE_NAME].endswith("_bucket") and
"Inf" not in sample[self.SAMPLE_LABELS]["le"]):
sample[self.SAMPLE_LABELS]["le"] = float(sample[self.SAMPLE_LABELS]["le"])
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname)
self.gauge("{}.{}.count".format(scraper_config['namespace'], metric_name), val, tags=tags, hostname=custom_hostname)

def _metric_tags(self, metric_name, val, sample, scraper_config, hostname=None):
Expand Down
9 changes: 5 additions & 4 deletions datadog_checks_base/tests/test_openmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,14 @@ def test_submit_summary(aggregator, mocked_prometheus_check, mocked_prometheus_s

def test_submit_histogram(aggregator, mocked_prometheus_check, mocked_prometheus_scraper_config):
_histo = HistogramMetricFamily('my_histogram', 'my_histogram')
_histo.add_metric([], buckets=[('0', 1), ('+Inf', 2)], sum_value=3)
_histo.add_metric([], buckets=[("-Inf", 0), ("1", 1), ("3.1104e+07", 1), ("4.324e+08", 1) , ("+Inf", 3)], sum_value=3)
check = mocked_prometheus_check
check._submit('custom.histogram', _histo, mocked_prometheus_scraper_config)
aggregator.assert_metric('prometheus.custom.histogram.sum', 3, tags=[], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 2, tags=[])
aggregator.assert_metric('prometheus.custom.histogram.count', 1, tags=['upper_bound:0'], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 2, tags=['upper_bound:+Inf'], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 3, tags=[], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 1, tags=['upper_bound:1.0'], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 1, tags=['upper_bound:31104000.0'], count=1)
aggregator.assert_metric('prometheus.custom.histogram.count', 1, tags=['upper_bound:432400000.0'], count=1)
aggregator.assert_all_metrics_covered()


Expand Down

0 comments on commit 14faa49

Please sign in to comment.