-
Notifications
You must be signed in to change notification settings - Fork 798
Description
Becuase the HttpX instrumentation changes the httpx.client class, it does not work on classes that are defined on import (even if the class is only instantiated after instrumentation). This means that as soon as OpenAI created an extension of the HttpX client, the httpx instrumentation stopped working. This could be fixed in OpenAI by defining the class at runtime:
class SyncAPIClient(BaseClient[httpx.Client, Stream[Any]]):
...
def __init__(
...
) -> None:
...
# Define at runtime
class SyncHttpxClientWrapper(httpx.Client):
def __del__(self) -> None:
try:
self.close()
except Exception:
pass
self._client = http_client or SyncHttpxClientWrapper(
base_url=base_url,
# cast to a valid type because mypy doesn't understand our type narrowing
timeout=cast(Timeout, timeout),
proxies=proxies,
transport=transport,
limits=limits,
follow_redirects=True,
)
This could also be solved by instrumenting httpx even before importing any library that uses httpx. However, I think these restrictions mean that the HttpX instrumentation is too fragile. We need to improve it so that it works intuitively for all such scenarios.
Describe your environment
Windows
opentelemetry-api 1.23.0
opentelemetry-instrumentation 0.44b0
opentelemetry-instrumentation-httpx 0.44b0
opentelemetry-instrumentation-openai 0.14.1
opentelemetry-sdk 1.23.0
opentelemetry-semantic-conventions 0.44b0
opentelemetry-semantic-conventions-ai 0.0.23
opentelemetry-util-http 0.44b0
Steps to reproduce
from openai import OpenAI # 1.x
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
import httpx
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
HTTPXClientInstrumentor().instrument()
url = "https://www.example.org/"
with httpx.Client() as client:
response = client.get(url)
OpenAIInstrumentor().instrument()
client = OpenAI() # 1.x
completion = client.chat.completions.create( # 1.x
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
input()
What is the expected behavior?
api.openai.com POST should be captured. Note that this is separate from the openai.chat span captured by the openai instrumentation.
What is the actual behavior?
Only the httpx example span and openai.chat spans are collected.
Additional context
Add any other context about the problem here.