opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

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

Open horw opened this issue 1 year ago • 2 comments
trafficstars

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

horw avatar Jul 24 '24 06:07 horw

Can you assign this one to me?

shijiadong2022 avatar Jul 24 '24 07:07 shijiadong2022

@shijiadong2022 looking forward your PR

xrmx avatar Jul 24 '24 12:07 xrmx

@shijiadong2022 have you started working on this?

xrmx avatar Jul 31 '24 09:07 xrmx

I am working on it and will submit a PR soon

shijiadong2022 avatar Jul 31 '24 11:07 shijiadong2022

@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)

xrmx avatar Aug 26 '24 19:08 xrmx