Skip to content

Commit 898cbf2

Browse files
committed
openai: inherint StreamWrapper from wrapt's ObjectProxy
So we have whatever attribute the callers expects.
1 parent d559b0c commit 898cbf2

File tree

1 file changed

+6
-4
lines changed
  • instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
from opentelemetry.trace import Span
2929
from opentelemetry.trace.status import StatusCode
3030
from opentelemetry.util.types import Attributes
31+
from wrapt import ObjectProxy
3132

3233
EVENT_GEN_AI_CONTENT_COMPLETION = "gen_ai.content.completion"
3334

3435
logger = logging.getLogger(__name__)
3536

3637

37-
class StreamWrapper:
38+
class StreamWrapper(ObjectProxy):
3839
def __init__(
3940
self,
4041
stream,
@@ -47,7 +48,8 @@ def __init__(
4748
token_usage_metric: Histogram,
4849
operation_duration_metric: Histogram,
4950
):
50-
self.stream = stream
51+
super().__init__(stream)
52+
5153
self.span = span
5254
self.span_attributes = span_attributes
5355
self.capture_message_content = capture_message_content
@@ -121,7 +123,7 @@ def __aiter__(self):
121123

122124
def __next__(self):
123125
try:
124-
chunk = next(self.stream)
126+
chunk = next(self.__wrapped__)
125127
self.process_chunk(chunk)
126128
return chunk
127129
except Exception as exc:
@@ -130,7 +132,7 @@ def __next__(self):
130132

131133
async def __anext__(self):
132134
try:
133-
chunk = await self.stream.__anext__()
135+
chunk = await self.__wrapped__.__anext__()
134136
self.process_chunk(chunk)
135137
return chunk
136138
except Exception as exc:

0 commit comments

Comments
 (0)