Skip to content

ref: Introduce _is_sampled_streamed_span() helper to replace repeated check#6180

Open
ericapisani wants to merge 1 commit intomasterfrom
create-sampled-span-helper-0doa2
Open

ref: Introduce _is_sampled_streamed_span() helper to replace repeated check#6180
ericapisani wants to merge 1 commit intomasterfrom
create-sampled-span-helper-0doa2

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented May 1, 2026

The isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan) pattern was repeated in multiple places. Omitting the NoOpStreamedSpan guard is a frequent code review catch — it silently treats unsampled spans as sampled.

Introduce _is_sampled_streamed_span() in traces.py with a TypeGuard[StreamedSpan] return type for proper type narrowing, and replace all combined-check instances in scope.py, starlette.py, and fastapi.py.

Locations where _span can also be a regular Span (not just StreamedSpan) were intentionally left unchanged, as the semantics differ there.

Fixes PY-2397
Fixes #6182

…ed check

The `isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan)`
pattern was scattered across the codebase and easy to get wrong — forgetting the
`NoOpStreamedSpan` guard is a recurring code review catch.

Extract it into a single `_is_sampled_streamed_span()` helper in `traces.py` with
a `TypeGuard[StreamedSpan]` return type for proper type narrowing, and replace all
instances of the combined check in `scope.py`, `starlette.py`, and `fastapi.py`.
@ericapisani
Copy link
Copy Markdown
Member Author

bugbot run

@ericapisani
Copy link
Copy Markdown
Member Author

@sentry review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 12.39s

All tests are passing successfully.

❌ Patch coverage is 72.73%. Project has 14939 uncovered lines.

Files with missing lines (4)
File Patch % Lines
starlette.py 5.61% ⚠️ 370 Missing
scope.py 71.73% ⚠️ 253 Missing and 84 partials
traces.py 69.48% ⚠️ 94 Missing and 21 partials
fastapi.py 15.96% ⚠️ 79 Missing

Generated by Codecov Action

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 10e96aa. Configure here.

@ericapisani ericapisani marked this pull request as ready for review May 1, 2026 16:16
@ericapisani ericapisani requested a review from a team as a code owner May 1, 2026 16:16
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 1, 2026

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create helper method to replace frequent "is streamed span but not a noop streamed span" check

1 participant