From 4e0b705d879d05616a3797323da79a6887944c03 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Wed, 29 Sep 2021 12:13:16 -0400 Subject: [PATCH] Make measurement a concrete class (#2153) * Make Measurement a concrete class * comments * update changelog --- .../src/opentelemetry/metrics/measurement.py | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/metrics/measurement.py b/opentelemetry-api/src/opentelemetry/metrics/measurement.py index 6b5b081c266..e8fd9725bb8 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/measurement.py +++ b/opentelemetry-api/src/opentelemetry/metrics/measurement.py @@ -12,28 +12,41 @@ # See the License for the specific language governing permissions and # limitations under the License. -# pylint: disable=too-many-ancestors -# type:ignore +from typing import Union +from opentelemetry.util.types import Attributes -from abc import ABC, abstractmethod +class Measurement: + """A measurement observed in an asynchronous instrument + + Return/yield instances of this class from asynchronous instrument callbacks. + + Args: + value: The float or int measured value + attributes: The measurement's attributes + """ + + def __init__( + self, value: Union[int, float], attributes: Attributes = None + ) -> None: + self._value = value + self._attributes = attributes -class Measurement(ABC): @property - def value(self): + def value(self) -> Union[float, int]: return self._value @property - def attributes(self): + def attributes(self) -> Attributes: return self._attributes - @abstractmethod - def __init__(self, value, attributes=None): - self._value = value - self._attributes = attributes - + def __eq__(self, other: object) -> bool: + return ( + isinstance(other, Measurement) + and self.value == other.value + and self.attributes == other.attributes + ) -class DefaultMeasurement(Measurement): - def __init__(self, value, attributes=None): - super().__init__(value, attributes=attributes) + def __repr__(self) -> str: + return f"Measurement(value={self.value}, attributes={self.attributes})"