Skip to content

Commit 87b459f

Browse files
authored
Remove instrument_class_temporality from collect args (#2674)
Fixes #2673
1 parent f367ec2 commit 87b459f

File tree

8 files changed

+156
-69
lines changed

8 files changed

+156
-69
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/_view_instrument_match.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(
4343
view: View,
4444
instrument: Instrument,
4545
sdk_config: SdkConfiguration,
46+
instrument_class_temporality: Dict[type, AggregationTemporality],
4647
instrument_class_aggregation: Dict[type, Aggregation],
4748
):
4849
self._view = view
@@ -51,6 +52,7 @@ def __init__(
5152
self._attributes_aggregation: Dict[frozenset, _Aggregation] = {}
5253
self._attributes_previous_point: Dict[frozenset, _PointVarT] = {}
5354
self._lock = Lock()
55+
self._instrument_class_temporality = instrument_class_temporality
5456
self._instrument_class_aggregation = instrument_class_aggregation
5557
self._name = self._view._name or self._instrument.name
5658
self._description = (
@@ -122,9 +124,7 @@ def consume_measurement(self, measurement: Measurement) -> None:
122124

123125
self._attributes_aggregation[attributes].aggregate(measurement)
124126

125-
def collect(
126-
self, instrument_class_temporality: Dict[type, AggregationTemporality]
127-
) -> Iterable[Metric]:
127+
def collect(self) -> Iterable[Metric]:
128128

129129
with self._lock:
130130
for (
@@ -139,6 +139,7 @@ def collect(
139139
current_point = aggregation.collect()
140140

141141
# pylint: disable=assignment-from-none
142+
142143
self._attributes_previous_point[
143144
attributes
144145
] = _convert_aggregation_temporality(
@@ -161,7 +162,7 @@ def collect(
161162
point=_convert_aggregation_temporality(
162163
previous_point,
163164
current_point,
164-
instrument_class_temporality[
165+
self._instrument_class_temporality[
165166
self._instrument.__class__
166167
],
167168
),

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/export/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def collect(self, timeout_millis: float = 10_000) -> None:
241241
)
242242
return
243243
self._receive_metrics(
244-
self._collect(self, self._instrument_class_temporality),
244+
self._collect(self),
245245
timeout_millis=timeout_millis,
246246
)
247247

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/measurement_consumer.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
from abc import ABC, abstractmethod
1818
from threading import Lock
19-
from typing import Dict, Iterable, List, Mapping
19+
from typing import Iterable, List, Mapping
2020

2121
# This kind of import is needed to avoid Sphinx errors.
2222
import opentelemetry.sdk._metrics
2323
import opentelemetry.sdk._metrics._internal.instrument
2424
import opentelemetry.sdk._metrics._internal.sdk_configuration
2525
from opentelemetry._metrics._internal.instrument import CallbackOptions
26-
from opentelemetry.sdk._metrics._internal.export import AggregationTemporality
2726
from opentelemetry.sdk._metrics._internal.measurement import Measurement
2827
from opentelemetry.sdk._metrics._internal.metric_reader_storage import (
2928
MetricReaderStorage,
@@ -49,7 +48,6 @@ def register_asynchronous_instrument(
4948
def collect(
5049
self,
5150
metric_reader: "opentelemetry.sdk._metrics.MetricReader",
52-
instrument_type_temporality: Dict[type, AggregationTemporality],
5351
) -> Iterable[Metric]:
5452
pass
5553

@@ -66,7 +64,9 @@ def __init__(
6664
"opentelemetry.sdk._metrics.MetricReader", MetricReaderStorage
6765
] = {
6866
reader: MetricReaderStorage(
69-
sdk_config, reader._instrument_class_aggregation
67+
sdk_config,
68+
reader._instrument_class_temporality,
69+
reader._instrument_class_aggregation,
7070
)
7171
for reader in sdk_config.metric_readers
7272
}
@@ -90,7 +90,6 @@ def register_asynchronous_instrument(
9090
def collect(
9191
self,
9292
metric_reader: "opentelemetry.sdk._metrics.MetricReader",
93-
instrument_type_temporality: Dict[type, AggregationTemporality],
9493
) -> Iterable[Metric]:
9594
with self._lock:
9695
metric_reader_storage = self._reader_storages[metric_reader]
@@ -99,6 +98,4 @@ def collect(
9998
for async_instrument in self._async_instruments:
10099
for measurement in async_instrument.callback(callback_options):
101100
metric_reader_storage.consume_measurement(measurement)
102-
return self._reader_storages[metric_reader].collect(
103-
instrument_type_temporality
104-
)
101+
return self._reader_storages[metric_reader].collect()

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/metric_reader_storage.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ class MetricReaderStorage:
4343
def __init__(
4444
self,
4545
sdk_config: SdkConfiguration,
46+
instrument_class_temporality: Dict[type, AggregationTemporality],
4647
instrument_class_aggregation: Dict[type, Aggregation],
4748
) -> None:
4849
self._lock = RLock()
4950
self._sdk_config = sdk_config
5051
self._instrument_view_instrument_matches: Dict[
5152
Instrument, List[_ViewInstrumentMatch]
5253
] = {}
54+
self._instrument_class_temporality = instrument_class_temporality
5355
self._instrument_class_aggregation = instrument_class_aggregation
5456

5557
def _get_or_init_view_instrument_match(
@@ -80,6 +82,9 @@ def _get_or_init_view_instrument_match(
8082
view=_DEFAULT_VIEW,
8183
instrument=instrument,
8284
sdk_config=self._sdk_config,
85+
instrument_class_temporality=(
86+
self._instrument_class_temporality
87+
),
8388
instrument_class_aggregation=(
8489
self._instrument_class_aggregation
8590
),
@@ -97,9 +102,7 @@ def consume_measurement(self, measurement: Measurement) -> None:
97102
):
98103
view_instrument_match.consume_measurement(measurement)
99104

100-
def collect(
101-
self, instrument_type_temporality: Dict[type, AggregationTemporality]
102-
) -> Iterable[Metric]:
105+
def collect(self) -> Iterable[Metric]:
103106
# Use a list instead of yielding to prevent a slow reader from holding
104107
# SDK locks
105108
metrics: List[Metric] = []
@@ -117,11 +120,7 @@ def collect(
117120
view_instrument_matches
118121
) in self._instrument_view_instrument_matches.values():
119122
for view_instrument_match in view_instrument_matches:
120-
metrics.extend(
121-
view_instrument_match.collect(
122-
instrument_type_temporality
123-
)
124-
)
123+
metrics.extend(view_instrument_match.collect())
125124

126125
return metrics
127126

@@ -142,6 +141,9 @@ def _handle_view_instrument_match(
142141
view=view,
143142
instrument=instrument,
144143
sdk_config=self._sdk_config,
144+
instrument_class_temporality=(
145+
self._instrument_class_temporality
146+
),
145147
instrument_class_aggregation=(
146148
self._instrument_class_aggregation
147149
),

opentelemetry-sdk/tests/metrics/test_measurement_consumer.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from opentelemetry.sdk._metrics._internal.sdk_configuration import (
2323
SdkConfiguration,
2424
)
25-
from opentelemetry.sdk._metrics.export import AggregationTemporality
2625

2726

2827
@patch(
@@ -85,10 +84,8 @@ def test_collect_passed_to_reader_stage(self, MockMetricReaderStorage):
8584
)
8685
for r_mock, rs_mock in zip(reader_mocks, reader_storage_mocks):
8786
rs_mock.collect.assert_not_called()
88-
consumer.collect(r_mock, AggregationTemporality.CUMULATIVE)
89-
rs_mock.collect.assert_called_once_with(
90-
AggregationTemporality.CUMULATIVE
91-
)
87+
consumer.collect(r_mock)
88+
rs_mock.collect.assert_called_once_with()
9289

9390
def test_collect_calls_async_instruments(self, MockMetricReaderStorage):
9491
"""Its collect() method should invoke async instruments and pass measurements to the
@@ -108,7 +105,7 @@ def test_collect_calls_async_instruments(self, MockMetricReaderStorage):
108105
i_mock.callback.return_value = [Mock()]
109106
consumer.register_asynchronous_instrument(i_mock)
110107

111-
consumer.collect(reader_mock, AggregationTemporality.CUMULATIVE)
108+
consumer.collect(reader_mock)
112109

113110
# it should call async instruments
114111
for i_mock in async_instrument_mocks:

0 commit comments

Comments
 (0)