You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you call HTTPXClientInstrumentor.instrument, what happens in HTTPXClientInstrumentor._instrument, is that it creates subclasses of httpx.Clientand httpx.AsyncClient, and replaces httpx clients with those subclasses:
The problem with this approach is that subclasses of httpx clients created before calling HTTPXClientInstrumentor.instrument will not be instrumented, because they are already subclasses of the original client class.
When I updated openai I found that it broke telemetry. The reason is that it now uses subclasses of httpx clients.
So, if openai is imported before instrumenting httpx, telemetry is not going to work.
You can check this issue: openai/openai-python#1144.
To be clear, this is not a bug on openai. It is a problem in this package, which maybe I would not label as bug, but it is a surprising and unexpected behavior from a user's point of view, and as far as I know, it's not documented.
Steps to reproduce
Running this, openai httpx requests are instrumented:
It is subclassing to add some properties, and wrapping _transport in a SyncOpenTelemetryTransport or it's async version.
One way to avoid this behavior would be to patch directly the client classes, instead of patching the httpx package to replace the original clients with the instrumented ones.
However, I'm not sure about what unintentional consecuences this could have.
What do you think about this?
The text was updated successfully, but these errors were encountered:
When you call
HTTPXClientInstrumentor.instrument
, what happens inHTTPXClientInstrumentor._instrument
, is that it creates subclasses ofhttpx.Client
andhttpx.AsyncClient
, and replaces httpx clients with those subclasses:opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py
Lines 569 to 570 in 8fd2105
The problem with this approach is that subclasses of httpx clients created before calling
HTTPXClientInstrumentor.instrument
will not be instrumented, because they are already subclasses of the original client class.When I updated
openai
I found that it broke telemetry. The reason is that it now uses subclasses of httpx clients.So, if openai is imported before instrumenting httpx, telemetry is not going to work.
You can check this issue: openai/openai-python#1144.
To be clear, this is not a bug on
openai
. It is a problem in this package, which maybe I would not label as bug, but it is a surprising and unexpected behavior from a user's point of view, and as far as I know, it's not documented.Steps to reproduce
Running this, openai httpx requests are instrumented:
But this version is not instrumented, and the assertion fails:
Again, not
openai
specific, it is the same with any subclass:What is the expected behavior?
Subclasses of httpx's clients are instrumented even if they are created before instrumenting.
What is the actual behavior?
Subclasses of httpx's clients are not instrumented if they are created before instrumenting.
Solution proposal
The code of
_InstrumentedClient
and_InstrumentedAsyncClient
is this:opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py
Lines 491 to 526 in 8fd2105
It is subclassing to add some properties, and wrapping
_transport
in aSyncOpenTelemetryTransport
or it's async version.One way to avoid this behavior would be to patch directly the client classes, instead of patching the httpx package to replace the original clients with the instrumented ones.
However, I'm not sure about what unintentional consecuences this could have.
What do you think about this?
The text was updated successfully, but these errors were encountered: