Skip to content

HTTPXClientInstrumentor mixes async and non async hooks #1201

@alec-deason

Description

@alec-deason

Describe your environment
Python 3.10.3
opentelemetry-api==1.12.0rc2
opentelemetry-sdk==1.12.0rc2
opentelemetry-instrumentation-httpx==0.32b0

Steps to reproduce
Run this example:

 from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
 import httpx, asyncio

 def httpx_request_hook(span, request):
     print("test")

 HTTPXClientInstrumentor().instrument(request_hook=httpx_request_hook)

 # This succeeds
 with httpx.Client() as client:
     client.get("http://example.com")

 async def test_func():
     async with httpx.AsyncClient() as client:
         await client.get("http://example.com")

 # this fails with `TypeError: object NoneType can't be used in 'await' expression`
 asyncio.run(test_func())

What is the expected behavior?
After instrumentation it should be possible to successfully use httpx in both sync and async contexts.

What is the actual behavior?
If a non-async hook is provided then an exception is raised when the AsyncClient is used. If an async hook is provided then it never executes when the non-async Client is used.

Additional context
It seems to me this should either allow both async and sync hooks to be provided separately. Or it should require that the hook be non-blocking and only support non-async hooks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions