Skip to content

Commit dee861e

Browse files
authored
feat(span-first): Better span first estimates (#5936)
We were underestimating the size of spans with particularly bulky attributes, and flushing the buffer too late as a result. Take attribute size into account for estimating.
1 parent c97e572 commit dee861e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

sentry_sdk/_span_batcher.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,21 @@ def add(self, span: "StreamedSpan") -> None:
9191
@staticmethod
9292
def _estimate_size(item: "StreamedSpan") -> int:
9393
# Rough estimate of serialized span size that's quick to compute.
94-
# 210 is the rough size of the payload without attributes, and we
95-
# estimate additional 70 bytes on top of that per attribute.
96-
return 210 + 70 * len(item._attributes)
94+
# 210 is the rough size of the payload without attributes, and then we
95+
# estimate the attributes separately.
96+
estimate = 210
97+
for value in item._attributes.values():
98+
estimate += 50
99+
100+
if isinstance(value, str):
101+
estimate += len(value)
102+
else:
103+
estimate += len(str(value))
104+
105+
return estimate
97106

98107
@staticmethod
99108
def _to_transport_format(item: "StreamedSpan") -> "Any":
100-
# TODO[span-first]
101109
res: "dict[str, Any]" = {
102110
"trace_id": item.trace_id,
103111
"span_id": item.span_id,
@@ -126,7 +134,7 @@ def _flush(self) -> None:
126134
return
127135

128136
envelopes = []
129-
for trace_id, spans in self._span_buffer.items():
137+
for spans in self._span_buffer.values():
130138
if spans:
131139
dsc = spans[0]._dynamic_sampling_context()
132140

0 commit comments

Comments
 (0)