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