status.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import enum
  15. import logging
  16. import typing
  17. logger = logging.getLogger(__name__)
  18. class StatusCode(enum.Enum):
  19. """Represents the canonical set of status codes of a finished Span."""
  20. UNSET = 0
  21. """The default status."""
  22. OK = 1
  23. """The operation has been validated by an Application developer or Operator to have completed successfully."""
  24. ERROR = 2
  25. """The operation contains an error."""
  26. class Status:
  27. """Represents the status of a finished Span.
  28. Args:
  29. status_code: The canonical status code that describes the result
  30. status of the operation.
  31. description: An optional description of the status.
  32. """
  33. def __init__(
  34. self,
  35. status_code: StatusCode = StatusCode.UNSET,
  36. description: typing.Optional[str] = None,
  37. ):
  38. self._status_code = status_code
  39. self._description = None
  40. if description:
  41. if not isinstance(description, str):
  42. logger.warning("Invalid status description type, expected str")
  43. return
  44. if status_code is not StatusCode.ERROR:
  45. logger.warning(
  46. "description should only be set when status_code is set to StatusCode.ERROR"
  47. )
  48. return
  49. self._description = description
  50. @property
  51. def status_code(self) -> StatusCode:
  52. """Represents the canonical status code of a finished Span."""
  53. return self._status_code
  54. @property
  55. def description(self) -> typing.Optional[str]:
  56. """Status description"""
  57. return self._description
  58. @property
  59. def is_ok(self) -> bool:
  60. """Returns false if this represents an error, true otherwise."""
  61. return self.is_unset or self._status_code is StatusCode.OK
  62. @property
  63. def is_unset(self) -> bool:
  64. """Returns true if unset, false otherwise."""
  65. return self._status_code is StatusCode.UNSET