open-telemetry / opentelemetry-python-contrib

OpenTelemetry instrumentation for Python modules
https://opentelemetry.io
Apache License 2.0
701 stars 584 forks source link

Subclassing httpx causing `isinstance` checks failed #2609

Open CNSeniorious000 opened 3 months ago

CNSeniorious000 commented 3 months ago

Describe your environment

OS: Windows-10-10.0.22631-SP0 Python version: Python 3.12.3 Package version: opentelemetry-instrumentation-httpx="0.46b0"

What happened?

I instrumented httpx after some httpx clients being initialized, and then isinstance(client, Client) failed.

I admit that instrumenting code should ideally be placed before any other code, but I hope that even if it is not done this way, it will result in no errors if possible.

Related to https://github.com/pydantic/logfire/issues/264

Steps to Reproduce

from httpx import Client
client = Client()
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
HTTPXClientInstrumentor().instrument()
from httpx import Client
isinstance(client, Client)

Expected Result

True

Actual Result

False

Additional context

Maybe we can use an non-subclassing approach, like patching methods on the original class.

Would you like to implement a fix?

Being open to