From c72095e1ea1600019cc869d721070be1d5fbef87 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Sun, 28 Nov 2021 17:34:23 -0600 Subject: [PATCH] Add _ViewInstrumentMatch Fixes #2296 --- .../sdk/_metrics/_view_instrument_match.py | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py index d9c21a33af8..9507aa6c3b3 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from threading import Lock from typing import Callable, Dict, List from opentelemetry.sdk._metrics.aggregation import Aggregation @@ -31,7 +32,38 @@ def __init__( exemplar_reservoir: Callable, resource: Resource, ): - pass + self._name = name + self._unit = unit + self._description = description + + if attribute_keys is None: + self._attribute_keys = set() + else: + self._attribute_keys = set(attribute_keys.items()) + + self._extra_dimensions = extra_dimensions + self._aggregation = aggregation + self._exemplar_reservoir = exemplar_reservoir + self._attributes_aggregation = {} + self._lock = Lock() def _process(self, measurement: Measurement) -> None: - pass + + if measurement.attributes is None: + attributes = {} + + else: + attributes = measurement.attributes + + attributes = frozenset( + set(attributes).difference(self._attribute_keys) + ) + + if attributes not in self._attributes_aggregation.keys(): + # FIXME how to handle aggregations that support config? + with self._lock: + self._attributes_aggregation[attributes] = self._aggregation[ + attributes + ] + + self._attributes_aggregation[attributes].aggregate(measurement.amount)