Skip to content

Commit b9ecd72

Browse files
xrmxanuraaga
andauthored
elastic-opentelemetry-instrumentation-openai: register import hook for patching (#16)
Fix a race condition between openai initialization and httpx instrumentation kicking in by loading the openai module when required. This should make the http spans from the api calls of the openai client recorded. Co-authored-by: Anuraag (Rag) Agrawal <[email protected]>
1 parent 1329bf7 commit b9ecd72

File tree

2 files changed

+14
-3
lines changed
  • instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai

2 files changed

+14
-3
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
from timeit import default_timer
2121
from typing import Collection
2222

23-
import openai
24-
from wrapt import wrap_function_wrapper
23+
from wrapt import register_post_import_hook, wrap_function_wrapper
2524

2625
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
2726
from opentelemetry.instrumentation.utils import unwrap
@@ -83,6 +82,9 @@ def _instrument(self, **kwargs):
8382
self.token_usage_metric = create_gen_ai_client_token_usage(self.meter)
8483
self.operation_duration_metric = create_gen_ai_client_operation_duration(self.meter)
8584

85+
register_post_import_hook(self._patch, "openai")
86+
87+
def _patch(self, _module):
8688
wrap_function_wrapper(
8789
"openai.resources.chat.completions",
8890
"Completions.create",
@@ -95,6 +97,10 @@ def _instrument(self, **kwargs):
9597
)
9698

9799
def _uninstrument(self, **kwargs):
100+
# unwrap only supports uninstrementing real module references so we
101+
# import here.
102+
import openai
103+
98104
unwrap(openai.resources.chat.completions.Completions, "create")
99105
unwrap(openai.resources.chat.completions.AsyncCompletions, "create")
100106

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import json
1818
from collections.abc import Iterable
1919
from timeit import default_timer
20+
from typing import TYPE_CHECKING
2021

21-
from openai.types import CompletionUsage
2222
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
2323
from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
2424
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
@@ -41,6 +41,11 @@
4141
from opentelemetry.metrics import Histogram
4242
from opentelemetry.trace import Span
4343

44+
if TYPE_CHECKING:
45+
from openai.types import CompletionUsage
46+
else:
47+
CompletionUsage = None
48+
4449

4550
def _set_span_attributes_from_response(
4651
span: Span, response_id: str, model: str, choices, usage: CompletionUsage

0 commit comments

Comments
 (0)