Skip to content

[do not merge] feat: Span streaming & new span API#5551

Draft
sentrivana wants to merge 202 commits intomasterfrom
feat/span-first
Draft

[do not merge] feat: Span streaming & new span API#5551
sentrivana wants to merge 202 commits intomasterfrom
feat/span-first

Conversation

@sentrivana
Copy link
Contributor

Introduce a new start_span() API with a simpler and more intuitive signature to eventually replace the original start_span() and start_transaction() APIs.

Additionally, introduce a new streaming mode (sentry_sdk.init(_experiments={"trace_lifecycle": "stream"})) that will send spans as they finish, rather than by transaction.

import sentry_sdk

sentry_sdk.init(
    _experiments={"trace_lifecycle": "stream"},
)

with sentry_sdk.traces.start_span(name="my_span"):
    ...

The new API MUST be used with the new streaming mode, and the old API MUST be used in the legacy non-streaming (static) mode.

Migration guide: getsentry/sentry-docs#16072

Notes

  • The diff is huge mostly because I've optimized for easy removal of legacy code in the next major, deliberately duplicating a lot. I'll of course split it up to reviewable PRs once ready.
    • Chose to go with a new file and a new span class so that we can just remove the old Span and drop the new StreamedSpan in tracing.py as a replacement.
  • The batcher for spans is a bit different from the logs and metrics batchers because it needs to batch by trace_id (we can't send spans from different traces in the same envelope).

Release Plan

  • There will be prereleases for internal testing.
  • We'll release the new API in a minor version as opt-in.
  • In the next major, we'll drop the legacy API.

Project

https://linear.app/getsentry/project/span-first-sdk-python-727da28dd037/overview

Comment on lines +55 to +58
if isinstance(span, Span):
set_on_span = span.set_attribute
else:
set_on_span = span.set_data
Copy link

@sentry-warden sentry-warden bot Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_attribute method does not exist on Span class, will raise AttributeError

The code assigns set_on_span = span.set_attribute when the span is an instance of Span, but the Span class (in sentry_sdk/tracing.py) does not have a set_attribute method. This will cause an AttributeError at runtime when _record_exception_on_span is called with a legacy Span object. The logic appears to be inverted - Span has set_data() while StreamedSpan has set_attribute().

Verification

Verified by reading sentry_sdk/tracing.py (Span class definition at line 225) - it has set_data() at line 590 but no set_attribute method. Confirmed via grep that 'set_attribute' has no matches in tracing.py. Also verified sentry_sdk/traces.py where StreamedSpan class at line 216 has set_attribute() at line 410.

Suggested fix: Swap the method assignments - use set_data for Span and set_attribute for StreamedSpan

Suggested change
if isinstance(span, Span):
set_on_span = span.set_attribute
else:
set_on_span = span.set_data
set_on_span = span.set_data
set_on_span = span.set_attribute
Also found at 1 additional location
  • sentry_sdk/integrations/sqlalchemy.py:102-102

Identified by Warden code-review, find-bugs · LS6-SGE

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix attempt detected (commit 4b2147d)

The fix attempted to address the inverted logic but introduced the same error in reverse - it assigns set_attribute to Span (which doesn't have this method) and set_data to StreamedSpan (which has set_attribute), so the AttributeError on Span objects will still occur at runtime.

The original issue appears unresolved. Please review and try again.

Evaluated by Warden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants