opentelemetry-python-contrib
opentelemetry-python-contrib copied to clipboard
Sync hook used as async hook in opentelemetry-instrumentation-httpx causing TypeError
Describe your environment
OS: Ubuntu Python 3.10.12 Name: opentelemetry-instrumentation-httpx Version: 0.46b0
What happened?
code from lib
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 you set up request_hook, it will also be used for async_request_hook if you don't initialize async_request_hook. This could lead to issues because you don't want to pass an async function to request_hook. If a non-async function is passed, it may not work as expected. It seems that there needs to be a wrapper around request_hook to check if it is not async and then wrap it accordingly.
Steps to Reproduce
def hook(span, req):
pass
HTTPXClientInstrumentor().instrument(request_hook=hook)
with Client() as client:
client.post("http://localhost:9999/hello")
async with AsyncClient() as client:
await client.post("http://localhost:9999/hello")
Expected Result
Both requests completed
Actual Result
await self._request_hook(span, request_info)
TypeError: object NoneType can't be used in 'await' expression
Additional context
No response
Would you like to implement a fix?
None
Can you assign this one to me?
@shijiadong2022 looking forward your PR
@shijiadong2022 have you started working on this?
I am working on it and will submit a PR soon
@horw Could you please double check if the fix works for you? To have the desired outcome with the updated code you should provide yourself the async_request_hook callback e.g. HTTPXClientInstrumentor().instrument(request_hook=hook, async_request_hook=async_request_hook)