observation.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright The OpenTelemetry Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Optional, Union
  15. from opentelemetry.context import Context
  16. from opentelemetry.util.types import Attributes
  17. class Observation:
  18. """A measurement observed in an asynchronous instrument
  19. Return/yield instances of this class from asynchronous instrument callbacks.
  20. Args:
  21. value: The float or int measured value
  22. attributes: The measurement's attributes
  23. context: The measurement's context
  24. """
  25. def __init__(
  26. self,
  27. value: Union[int, float],
  28. attributes: Attributes = None,
  29. context: Optional[Context] = None,
  30. ) -> None:
  31. self._value = value
  32. self._attributes = attributes
  33. self._context = context
  34. @property
  35. def value(self) -> Union[float, int]:
  36. return self._value
  37. @property
  38. def attributes(self) -> Attributes:
  39. return self._attributes
  40. @property
  41. def context(self) -> Optional[Context]:
  42. return self._context
  43. def __eq__(self, other: object) -> bool:
  44. return (
  45. isinstance(other, Observation)
  46. and self.value == other.value
  47. and self.attributes == other.attributes
  48. and self.context == other.context
  49. )
  50. def __repr__(self) -> str:
  51. return f"Observation(value={self.value}, attributes={self.attributes}, context={self.context})"