Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression-fix: retain httpx.URL type on request.url #2359

Merged
merged 14 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2297](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2297))
- Ensure all http.server.duration metrics have the same description
([#2151](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2298))
- Fix regression in httpx `request.url` not being of type `httpx.URL` after `0.44b0`
([#2359](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2359))
- Avoid losing repeated HTTP headers
([#2266](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2266))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _extract_parameters(args, kwargs):
# In httpx >= 0.20.0, handle_request receives a Request object
request: httpx.Request = args[0]
method = request.method.encode()
url = remove_url_credentials(str(request.url))
url = httpx.URL(remove_url_credentials(str(request.url)))
headers = request.headers
stream = request.stream
extensions = request.extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def _async_call(coro: typing.Coroutine) -> asyncio.Task:


def _response_hook(span, request: "RequestInfo", response: "ResponseInfo"):
# httpx < 0.20.0 url is a tuple of len 4 and not a httpx.URL
samypr100 marked this conversation as resolved.
Show resolved Hide resolved
is_url_tuple = isinstance(request[1], tuple) and len(request[1]) == 4
if not is_url_tuple:
assert isinstance(request.url, httpx.URL)
samypr100 marked this conversation as resolved.
Show resolved Hide resolved
span.set_attribute(
HTTP_RESPONSE_BODY,
b"".join(response[2]),
Expand All @@ -66,18 +70,30 @@ def _response_hook(span, request: "RequestInfo", response: "ResponseInfo"):
async def _async_response_hook(
span: "Span", request: "RequestInfo", response: "ResponseInfo"
):
# httpx < 0.20.0 url is a tuple of len 4 and not a httpx.URL
is_url_tuple = isinstance(request[1], tuple) and len(request[1]) == 4
if not is_url_tuple:
assert isinstance(request.url, httpx.URL)
span.set_attribute(
HTTP_RESPONSE_BODY,
b"".join([part async for part in response[2]]),
)


def _request_hook(span: "Span", request: "RequestInfo"):
# httpx < 0.20.0 url is a tuple of len 4 and not a httpx.URL
is_url_tuple = isinstance(request[1], tuple) and len(request[1]) == 4
if not is_url_tuple:
assert isinstance(request.url, httpx.URL)
url = httpx.URL(request[1])
span.update_name("GET" + str(url))


async def _async_request_hook(span: "Span", request: "RequestInfo"):
# httpx < 0.20.0 url is a tuple of len 4 and not a httpx.URL
is_url_tuple = isinstance(request[1], tuple) and len(request[1]) == 4
if not is_url_tuple:
assert isinstance(request.url, httpx.URL)
url = httpx.URL(request[1])
span.update_name("GET" + str(url))

Expand Down
Loading