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 29, 2022
1 parent ef9847f commit 968e287
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
)
from prometheus_client.core import Metric as PrometheusMetric

from opentelemetry.sdk.metrics._internal.aggregation import (
AggregationTemporality,
)
from opentelemetry.sdk.metrics.export import (
Gauge,
Histogram,
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,16 @@ 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 @@ -23,6 +23,7 @@
PrometheusMetricReader,
_CustomCollector,
)
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
Histogram,
Expand Down Expand Up @@ -51,7 +52,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 +287,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 968e287

Please sign in to comment.