Skip to content

Commit b431243

Browse files
feat(httpx): Add source information for slow outgoing HTTP requests (#4904)
Uses functionality from SQL query source and tests that it works in the HTTP request setting.
1 parent 77937e9 commit b431243

File tree

10 files changed

+693
-7
lines changed

10 files changed

+693
-7
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOURCE_FOR_STYLE,
2323
TransactionSource,
2424
)
25-
from sentry_sdk.tracing_utils import should_propagate_trace
25+
from sentry_sdk.tracing_utils import should_propagate_trace, add_http_request_source
2626
from sentry_sdk.utils import (
2727
capture_internal_exceptions,
2828
ensure_integration_enabled,
@@ -279,6 +279,8 @@ async def on_request_end(session, trace_config_ctx, params):
279279
span.set_data("reason", params.response.reason)
280280
span.finish()
281281

282+
add_http_request_source(span)
283+
282284
trace_config = TraceConfig()
283285

284286
trace_config.on_request_start.append(on_request_start)

sentry_sdk/integrations/httpx.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import sentry_sdk
2+
from sentry_sdk import start_span
23
from sentry_sdk.consts import OP, SPANDATA
34
from sentry_sdk.integrations import Integration, DidNotEnable
45
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME
5-
from sentry_sdk.tracing_utils import Baggage, should_propagate_trace
6+
from sentry_sdk.tracing_utils import (
7+
Baggage,
8+
should_propagate_trace,
9+
add_http_request_source,
10+
)
611
from sentry_sdk.utils import (
712
SENSITIVE_DATA_SUBSTITUTE,
813
capture_internal_exceptions,
@@ -52,7 +57,7 @@ def send(self, request, **kwargs):
5257
with capture_internal_exceptions():
5358
parsed_url = parse_url(str(request.url), sanitize=False)
5459

55-
with sentry_sdk.start_span(
60+
with start_span(
5661
op=OP.HTTP_CLIENT,
5762
name="%s %s"
5863
% (
@@ -88,7 +93,10 @@ def send(self, request, **kwargs):
8893
span.set_http_status(rv.status_code)
8994
span.set_data("reason", rv.reason_phrase)
9095

91-
return rv
96+
with capture_internal_exceptions():
97+
add_http_request_source(span)
98+
99+
return rv
92100

93101
Client.send = send
94102

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

109-
with sentry_sdk.start_span(
117+
with start_span(
110118
op=OP.HTTP_CLIENT,
111119
name="%s %s"
112120
% (
@@ -144,7 +152,10 @@ async def send(self, request, **kwargs):
144152
span.set_http_status(rv.status_code)
145153
span.set_data("reason", rv.reason_phrase)
146154

147-
return rv
155+
with capture_internal_exceptions():
156+
add_http_request_source(span)
157+
158+
return rv
148159

149160
AsyncClient.send = send
150161

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import os
2+
import sys
13
import pytest
24

35
pytest.importorskip("aiohttp")
6+
7+
# Load `aiohttp_helpers` into the module search path to test request source path names relative to module. See
8+
# `test_request_source_with_module_in_search_path`
9+
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))

tests/integrations/aiohttp/aiohttp_helpers/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def get_request_with_client(client, url):
2+
await client.get(url)

0 commit comments

Comments
 (0)