Skip to content

Commit

Permalink
Verify previous point is returned for cumulative instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jun 21, 2022
1 parent d4d7c67 commit 5c53d96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
HistogramMetricFamily,
)
from prometheus_client.core import Metric as PrometheusMetric
from opentelemetry.sdk.metrics._internal.aggregation import (
AggregationTemporality
)

from opentelemetry.sdk.metrics.export import (
Gauge,
Expand All @@ -85,6 +88,7 @@
MetricsData,
Sum,
)
from opentelemetry.sdk.metrics.view import Aggregation

_logger = getLogger(__name__)

Expand Down Expand Up @@ -112,8 +116,18 @@ class PrometheusMetricReader(MetricReader):
the metric belongs to.
"""

def __init__(self, prefix: str = "") -> None:
super().__init__()
def __init__(
self,
preferred_temporality: Dict[type, AggregationTemporality] = None,
preferred_aggregation: Dict[
type, Aggregation
] = None,
prefix: str = ""
) -> None:
super().__init__(
preferred_temporality=preferred_temporality,
preferred_aggregation=preferred_aggregation
)
self._collector = _CustomCollector(prefix)
REGISTRY.register(self._collector)
self._collector._callback = self.collect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from prometheus_client import generate_latest
from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily
from opentelemetry.sdk.metrics import MeterProvider

from opentelemetry.exporter.prometheus import (
PrometheusMetricReader,
Expand All @@ -37,6 +38,7 @@
_generate_sum,
_generate_unsupported_metric,
)
from opentelemetry.sdk.metrics import Counter


class TestPrometheusMetricReader(TestCase):
Expand All @@ -51,7 +53,7 @@ def setUp(self):
def test_constructor(self):
"""Test the constructor."""
with self._registry_register_patch:
exporter = PrometheusMetricReader("testprefix")
exporter = PrometheusMetricReader(prefix="testprefix")
self.assertEqual(exporter._collector._prefix, "testprefix")
self.assertTrue(self._mock_registry_register.called)

Expand Down Expand Up @@ -286,3 +288,33 @@ def test_check_value(self):
self.assertEqual(collector._check_value(True), "true")
self.assertEqual(collector._check_value(False), "false")
self.assertEqual(collector._check_value(None), "null")

def test_multiple_collection_calls_1(self):

metric_reader = PrometheusMetricReader(
prefix="prefix",
preferred_temporality={Counter: AggregationTemporality.CUMULATIVE}
)
provider = MeterProvider(metric_readers=[metric_reader])
meter = provider.get_meter("getting-started", "0.1.2")
counter = meter.create_counter("counter")
counter.add(1)
result_0 = list(metric_reader._collector.collect())
result_1 = list(metric_reader._collector.collect())
result_2 = list(metric_reader._collector.collect())
self.assertEqual(result_0, result_1)
self.assertEqual(result_1, result_2)

metric_reader = PrometheusMetricReader(
prefix="prefix",
preferred_temporality={Counter: AggregationTemporality.DELTA}
)
provider = MeterProvider(metric_readers=[metric_reader])
meter = provider.get_meter("getting-started", "0.1.2")
counter = meter.create_counter("counter")
counter.add(1)
result_0 = list(metric_reader._collector.collect())
result_1 = list(metric_reader._collector.collect())
result_2 = list(metric_reader._collector.collect())
self.assertNotEqual(result_0, result_1)
self.assertEqual(result_1, result_2)

0 comments on commit 5c53d96

Please sign in to comment.