Skip to content

ref(aiohttp): Move crumbs to integration - #7135

Open
sentrivana wants to merge 22 commits into
masterfrom
ivana/move-http-crumbs-1
Open

ref(aiohttp): Move crumbs to integration#7135
sentrivana wants to merge 22 commits into
masterfrom
ivana/move-http-crumbs-1

Conversation

@sentrivana

@sentrivana sentrivana commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Description

Create HTTP client breadcrumbs directly in the aiohttp integration instead of using maybe_create_breadcrumbs_from_span.

👀 Best reviewed with Hide whitespace on.

This turned out to be a bigger change than expected:

  • Breadcrumb data should be on par with what we're sending now. When we were taking it from the span, we had all the data readily available when we needed to create the breadcrumb. When moving the logic to the integration, we now need to remember information from the start request hook because it's not available in the finish request hook, where we create the breadcrumb now. (We need to create the breadcrumb at the end, because it contains stuff like HTTP status code.)
  • Breadcrumb data should be subject to PII filtering from send_default_pii/data_collection. So e.g. if parts of the query have been redacted on a span, they should not appear verbatim in the breadcrumb.
  • Since the PII redacting logic is different between transaction-based tracing and span streaming, the existing crumb tests needed to test both paths, as well as different PII filtering settings.

Additionally:

  • Rename span on the aiohttp trace context to _sentry_span

Issues

Reminders

span: "Union[Span, StreamedSpan, None]"
if has_span_streaming_enabled(client.options):
if sentry_sdk.traces.get_current_span() is None:
span = None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This had to be moved after the PII redaction, because even if we don't want to create a span, we do want to create a breadcrumb, and we need to apply the same PII redacting logic to breadcrumbs.

scope: "sentry_sdk.Scope", span: "sentry_sdk.tracing.Span"
) -> None:
if span.op == OP.HTTP_CLIENT:
if span.op == OP.HTTP_CLIENT and span.origin not in ("auto.http.aiohttp",):

@sentrivana sentrivana Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is just here to make sure we're not creating breadcrumbs the old way in transaction-based tracing anymore. Once all HTTP client integrations have been migrated, the whole function will go away

Base automatically changed from ivana/move-redis-breadcrumbs-to-integration to master August 7, 2026 11:33
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

99595 passed | ⏭️ 6479 skipped | Total: 106074 | Pass Rate: 93.89% | Execution Time: 356m 8s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +330
Passed Tests 📈 +330
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 89.09%. Project has 2503 uncovered lines.
✅ Project coverage is 89.99%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
sentry_sdk/integrations/aiohttp.py 86.05% ⚠️ 6 Missing and 4 partials
sentry_sdk/tracing_utils.py 100.00% ⚠️ 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.99%    89.99%        —%
==========================================
  Files          193       193         —
  Lines        24991     25015       +24
  Branches      9012      9022       +10
==========================================
+ Hits         22490     22512       +22
- Misses        2501      2503        +2
- Partials      1437      1436        -1

Generated by Codecov Action

@sentrivana
sentrivana marked this pull request as ready for review August 7, 2026 11:44
@sentrivana
sentrivana requested a review from a team as a code owner August 7, 2026 11:44
Comment thread sentry_sdk/integrations/aiohttp.py
Comment thread sentry_sdk/integrations/aiohttp.py Outdated
@sentrivana
sentrivana marked this pull request as draft August 7, 2026 12:17
@sentrivana
sentrivana marked this pull request as ready for review August 7, 2026 12:38
if trace_config_ctx.span is None:
status = int(params.response.status)

breadcrumb = trace_config_ctx._sentry_breadcrumb

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Accessing trace_config_ctx._sentry_breadcrumb in on_request_end may raise an AttributeError if it wasn't set in on_request_start due to the integration being disabled.
Severity: MEDIUM

Suggested Fix

In on_request_end, guard the access to trace_config_ctx._sentry_breadcrumb. You can use getattr(trace_config_ctx, "_sentry_breadcrumb", None) or a try...except AttributeError block to prevent the crash when the attribute is missing.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/aiohttp.py#L503

Potential issue: In the `on_request_end` function, `trace_config_ctx._sentry_breadcrumb`
is accessed without first checking if it has been set. If the `on_request_start`
function returns early (for example, if the AioHTTP integration is disabled after a
`ClientSession` is created), `_sentry_breadcrumb` is never assigned. This leads to an
`AttributeError` when `on_request_end` attempts to access it, which can crash the
request handler. This can occur when a `ClientSession` is created while the integration
is enabled, Sentry is then re-initialized without the integration, and a request is
subsequently made using the original session.

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

Non-blocking question, otherwise LGTM 🚀

Comment on lines +213 to +214
def add_http_breadcrumb(status_code, data):
# type: (Optional[int], dict[str, Any]) -> None

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.

Is there something preventing this type definition from living inline like the following?

Suggested change
def add_http_breadcrumb(status_code, data):
# type: (Optional[int], dict[str, Any]) -> None
def add_http_breadcrumb(status_code: "Optional[int]", data: "dict[str,Any]") -> "None":

Or is it in a comment as a matter of personal preference?

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