Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76d8950

Browse files
author
Alex Boten
authored
Merge branch 'main' into codeboten/test-async
2 parents 771c4d2 + 9277351 commit 76d8950

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import collections
6666
import logging
6767
import re
68+
from itertools import chain
6869
from typing import Iterable, Optional, Sequence, Tuple
6970

7071
from prometheus_client import core
@@ -78,14 +79,13 @@
7879
def _convert_buckets(metric: Metric) -> Sequence[Tuple[str, int]]:
7980
buckets = []
8081
total_count = 0
81-
for index, value in enumerate(metric.point.bucket_counts):
82-
total_count += value
83-
buckets.append(
84-
(
85-
f"{metric.point.explicit_bounds[index]}",
86-
total_count,
87-
)
88-
)
82+
for upper_bound, count in zip(
83+
chain(metric.point.explicit_bounds, ["+Inf"]),
84+
metric.point.bucket_counts,
85+
):
86+
total_count += count
87+
buckets.append((f"{upper_bound}", total_count))
88+
8989
return buckets
9090

9191

exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
from textwrap import dedent
1617
from unittest import mock
1718

1819
from prometheus_client import generate_latest
@@ -61,10 +62,10 @@ def test_histogram_to_prometheus(self):
6162
Histogram(
6263
time_unix_nano=1641946016139533244,
6364
start_time_unix_nano=1641946016139533244,
64-
bucket_counts=[1, 1],
65+
bucket_counts=[1, 3, 2],
6566
sum=579.0,
6667
explicit_bounds=[123.0, 456.0],
67-
aggregation_temporality=AggregationTemporality.DELTA,
68+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
6869
),
6970
attributes={"histo": 1},
7071
)
@@ -73,8 +74,20 @@ def test_histogram_to_prometheus(self):
7374
collector.add_metrics_data([record])
7475
result_bytes = generate_latest(collector)
7576
result = result_bytes.decode("utf-8")
76-
self.assertIn('testprefix_test_name_s_sum{histo="1"} 579.0', result)
77-
self.assertIn('testprefix_test_name_s_count{histo="1"} 2.0', result)
77+
self.assertEqual(
78+
result,
79+
dedent(
80+
"""\
81+
# HELP testprefix_test_name_s foo
82+
# TYPE testprefix_test_name_s histogram
83+
testprefix_test_name_s_bucket{histo="1",le="123.0"} 1.0
84+
testprefix_test_name_s_bucket{histo="1",le="456.0"} 4.0
85+
testprefix_test_name_s_bucket{histo="1",le="+Inf"} 6.0
86+
testprefix_test_name_s_count{histo="1"} 6.0
87+
testprefix_test_name_s_sum{histo="1"} 579.0
88+
"""
89+
),
90+
)
7891

7992
def test_sum_to_prometheus(self):
8093
labels = {"environment@": "staging", "os": "Windows"}

0 commit comments

Comments
 (0)