diff --git a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py index ac1ea58c660..c01d109210f 100644 --- a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py +++ b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py @@ -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, @@ -85,6 +88,7 @@ MetricsData, Sum, ) +from opentelemetry.sdk.metrics.view import Aggregation _logger = getLogger(__name__) @@ -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 diff --git a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py index 4b6118bdab1..b1de127737b 100644 --- a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py +++ b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py @@ -23,6 +23,7 @@ PrometheusMetricReader, _CustomCollector, ) +from opentelemetry.sdk.metrics import Counter, MeterProvider from opentelemetry.sdk.metrics.export import ( AggregationTemporality, Histogram, @@ -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) @@ -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)