Skip to content

feat(fastapi): Support span streaming in active thread tracking#6118

Merged
ericapisani merged 2 commits intomasterfrom
ep/py-2322-span-first-fastapi-7a5
Apr 21, 2026
Merged

feat(fastapi): Support span streaming in active thread tracking#6118
ericapisani merged 2 commits intomasterfrom
ep/py-2322-span-first-fastapi-7a5

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented Apr 21, 2026

Support span-first functionality in the FastAPI integration's active thread tracking, while continuing to support the legacy transaction-based path.

When the span streaming experiment (trace_lifecycle: "stream") is enabled, current_scope.transaction is None and the scope instead holds a StreamedSpan. The existing profiler hook in patch_get_request_handler only called current_scope.transaction.update_active_thread(), so under streaming the profiler's thread.id was never attached to the segment. This PR detects the StreamedSpan case and calls _update_active_thread() on its segment; the legacy transaction path is preserved unchanged.

Tests are parametrized across streaming and static modes where relevant, and a dedicated test asserts that the active thread id ends up on the segment's attributes when streaming is enabled.

Refs PY-2322
Fixes #6020

When the span streaming experiment is enabled, the current scope has a
StreamedSpan instead of a legacy transaction. Detect this case and call
the segment's _update_active_thread() so the profiler's active thread
id is recorded on the segment in span-first mode.

Also parametrize existing tests over streaming/static modes and add a
dedicated test asserting the active thread id is attached to the
segment when streaming.

Refs PY-2322
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Apr 21, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 21, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (ci) Cancel in-progress PR workflows on new commit push by joshuarli in #5994
  • (consts) Add updated span convention constants to SPANDATA by ericapisani in #6093
  • (fastapi) Support span streaming in active thread tracking by ericapisani in #6118
  • (httpx) Migrate to span first by ericapisani in #6084
  • Add db.driver.name spans to database integrations by ericapisani in #6082

Bug Fixes 🐛

  • (google_genai) Redact binary data in inline_data and fix multi-part message extraction by ericapisani in #5977
  • (grpc) Add isolation_scope to async server interceptor by robinvd in #5940
  • (openai-agents) Resolve agent from bindings for openai-agents >= 0.14 by ericapisani in #6102
  • (profiler) Stop nulling buffer on teardown by ericapisani in #6075

Internal Changes 🔧

  • (celery) Remove unused NoOpMgr from utils by sentrivana in #6078
  • (ci) Update outdated pinned action version comments by JoshuaMoelans in #6088
  • (pydantic-ai) Remove dead Model.request patch by alexander-alderman-webb in #5956
  • (tests) Replace deprecated enable_tracingwith traces_sample_rate by sentrivana in #6077
  • 🤖 Update test matrix with new releases (04/20) by github-actions in #6100
  • Set explicit base-branch for codecov action by ericapisani in #5992

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 21, 2026

Codecov Results 📊

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

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 16.67%. Project has 14778 uncovered lines.
✅ Project coverage is 31.26%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
fastapi.py 16.09% ⚠️ 73 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    31.26%    31.26%        —%
==========================================
  Files          190       190         —
  Lines        21493     21498        +5
  Branches      7138      7140        +2
==========================================
+ Hits          6719      6720        +1
- Misses       14774     14778        +4
- Partials       559       559         —

Generated by Codecov Action

Comment thread sentry_sdk/integrations/fastapi.py Outdated
@ericapisani ericapisani marked this pull request as ready for review April 21, 2026 13:57
@ericapisani ericapisani requested a review from a team as a code owner April 21, 2026 13:57
@ericapisani ericapisani merged commit e882029 into master Apr 21, 2026
159 checks passed
@ericapisani ericapisani deleted the ep/py-2322-span-first-fastapi-7a5 branch April 21, 2026 14:18
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.

Migrate fastapi to span first

2 participants