Skip to content

Commit d06de3f

Browse files
samypr100ocelotllzchensrikanthccv
authored
regression-fix: retain httpx.URL type on request.url (#2359)
* fix: retain httpx.URL type on request.url This fixes a regression caused in #2020 where request.url stopped being of `httpx.URL` type, causing issues with request/response hooks. * Update CHANGELOG.md * tests: adding assertions to verify request.url on hooks is a httpx.URL * fixup: adjust check to consider httpx < 0.20.0 * fixup: keep code dry --------- Co-authored-by: Diego Hurtado <[email protected]> Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent ca082a7 commit d06de3f

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#2297](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2297))
2020
- Ensure all http.server.duration metrics have the same description
2121
([#2151](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2298))
22+
- Fix regression in httpx `request.url` not being of type `httpx.URL` after `0.44b0`
23+
([#2359](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2359))
2224
- Avoid losing repeated HTTP headers
2325
([#2266](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2266))
2426
- `opentelemetry-instrumentation-elasticsearch` Don't send bulk request body as db statement

instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _extract_parameters(args, kwargs):
270270
# In httpx >= 0.20.0, handle_request receives a Request object
271271
request: httpx.Request = args[0]
272272
method = request.method.encode()
273-
url = remove_url_credentials(str(request.url))
273+
url = httpx.URL(remove_url_credentials(str(request.url)))
274274
headers = request.headers
275275
stream = request.stream
276276
extensions = request.extensions

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

+9
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@
5151
HTTP_RESPONSE_BODY = "http.response.body"
5252

5353

54+
def _is_url_tuple(request: "RequestInfo"):
55+
"""Determine if request url format is for httpx versions < 0.20.0."""
56+
return isinstance(request[1], tuple) and len(request[1]) == 4
57+
58+
5459
def _async_call(coro: typing.Coroutine) -> asyncio.Task:
5560
loop = asyncio.get_event_loop()
5661
return loop.run_until_complete(coro)
5762

5863

5964
def _response_hook(span, request: "RequestInfo", response: "ResponseInfo"):
65+
assert _is_url_tuple(request) or isinstance(request.url, httpx.URL)
6066
span.set_attribute(
6167
HTTP_RESPONSE_BODY,
6268
b"".join(response[2]),
@@ -66,18 +72,21 @@ def _response_hook(span, request: "RequestInfo", response: "ResponseInfo"):
6672
async def _async_response_hook(
6773
span: "Span", request: "RequestInfo", response: "ResponseInfo"
6874
):
75+
assert _is_url_tuple(request) or isinstance(request.url, httpx.URL)
6976
span.set_attribute(
7077
HTTP_RESPONSE_BODY,
7178
b"".join([part async for part in response[2]]),
7279
)
7380

7481

7582
def _request_hook(span: "Span", request: "RequestInfo"):
83+
assert _is_url_tuple(request) or isinstance(request.url, httpx.URL)
7684
url = httpx.URL(request[1])
7785
span.update_name("GET" + str(url))
7886

7987

8088
async def _async_request_hook(span: "Span", request: "RequestInfo"):
89+
assert _is_url_tuple(request) or isinstance(request.url, httpx.URL)
8190
url = httpx.URL(request[1])
8291
span.update_name("GET" + str(url))
8392

0 commit comments

Comments
 (0)