Skip to content

feat: Make ASGI support span first#5680

Draft
sentrivana wants to merge 2 commits intomasterfrom
ivana/move-asgi-to-span-first
Draft

feat: Make ASGI support span first#5680
sentrivana wants to merge 2 commits intomasterfrom
ivana/move-asgi-to-span-first

Conversation

@sentrivana
Copy link
Contributor

@sentrivana sentrivana commented Mar 16, 2026

This PR makes the ASGI integration work both in legacy and span streaming mode. Some features and attributes will be missing in span streaming mode for now (see the Out of scope section below).


Migrating integrations

In order to support both legacy spans and span streaming, most integrations will follow the same patterns:

Transactions are gone

There are no transactions anymore. Top-level spans will also be started via the start_span API.

Setting data on spans

If an integration sets data on a span (via span.set_data, span.set_tag etc.), it should use span.set_attribute when span streaming is enabled.

The attributes that we set need to be in Sentry conventions. This is deliberately not the case for most quick ports of integrations like this one and will follow in a future step.

Trace propagation

If an integration sits at a service boundary and is capable of propagating incoming trace information (like WSGI/ASGI or Celery), in span first mode we need to switch from the old style with continue_trace(...) as transaction: to the new style continue_trace() and new_trace() (not context managers).

start_span arguments

You can pass things like op, origin, source to the old start_span API. With the new API, this is no longer possible, and the individual properties need to be set as attributes directly.

Out of scope

For now, the following is out of scope and will follow in the future:

@github-actions
Copy link
Contributor

github-actions bot commented Mar 16, 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 ✨

Anthropic

  • Emit gen_ai.chat spans for asynchronous messages.stream() by alexander-alderman-webb in #5572
  • Emit AI Client Spans for synchronous messages.stream() by alexander-alderman-webb in #5565
  • Set gen_ai.response.id span attribute by ericapisani in #5662
  • Add gen_ai.system attribute to spans by ericapisani in #5661

Pydantic Ai

  • Support ImageUrl content type in span instrumentation by ericapisani in #5629
  • Add tool description to execute_tool spans by ericapisani in #5596

Other

  • (crons) Add owner field to MonitorConfig by julwhitney13 in #5610
  • (otlp) Add collector_url option to OTLPIntegration by sl0thentr0py in #5603
  • Make ASGI support span first by sentrivana in #5680

Bug Fixes 🐛

  • (ai) Truncate list-based message content in AI monitoring by ericapisani in #5631
  • (anthropic) Close span on GeneratorExit by alexander-alderman-webb in #5643
  • (celery) Propagate user-set headers by sentrivana in #5581
  • (langchain) Wrap finish_reason in array for gen_ai span attribute by ericapisani in #5666
  • (profiler) Prevent buffer race condition during rapid start/stop cycles by ericapisani in #5622
  • (utils) Avoid double serialization of strings in safe_serialize by ericapisani in #5587
  • Enable unused import ruff check and fix unused imports by sentrivana in #5652

Documentation 📚

  • (openai-agents) Remove inapplicable comment by alexander-alderman-webb in #5495
  • Add AGENTS.md by sentrivana in #5579
  • Add set_attribute example to changelog by sentrivana in #5578

Internal Changes 🔧

Anthropic

  • Check system and response ID attributes on spans created by stream() by alexander-alderman-webb in #5665
  • Skip accumulation logic for unexpected types in streamed response by alexander-alderman-webb in #5564
  • Factor out streamed result handling by alexander-alderman-webb in #5563
  • Stream valid JSON by alexander-alderman-webb in #5641
  • Stop mocking response iterator by alexander-alderman-webb in #5573

Docs

  • Remove agentic codebase documentation workflows by dingsdax in #5655
  • Switch agentic workflows from Copilot to Claude engine by dingsdax in #5654
  • Add agentic workflows for codebase documentation by dingsdax in #5649

Openai Agents

  • Do not fail on new tool fields by alexander-alderman-webb in #5625
  • Stop expecting a specific function name by alexander-alderman-webb in #5623
  • Set streaming header when library uses with_streaming_response() by alexander-alderman-webb in #5583
  • Replace mocks with httpx for streamed responses by alexander-alderman-webb in #5580
  • Replace mocks with httpx in non-MCP tool tests by alexander-alderman-webb in #5602
  • Replace mocks with httpx in MCP tool tests by alexander-alderman-webb in #5605
  • Replace mocks with httpx in handoff tests by alexander-alderman-webb in #5604
  • Replace mocks with httpx in API error test by alexander-alderman-webb in #5601
  • Replace mocks with httpx in non-error single-response tests by alexander-alderman-webb in #5600
  • Remove test for unreachable state by alexander-alderman-webb in #5584
  • Expect namespace tool field for new openai versions by alexander-alderman-webb in #5599

Other

  • (graphene) Simplify span creation by sentrivana in #5648
  • (httpx) Resolve type checking failures by alexander-alderman-webb in #5626
  • (pyramid) Support alpha suffixes in version parsing by alexander-alderman-webb in #5618
  • (rust) Don't implement separate scope management by sentrivana in #5639
  • (strawberry) Simplify span creation by sentrivana in #5647
  • 🤖 Update test matrix with new releases (03/16) by github-actions in #5671
  • Remove custom warden action by sentrivana in #5653
  • Add httpx to linting requirements by alexander-alderman-webb in #5644
  • Remove CodeQL action by sentrivana in #5616
  • Normalize dots in package names in populate_tox.py by alexander-alderman-webb in #5574
  • Do not run actions on potel-base by sentrivana in #5614

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 16, 2026

Codecov Results 📊

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

All tests are passing successfully.

❌ Patch coverage is 6.06%. Project has 14293 uncovered lines.

Files with missing lines (1)
File Patch % Lines
asgi.py 18.63% ⚠️ 131 Missing

Generated by Codecov Action

Comment on lines +237 to +240
segment = sentry_sdk.traces.start_span(
name=transaction_name, attributes=attributes
)
span_ctx = segment or nullcontext()
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.

Span streaming mode starts spans for HTTP requests not in http_methods_to_capture

In span streaming mode, when ty is 'http' and the method is NOT in http_methods_to_capture, a span is still created (line 237-238). In contrast, the legacy mode correctly skips transaction creation, leaving transaction = None and using nullcontext(). This behavioral difference means span streaming will generate spans for filtered-out HTTP methods (e.g., OPTIONS, HEAD) when users explicitly configured http_methods_to_capture to exclude them, causing unexpected tracing overhead and data noise.

Verification

Traced both code paths in _run_app: Legacy mode (lines 242-274) only creates a transaction when ty is http/websocket AND method is in http_methods_to_capture, otherwise transaction stays None. Span streaming mode (lines 217-240) always creates a span at line 237 regardless of whether the inner condition (lines 222-227) was met. Read full asgi.py file and traces.py to confirm StreamedSpan behavior.

Identified by Warden code-review, find-bugs · KTW-XF2

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 a4dbe50)

The span streaming mode still calls sentry_sdk.traces.start_span() unconditionally on line 237 regardless of whether the HTTP method is in http_methods_to_capture, meaning spans are still created for filtered HTTP methods despite the conditional logic checking the method filter.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant