Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOURCE_FOR_STYLE,
TransactionSource,
)
from sentry_sdk.tracing_utils import should_propagate_trace
from sentry_sdk.tracing_utils import should_propagate_trace, add_http_request_source
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
Expand Down Expand Up @@ -279,6 +279,8 @@ async def on_request_end(session, trace_config_ctx, params):
span.set_data("reason", params.response.reason)
span.finish()

add_http_request_source(span)

trace_config = TraceConfig()

trace_config.on_request_start.append(on_request_start)
Expand Down
21 changes: 16 additions & 5 deletions sentry_sdk/integrations/httpx.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import sentry_sdk
from sentry_sdk import start_span
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME
from sentry_sdk.tracing_utils import Baggage, should_propagate_trace
from sentry_sdk.tracing_utils import (
Baggage,
should_propagate_trace,
add_http_request_source,
)
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
Expand Down Expand Up @@ -52,7 +57,7 @@ def send(self, request, **kwargs):
with capture_internal_exceptions():
parsed_url = parse_url(str(request.url), sanitize=False)

with sentry_sdk.start_span(
with start_span(
op=OP.HTTP_CLIENT,
name="%s %s"
% (
Expand Down Expand Up @@ -88,7 +93,10 @@ def send(self, request, **kwargs):
span.set_http_status(rv.status_code)
span.set_data("reason", rv.reason_phrase)

return rv
with capture_internal_exceptions():
add_http_request_source(span)
Copy link

Choose a reason for hiding this comment

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

Bug: Span Context Mismatch Causes Missing Data

The add_http_request_source(span) call is placed outside the span context manager, which means it executes after the span has finished. This prevents the HTTP request source information from being correctly added to the span.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor Author

@alexander-alderman-webb alexander-alderman-webb Oct 9, 2025

Choose a reason for hiding this comment

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

I placed add_http_request_source(span) after the span is finished because you need the end timestamp to determine the delay in receiving a response to the HTTP request.

It's done analogously in asyncpg and sqlalchemy.


return rv

Client.send = send

Expand All @@ -106,7 +114,7 @@ async def send(self, request, **kwargs):
with capture_internal_exceptions():
parsed_url = parse_url(str(request.url), sanitize=False)

with sentry_sdk.start_span(
with start_span(
op=OP.HTTP_CLIENT,
name="%s %s"
% (
Expand Down Expand Up @@ -144,7 +152,10 @@ async def send(self, request, **kwargs):
span.set_http_status(rv.status_code)
span.set_data("reason", rv.reason_phrase)

return rv
with capture_internal_exceptions():
add_http_request_source(span)

return rv

AsyncClient.send = send

Expand Down
6 changes: 6 additions & 0 deletions tests/integrations/aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import os
import sys
import pytest

pytest.importorskip("aiohttp")

# Load `aiohttp_helpers` into the module search path to test request source path names relative to module. See
# `test_request_source_with_module_in_search_path`
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
Empty file.
2 changes: 2 additions & 0 deletions tests/integrations/aiohttp/aiohttp_helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
async def get_request_with_client(client, url):
await client.get(url)
Loading