Skip to content

feat(aiohttp): Add span streaming support#6179

Open
ericapisani wants to merge 9 commits intomasterfrom
py-2298-migrate-aiohttp-to-span-first
Open

feat(aiohttp): Add span streaming support#6179
ericapisani wants to merge 9 commits intomasterfrom
py-2298-migrate-aiohttp-to-span-first

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented Apr 30, 2026

Migrate the aiohttp integration to support the new span-first streaming model alongside the existing transaction-based approach.

Fixes PY-2298
Fixes #5996

Migrate the aiohttp integration to support the new span-first
streaming model alongside the existing transaction-based approach.
When trace_lifecycle is set to "stream", server and client spans use
the StreamedSpan API with OTel-style attributes. The legacy path
remains unchanged.

Also adds AnnotatedValue.substituted_because_over_size_limit() for
body-size-limit annotations and comprehensive tests covering headers,
request bodies, transaction styles, error paths, and trace propagation
under span streaming. It's being introduced in #6178 but added here
in order to be able to start leveraging it right away.
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Apr 30, 2026

@ericapisani
Copy link
Copy Markdown
Member Author

bugbot run

@ericapisani
Copy link
Copy Markdown
Member Author

@sentry review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 30, 2026

Codecov Results 📊

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

All tests are passing successfully.

❌ Patch coverage is 10.64%. Project has 14991 uncovered lines.

Files with missing lines (2)
File Patch % Lines
aiohttp.py 9.55% ⚠️ 199 Missing
_types.py 65.79% ⚠️ 13 Missing

Generated by Codecov Action

Comment thread sentry_sdk/integrations/aiohttp.py Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

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 2dc9255. Configure here.

Comment thread sentry_sdk/integrations/aiohttp.py Outdated
Comment thread sentry_sdk/integrations/aiohttp.py Outdated
Comment thread sentry_sdk/integrations/aiohttp.py Outdated
Comment on lines +157 to +170
url_query = (
{"url.query": request.query_string}
if request.query_string
else {}
)

client_address = (
{
"client.address": request.remote,
"user.ip_address": request.remote,
}
if should_send_default_pii() and request.remote
else {}
)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This pattern was followed so that if there are no values in the query string or in the client address, we are not adding empty strings to the envelope.

raise
except (asyncio.CancelledError, ConnectionResetError):
if isinstance(span, StreamedSpan):
span.status = SpanStatus.ERROR.value
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note that despite a NoOpStreamedSpan doing nothing when status is set on it, this is fine because ultimately these types of spans aren't sampled or are being ignored.

)
if body_data is not None:
span._segment.set_attribute(
"http.request.body.data", body_data
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As this is currently written, there is a potential for this to be an empty string instead of a value - specifically in the case of when an AnnotatedValue was returned in get_aiohttp_request_data.

There's a pull request that's open in order to ensure that this gets a value set on it in those situations, which will also have the benefit of making it clearer to users on why a value was replaced (i.e. too large, raw data that was present)

Comment on lines +394 to +395
legacy_span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
legacy_span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These were not updated from http to url in order to preserve existing behaviour

Comment thread sentry_sdk/_types.py
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The changes here were added in from #6178 so they could be used right away

@ericapisani ericapisani marked this pull request as ready for review May 1, 2026 19:20
@ericapisani ericapisani requested a review from a team as a code owner May 1, 2026 19:20
Copy link
Copy Markdown

@cursor cursor Bot left a comment

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.

Reviewed by Cursor Bugbot for commit 551e1d7. Configure here.

Comment thread sentry_sdk/integrations/aiohttp.py Outdated
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 aiohttp to span first

1 participant