status.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. import enum
  4. import logging
  5. logger = logging.getLogger(__name__)
  6. class StatusCode(enum.Enum):
  7. """Represents the canonical set of status codes of a finished Span."""
  8. UNSET = 0
  9. """The default status."""
  10. OK = 1
  11. """The operation has been validated by an Application developer or Operator to have completed successfully."""
  12. ERROR = 2
  13. """The operation contains an error."""
  14. class Status:
  15. """Represents the status of a finished Span.
  16. Args:
  17. status_code: The canonical status code that describes the result
  18. status of the operation.
  19. description: An optional description of the status.
  20. """
  21. def __init__(
  22. self,
  23. status_code: StatusCode = StatusCode.UNSET,
  24. description: str | None = None,
  25. ):
  26. self._status_code = status_code
  27. self._description = None
  28. if description:
  29. if not isinstance(description, str):
  30. logger.warning("Invalid status description type, expected str")
  31. return
  32. if status_code is not StatusCode.ERROR:
  33. logger.warning(
  34. "description should only be set when status_code is set to StatusCode.ERROR"
  35. )
  36. return
  37. self._description = description
  38. @property
  39. def status_code(self) -> StatusCode:
  40. """Represents the canonical status code of a finished Span."""
  41. return self._status_code
  42. @property
  43. def description(self) -> str | None:
  44. """Status description"""
  45. return self._description
  46. @property
  47. def is_ok(self) -> bool:
  48. """Returns false if this represents an error, true otherwise."""
  49. return self.is_unset or self._status_code is StatusCode.OK
  50. @property
  51. def is_unset(self) -> bool:
  52. """Returns true if unset, false otherwise."""
  53. return self._status_code is StatusCode.UNSET