Skip to content

Commit e9d6782

Browse files
authored
ref: Tweak StreamedSpan interface (#5589)
### Description - add a `__repr__` - remove `set_x()` methods, `set_http_status` to keep API surface minimal - remove getters/setters in favor of properties where applicable (status, name) - add a warning when setting an unsupported span status value #### Issues <!-- * resolves: #1234 * resolves: LIN-1234 --> #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 4d9394d commit e9d6782

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

sentry_sdk/traces.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from enum import Enum
1010
from typing import TYPE_CHECKING
1111

12-
from sentry_sdk.consts import SPANDATA
13-
from sentry_sdk.utils import format_attribute
12+
from sentry_sdk.utils import format_attribute, logger
1413

1514
if TYPE_CHECKING:
1615
from typing import Optional, Union
@@ -71,6 +70,7 @@ class StreamedSpan:
7170
__slots__ = (
7271
"_name",
7372
"_attributes",
73+
"_active",
7474
"_span_id",
7575
"_trace_id",
7676
"_status",
@@ -81,9 +81,11 @@ def __init__(
8181
*,
8282
name: str,
8383
attributes: "Optional[Attributes]" = None,
84+
active: bool = True,
8485
trace_id: "Optional[str]" = None,
8586
):
8687
self._name: str = name
88+
self._active: bool = active
8789
self._attributes: "Attributes" = {}
8890
if attributes:
8991
for attribute, value in attributes.items():
@@ -92,8 +94,17 @@ def __init__(
9294
self._span_id: "Optional[str]" = None
9395
self._trace_id: "Optional[str]" = trace_id
9496

95-
self.set_status(SpanStatus.OK)
96-
self.set_source(SegmentSource.CUSTOM)
97+
self._status = SpanStatus.OK.value
98+
self.set_attribute("sentry.span.source", SegmentSource.CUSTOM.value)
99+
100+
def __repr__(self) -> str:
101+
return (
102+
f"<{self.__class__.__name__}("
103+
f"name={self._name}, "
104+
f"trace_id={self.trace_id}, "
105+
f"span_id={self.span_id}, "
106+
f"active={self._active})>"
107+
)
97108

98109
def get_attributes(self) -> "Attributes":
99110
return self._attributes
@@ -111,43 +122,34 @@ def remove_attribute(self, key: str) -> None:
111122
except KeyError:
112123
pass
113124

114-
def get_status(self) -> "Union[SpanStatus, str]":
115-
if self._status in {s.value for s in SpanStatus}:
116-
return SpanStatus(self._status)
117-
125+
@property
126+
def status(self) -> "str":
118127
return self._status
119128

120-
def set_status(self, status: "Union[SpanStatus, str]") -> None:
129+
@status.setter
130+
def status(self, status: "Union[SpanStatus, str]") -> None:
121131
if isinstance(status, Enum):
122132
status = status.value
123133

124-
self._status = status
125-
126-
def set_http_status(self, http_status: int) -> None:
127-
self.set_attribute(SPANDATA.HTTP_STATUS_CODE, http_status)
134+
if status not in {e.value for e in SpanStatus}:
135+
logger.debug(
136+
f'Unsupported span status {status}. Expected one of: "ok", "error"'
137+
)
138+
return
128139

129-
if http_status >= 400:
130-
self.set_status(SpanStatus.ERROR)
131-
else:
132-
self.set_status(SpanStatus.OK)
140+
self._status = status
133141

134-
def get_name(self) -> str:
142+
@property
143+
def name(self) -> str:
135144
return self._name
136145

137-
def set_name(self, name: str) -> None:
146+
@name.setter
147+
def name(self, name: str) -> None:
138148
self._name = name
139149

140-
def set_op(self, op: str) -> None:
141-
self.set_attribute("sentry.op", op)
142-
143-
def set_origin(self, origin: str) -> None:
144-
self.set_attribute("sentry.origin", origin)
145-
146-
def set_source(self, source: "Union[str, SegmentSource]") -> None:
147-
if isinstance(source, Enum):
148-
source = source.value
149-
150-
self.set_attribute("sentry.span.source", source)
150+
@property
151+
def active(self) -> bool:
152+
return self._active
151153

152154
@property
153155
def span_id(self) -> str:

0 commit comments

Comments
 (0)