Skip to content

fix(starlette): Set transaction name on current scope in sync handler - #7201

Merged
mjq merged 2 commits into
masterfrom
mjq/starlette-sync-scope-fix
Aug 24, 2026
Merged

fix(starlette): Set transaction name on current scope in sync handler#7201
mjq merged 2 commits into
masterfrom
mjq/starlette-sync-scope-fix

Conversation

@mjq

@mjq mjq commented Aug 13, 2026

Copy link
Copy Markdown
Member

The sync request/response handler passed the isolation scope to _set_transaction_name_and_source, but the transaction/segment span lives on the current scope. As a result the route-resolved name never reached the span for sync endpoints, which were instead named by the raw URL from the ASGI middleware (transaction_info.source of url rather than route). Async handlers already used the current scope and were unaffected.

Pass the current scope (already computed above) so sync and async handlers behave identically:

  • streaming: the segment name / sentry.segment.name.source are route-based
  • static: the transaction event name / source are route-based

For parametrized routes this also removes high-cardinality URL transaction names for sync endpoints.

Found while working on #7183.

The sync request/response handler passed the _isolation_ scope to
`_set_transaction_name_and_source`, but the transaction/segment span lives on
the _current_ scope. As a result the route-resolved name never reached the span
for sync endpoints, which were instead named by the raw URL from the ASGI
middleware (`transaction_info.source` of `url` rather than `route`). Async
handlers already used the current scope and were unaffected.

Pass the current scope (already computed above) so sync and async handlers
behave identically:
- streaming: the segment name / `sentry.segment.name.source` are route-based
- static: the transaction event name / source are route-based

For parametrized routes this also removes high-cardinality URL transaction
names for sync endpoints.
@mjq
mjq force-pushed the mjq/starlette-sync-scope-fix branch from e043a2f to b6107ba Compare August 13, 2026 20:44

@cursor cursor Bot left a comment

Copy link
Copy Markdown

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 b6107ba. Configure here.

@github-actions

github-actions Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

117639 passed | ⏭️ 6732 skipped | Total: 124371 | Pass Rate: 94.59% | Execution Time: 423m 38s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +4448
Passed Tests 📈 +4482
Failed Tests 📉 -1
Skipped Tests 📉 -33

➖ Removed Tests (1)

View removed tests
  • test_span_streaming_quart_auth_user_id[42-True]
    • File: tests.integrations.quart.test_quart

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2488 uncovered lines.
❌ Project coverage is 90.22%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    90.25%    90.22%    -0.03%
==========================================
  Files          193       193         —
  Lines        25433     25433         —
  Branches      9346      9346         —
==========================================
+ Hits         22952     22945        -7
- Misses        2481      2488        +7
- Partials      1436      1436         —

Generated by Codecov Action

@mjq
mjq marked this pull request as ready for review August 19, 2026 21:00
@mjq
mjq requested a review from a team as a code owner August 19, 2026 21:00

@ericapisani ericapisani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great catch, and thanks for making these changes. Very minor suggestion for the span streaming test, but at the core this LGTM.

Thanks for also separating span streaming enabled and the transactions approach into 2 separate tests - will make it easier when we remove the transactions code 🔥

Comment thread tests/integrations/starlette/test_starlette.py Outdated
Comment thread tests/integrations/starlette/test_starlette.py Outdated
@mjq
mjq force-pushed the mjq/starlette-sync-scope-fix branch from e7bbe5e to 4b5d7f3 Compare August 24, 2026 16:41
@mjq
mjq merged commit 9147b3c into master Aug 24, 2026
269 of 272 checks passed
@mjq
mjq deleted the mjq/starlette-sync-scope-fix branch August 24, 2026 17:14
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.

2 participants