-
Notifications
You must be signed in to change notification settings - Fork 683
fix Sync hook used as async hook in opentelemetry-instrumentation-httpx causing TypeError #2794
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
Changes from all commits
3ff1113
f3347b6
e548302
23d3131
2602421
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,6 +192,7 @@ async def async_response_hook(span, request, response): | |
""" | ||
import logging | ||
import typing | ||
from inspect import iscoroutinefunction | ||
from types import TracebackType | ||
|
||
import httpx | ||
|
@@ -731,8 +732,16 @@ def _instrument(self, **kwargs): | |
self._original_async_client = httpx.AsyncClient | ||
request_hook = kwargs.get("request_hook") | ||
response_hook = kwargs.get("response_hook") | ||
async_request_hook = kwargs.get("async_request_hook", request_hook) | ||
async_response_hook = kwargs.get("async_response_hook", response_hook) | ||
if iscoroutinefunction(request_hook): | ||
async_request_hook = kwargs.get("async_request_hook", request_hook) | ||
Comment on lines
+735
to
+736
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the previous review, I'm not sure we want this. I think the idea is to remove the default from the previous code here |
||
else: | ||
async_request_hook = kwargs.get("async_request_hook") | ||
if iscoroutinefunction(response_hook): | ||
async_response_hook = kwargs.get( | ||
"async_response_hook", response_hook | ||
) | ||
else: | ||
async_response_hook = kwargs.get("async_response_hook") | ||
if callable(request_hook): | ||
_InstrumentedClient._request_hook = request_hook | ||
if callable(async_request_hook): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And check here if it is a coroutine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you'd just need the below:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @xrmx and @lzchen for your feedback. I've tried the code you mentioned but the testAsyncInstrumentationIntegration test case will fail with the change. The reason is in this case, request_hook is actually an aysnc request hook which we cannot set to None. Please correct me if I misunderstood. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should remove this logic of defaulting to
request hook
ifasync_request_hook
does not exist. It doesn't make much intuitive sense to me. We can then probably just add a check to see if the hook coming fromasync_request_hook
is indeed an async function withinspect.iscoroutinefunction
as part of the inspect module and not assign it to_InstrumentedAsyncClient._request_hook
if not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The easiest way is probably to leave it as None if not set; it will fail during the callable check when the hook is None. Also, this PR needs the async_response_hook fix as well. And of course, some tests would be nice to avoid problems with this again. Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this and checking if
async_request_hook
is actuallyasync
is probably sufficient, and as always, adding more tests are always welcome.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review. I will work on the changes.