1# Copyright The OpenTelemetry Authors
2# SPDX-License-Identifier: Apache-2.0
3
4import enum
5import logging
6
7logger = logging.getLogger(__name__)
8
9
10class StatusCode(enum.Enum):
11 """Represents the canonical set of status codes of a finished Span."""
12
13 UNSET = 0
14 """The default status."""
15
16 OK = 1
17 """The operation has been validated by an Application developer or Operator to have completed successfully."""
18
19 ERROR = 2
20 """The operation contains an error."""
21
22
23class Status:
24 """Represents the status of a finished Span.
25
26 Args:
27 status_code: The canonical status code that describes the result
28 status of the operation.
29 description: An optional description of the status.
30 """
31
32 def __init__(
33 self,
34 status_code: StatusCode = StatusCode.UNSET,
35 description: str | None = None,
36 ):
37 self._status_code = status_code
38 self._description = None
39
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
50 self._description = description
51
52 @property
53 def status_code(self) -> StatusCode:
54 """Represents the canonical status code of a finished Span."""
55 return self._status_code
56
57 @property
58 def description(self) -> str | None:
59 """Status description"""
60 return self._description
61
62 @property
63 def is_ok(self) -> bool:
64 """Returns false if this represents an error, true otherwise."""
65 return self.is_unset or self._status_code is StatusCode.OK
66
67 @property
68 def is_unset(self) -> bool:
69 """Returns true if unset, false otherwise."""
70 return self._status_code is StatusCode.UNSET