Skip to content

Commit 88ad971

Browse files
authored
Exporter/Prometheus: Append +Inf to histogram buckets. (census-instrumentation#490)
Prometheus requires buckets to be sorted, and +Inf present. Append +Inf to the bucket boundary list. Prometheus client comment is https://github.com/prometheus/client_python/blob/master/prometheus_client/metrics_core.py#L204.
1 parent beb70f7 commit 88ad971

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

opencensus/stats/exporters/prometheus_exporter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ def to_metric(self, desc, tag_values, agg_data):
188188
for ii, bound in enumerate(agg_data.bounds):
189189
cum_count += agg_data.counts_per_bucket[ii]
190190
points[str(bound)] = cum_count
191+
# Prometheus requires buckets to be sorted, and +Inf present.
192+
# In OpenCensus we don't have +Inf in the bucket bonds so need to
193+
# append it here.
194+
points["+Inf"] = agg_data.count_data
191195
metric = HistogramMetricFamily(name=metric_name,
192196
documentation=metric_description,
193197
labels=label_keys)

tests/unit/stats/exporter/test_prometheus_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_collector_to_metric_histogram(self):
195195
self.assertEqual(desc['name'], metric.name)
196196
self.assertEqual(desc['documentation'], metric.documentation)
197197
self.assertEqual('histogram', metric.type)
198-
self.assertEqual(4, len(metric.samples))
198+
self.assertEqual(5, len(metric.samples))
199199

200200
def test_collector_to_metric_invalid_dist(self):
201201
agg = mock.Mock()
@@ -238,7 +238,7 @@ def test_collector_collect(self):
238238
self.assertEqual(desc['name'], metric.name)
239239
self.assertEqual(desc['documentation'], metric.documentation)
240240
self.assertEqual('histogram', metric.type)
241-
self.assertEqual(4, len(metric.samples))
241+
self.assertEqual(5, len(metric.samples))
242242

243243
def test_collector_collect_with_none_label_value(self):
244244
agg = aggregation_module.LastValueAggregation(256)

0 commit comments

Comments
 (0)