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

fix Sync hook used as async hook in opentelemetry-instrumentation-httpx causing TypeError #2794

Conversation

shijiadong2022
Copy link
Contributor

Fixes # (#2734)

@@ -731,7 +731,9 @@ 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)
Copy link
Contributor

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 if async_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 from async_request_hook is indeed an async function with inspect.iscoroutinefunction as part of the inspect module and not assign it to _InstrumentedAsyncClient._request_hook if not.

Copy link
Member

@emdneto emdneto Aug 13, 2024

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

leave it as None if not set; it will fail during the callable check when the hook is None.

Yes this and checking if async_request_hook is actually async is probably sufficient, and as always, adding more tests are always welcome.

Copy link
Contributor Author

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.

@lzchen
Copy link
Contributor

lzchen commented Aug 14, 2024

Please also add a CHANGELOG entry :)

@kayhern kayhern force-pushed the fix-async-request-hook branch from aa4d12f to 2602421 Compare August 23, 2024 14:04
@kayhern kayhern requested a review from a team August 23, 2024 14:04
Comment on lines +735 to +736
if iscoroutinefunction(request_hook):
async_request_hook = kwargs.get("async_request_hook", request_hook)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

"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):
Copy link
Contributor

Choose a reason for hiding this comment

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

And check here if it is a coroutine

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you'd just need the below:

async_request_hook = kwargs.get("async_request_hook")

if callable(async_request_hook) and iscoroutinefunction(async_request_hook):
     _InstrumentedAsyncClient._request_hook = async_request_hook

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@xrmx xrmx mentioned this pull request Aug 25, 2024
11 tasks
@xrmx
Copy link
Contributor

xrmx commented Aug 26, 2024

Superseded by #2823

@xrmx xrmx closed this Aug 26, 2024
@shijiadong2022 shijiadong2022 deleted the fix-async-request-hook branch August 27, 2024 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants