Skip to content

feat: Make ASGI support span first - #5680

Merged
sentrivana merged 8 commits into
masterfrom
ivana/move-asgi-to-span-first
Mar 19, 2026
Merged

sentrivana merged 8 commits into
masterfrom
ivana/move-asgi-to-span-first

Conversation

@sentrivana

@sentrivana sentrivana commented Mar 16, 2026 •

Copy link
Copy Markdown
Collaborator

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

Best reviewed with whitespace ignored: https://github.com/getsentry/sentry-python/pull/5680/changes?w=1


A bit of a background on migrating integrations to span first. In order to support both legacy spans and span streaming, most integrations will follow the same patterns:

API

We need to use the start_span API from sentry_sdk.traces if we're in span streaming mode (traces_lifecycle="stream").

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

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

github-actions Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

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


This PR will not appear in the changelog.


🤖 This preview updates automatically when you update the PR.

@github-actions

github-actions Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Codecov Results 📊

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

All tests are passing successfully.

❌ Patch coverage is 6.25%. Project has 14372 uncovered lines.

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

Generated by Codecov Action

Comment thread sentry_sdk/integrations/asgi.py Resolved
@sentrivana
sentrivana marked this pull request as ready for review March 18, 2026 14:58
@sentrivana
sentrivana requested a review from a team as a code owner March 18, 2026 14:58

@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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment thread sentry_sdk/integrations/asgi.py Resolved
@sentrivana
sentrivana merged commit e103926 into master Mar 19, 2026
158 checks passed
@sentrivana
sentrivana deleted the ivana/move-asgi-to-span-first branch March 19, 2026 10:15
mgaligniana pushed a commit to mgaligniana/sentry-python that referenced this pull request Aug 9, 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).

Best reviewed with whitespace ignored:
https://github.com/getsentry/sentry-python/pull/5680/changes?w=1

---

A bit of a background on migrating integrations to span first. In order
to support both legacy spans and span streaming, most integrations will
follow the same patterns:

### API

We need to use the `start_span` API from `sentry_sdk.traces` if we're in
span streaming mode (`traces_lifecycle="stream"`).

There are no transactions anymore. Top-level spans will also be started
via the `start_span` API in span streaming mode.

### 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](getsentry#5152).

### 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()`](https://sentry-docs-git-ivana-span-first-migration-guide.sentry.dev/platforms/python/migration/span-first/#trace-propagation)
(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:
- Making sure all attributes are correct and that they're set in Sentry
conventions: getsentry#5152
- Migrating event processors (as streaming spans are not events, event
processors are not run on them, meaning some data will not be set yet):
getsentry#5152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants