Skip to content

Commit 8c54caa

Browse files
elastic-opentelemetry-instrumentation-openai: normalizes env variables to upstream (#29)
This normalizes the ENV variables around content capture to match upstream. Co-authored-by: Adrian Cole <[email protected]>
1 parent dae460d commit 8c54caa

File tree

49 files changed

+104
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+104
-95
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/README.md

Lines changed: 9 additions & 3 deletions

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
2727
from opentelemetry.instrumentation.utils import unwrap
2828
from opentelemetry.instrumentation.openai.environment_variables import (
29-
ELASTIC_OTEL_GENAI_CAPTURE_CONTENT,
3029
ELASTIC_OTEL_GENAI_EVENTS,
30+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
3131
)
3232
from opentelemetry.instrumentation.openai.helpers import (
3333
_get_embeddings_span_attributes_from_wrapper,
@@ -78,10 +78,13 @@ def _instrument(self, **kwargs):
7878
``tracer_provider``: a TracerProvider, defaults to global
7979
``meter_provider``: a MeterProvider, defaults to global
8080
``event_logger_provider``: a EventLoggerProvider, defaults to global
81-
``capture_content``: to enable content capturing, defaults to False
81+
``capture_message_content``: to enable content capturing, defaults to False
8282
"""
83-
capture_content = "true" if kwargs.get("capture_content") else "false"
84-
self.capture_content = os.environ.get(ELASTIC_OTEL_GENAI_CAPTURE_CONTENT, capture_content).lower() == "true"
83+
capture_message_content = "true" if kwargs.get("capture_message_content") else "false"
84+
self.capture_message_content = (
85+
os.environ.get(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, capture_message_content).lower()
86+
== "true"
87+
)
8588

8689
# we support 3 values for deciding how to send events:
8790
# - "latest" to match latest semconv, as 1.27.0 it's span
@@ -135,7 +138,7 @@ def _patch(self, _module):
135138
)
136139

137140
def _uninstrument(self, **kwargs):
138-
# unwrap only supports uninstrementing real module references so we
141+
# unwrap only supports uninstrumenting real module references so we
139142
# import here.
140143
import openai
141144

@@ -159,7 +162,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
159162
end_on_exit=False,
160163
) as span:
161164
# TODO: more fine grained depending on the message.role?
162-
if self.capture_content:
165+
if self.capture_message_content:
163166
messages = kwargs.get("messages", [])
164167

165168
if self.event_kind == "log":
@@ -184,7 +187,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
184187
return StreamWrapper(
185188
stream=result,
186189
span=span,
187-
capture_content=self.capture_content,
190+
capture_message_content=self.capture_message_content,
188191
event_kind=self.event_kind,
189192
event_attributes=event_attributes,
190193
event_logger=self.event_logger,
@@ -201,7 +204,7 @@ def _chat_completion_wrapper(self, wrapped, instance, args, kwargs):
201204
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
202205
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
203206

204-
if self.capture_content:
207+
if self.capture_message_content:
205208
if self.event_kind == "log":
206209
_send_log_events_from_choices(
207210
self.event_logger, choices=result.choices, attributes=event_attributes
@@ -234,7 +237,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
234237
# this is important to avoid having the span closed before ending the stream
235238
end_on_exit=False,
236239
) as span:
237-
if self.capture_content:
240+
if self.capture_message_content:
238241
messages = kwargs.get("messages", [])
239242

240243
if self.event_kind == "log":
@@ -259,7 +262,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
259262
return StreamWrapper(
260263
stream=result,
261264
span=span,
262-
capture_content=self.capture_content,
265+
capture_message_content=self.capture_message_content,
263266
event_kind=self.event_kind,
264267
event_attributes=event_attributes,
265268
event_logger=self.event_logger,
@@ -276,7 +279,7 @@ async def _async_chat_completion_wrapper(self, wrapped, instance, args, kwargs):
276279
_record_token_usage_metrics(self.token_usage_metric, span, result.usage)
277280
_record_operation_duration_metric(self.operation_duration_metric, span, start_time)
278281

279-
if self.capture_content:
282+
if self.capture_message_content:
280283
if self.event_kind == "log":
281284
_send_log_events_from_choices(
282285
self.event_logger, choices=result.choices, attributes=event_attributes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
ELASTIC_OTEL_GENAI_CAPTURE_CONTENT = "ELASTIC_OTEL_GENAI_CAPTURE_CONTENT"
17+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
1818

1919
ELASTIC_OTEL_GENAI_EVENTS = "ELASTIC_OTEL_GENAI_EVENTS"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(
4545
self,
4646
stream,
4747
span: Span,
48-
capture_content: bool,
48+
capture_message_content: bool,
4949
event_kind: Literal["log", "span"],
5050
event_attributes: Attributes,
5151
event_logger: EventLogger,
@@ -55,7 +55,7 @@ def __init__(
5555
):
5656
self.stream = stream
5757
self.span = span
58-
self.capture_content = capture_content
58+
self.capture_message_content = capture_message_content
5959
self.event_kind = event_kind
6060
self.event_attributes = event_attributes
6161
self.event_logger = event_logger
@@ -84,7 +84,7 @@ def end(self, exc=None):
8484
if self.usage:
8585
_record_token_usage_metrics(self.token_usage_metric, self.span, self.usage)
8686

87-
if self.capture_content:
87+
if self.capture_message_content:
8888
if self.event_kind == "log":
8989
_send_log_events_from_stream_choices(
9090
self.event_logger, choices=self.choices, span=self.span, attributes=self.event_attributes

0 commit comments

Comments
 (0)