observation.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. from opentelemetry.context import Context
  4. from opentelemetry.util.types import Attributes
  5. class Observation:
  6. """A measurement observed in an asynchronous instrument
  7. Return/yield instances of this class from asynchronous instrument callbacks.
  8. Args:
  9. value: The float or int measured value
  10. attributes: The measurement's attributes
  11. context: The measurement's context
  12. """
  13. def __init__(
  14. self,
  15. value: int | float,
  16. attributes: Attributes = None,
  17. context: Context | None = None,
  18. ) -> None:
  19. self._value = value
  20. self._attributes = attributes
  21. self._context = context
  22. @property
  23. def value(self) -> float | int:
  24. return self._value
  25. @property
  26. def attributes(self) -> Attributes:
  27. return self._attributes
  28. @property
  29. def context(self) -> Context | None:
  30. return self._context
  31. def __eq__(self, other: object) -> bool:
  32. return (
  33. isinstance(other, Observation)
  34. and self.value == other.value
  35. and self.attributes == other.attributes
  36. and self.context == other.context
  37. )
  38. def __repr__(self) -> str:
  39. return f"Observation(value={self.value}, attributes={self.attributes}, context={self.context})"