ref: Introduce _is_sampled_streamed_span() helper to replace repeated check#6180
ref: Introduce _is_sampled_streamed_span() helper to replace repeated check#6180ericapisani wants to merge 1 commit intomasterfrom
_is_sampled_streamed_span() helper to replace repeated check#6180Conversation
…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`.
|
bugbot run |
|
@sentry review |
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)
Generated by Codecov Action |
There was a problem hiding this comment.
✅ 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.
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
It looks like in blocks gated by not isinstance(current_span, NoOpStreamedSpan) we rely on private attributes and functions of StreamedSpan such as _segment and _is_segment().
If we're relying on private details that we expect to only exist on StreamedSpan (and not subclasses) it would be more appropriate to replace logic like
isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan)with
type(span) is StreamedSpanThere was a problem hiding this comment.
Wondering if it'd be better to replace
isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan)with
isinstance(span, StreamedSpan) and span.sampledBut I assume that might cause mypy to complain about nonexistent methods on NoOpStreamedSpans since it won't be able to realize the condition effectively means it can't be a NoOpStreamedSpan?
Edit: Only saw Alex's comment above now. That also sounds good.
|
Re: using the I think it depends on how strict we want to be with ensuring that the only spans that pass this are I wrote this check with Re: using the
We would achieve the same outcome using the |
The spans touched by the filtering logic are created by the SDK, so we know that they are of type |
The
isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan)pattern was repeated in multiple places. Omitting theNoOpStreamedSpanguard is a frequent code review catch — it silently treats unsampled spans as sampled.Introduce
_is_sampled_streamed_span()intraces.pywith aTypeGuard[StreamedSpan]return type for proper type narrowing, and replace all combined-check instances inscope.py,starlette.py, andfastapi.py.Locations where
_spancan also be a regularSpan(not justStreamedSpan) were intentionally left unchanged, as the semantics differ there.Fixes PY-2397
Fixes #6182