Skip to content

Rework like in js

2243832
Select commit
Loading
Failed to load commit list.
Open

feat(span-first): Support before_send_span #6239

Rework like in js
2243832
Select commit
Loading
Failed to load commit list.
@sentry/warden / warden: find-bugs completed May 11, 2026 in 4m 56s

1 issue

find-bugs: Found 1 issue (1 medium)

Medium

_estimate_size raises KeyError when span lacks attributes - `sentry_sdk/_span_batcher.py:151`

_estimate_size accesses item["attributes"].values() unconditionally, but 'attributes' is NotRequired in SpanJSON. The new before_send_span feature allows users to return a modified dict. While the intent is that invalid returns should fall back to the original span, the validation at client.py:979 only checks isinstance(serialized, dict) and serialized (non-empty). This allows dicts like {"not_a_span": True} to pass validation. When such a dict reaches _span_batcher.add(), it will raise KeyError in _estimate_size at line 151, causing the span to be dropped and potentially propagating the exception.

Also found at:

  • sentry_sdk/_span_batcher.py:165-170
  • sentry_sdk/client.py:978-986

⏱ 1m 17s · 795.1k in / 9.9k out · $4.48 (+verification: $3.35, +merge: $0.00)

Annotations

Check warning on line 151 in sentry_sdk/_span_batcher.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: find-bugs

_estimate_size raises KeyError when span lacks attributes

_estimate_size accesses item["attributes"].values() unconditionally, but 'attributes' is NotRequired in SpanJSON. The new before_send_span feature allows users to return a modified dict. While the intent is that invalid returns should fall back to the original span, the validation at client.py:979 only checks isinstance(serialized, dict) and serialized (non-empty). This allows dicts like {"not_a_span": True} to pass validation. When such a dict reaches _span_batcher.add(), it will raise KeyError in _estimate_size at line 151, causing the span to be dropped and potentially propagating the exception.

Check warning on line 170 in sentry_sdk/_span_batcher.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: find-bugs

[VPT-R9G] _estimate_size raises KeyError when span lacks attributes (additional location)

_estimate_size accesses item["attributes"].values() unconditionally, but 'attributes' is NotRequired in SpanJSON. The new before_send_span feature allows users to return a modified dict. While the intent is that invalid returns should fall back to the original span, the validation at client.py:979 only checks isinstance(serialized, dict) and serialized (non-empty). This allows dicts like {"not_a_span": True} to pass validation. When such a dict reaches _span_batcher.add(), it will raise KeyError in _estimate_size at line 151, causing the span to be dropped and potentially propagating the exception.

Check warning on line 986 in sentry_sdk/client.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: find-bugs

[VPT-R9G] _estimate_size raises KeyError when span lacks attributes (additional location)

_estimate_size accesses item["attributes"].values() unconditionally, but 'attributes' is NotRequired in SpanJSON. The new before_send_span feature allows users to return a modified dict. While the intent is that invalid returns should fall back to the original span, the validation at client.py:979 only checks isinstance(serialized, dict) and serialized (non-empty). This allows dicts like {"not_a_span": True} to pass validation. When such a dict reaches _span_batcher.add(), it will raise KeyError in _estimate_size at line 151, causing the span to be dropped and potentially propagating the exception.