Skip to content

Commit

Permalink
OpenAI - update semantic conventions to the latest version, write log…
Browse files Browse the repository at this point in the history
…s based events instead of span events (#2925)
  • Loading branch information
lmolkova authored Oct 30, 2024
1 parent 54c7ee8 commit 07c3324
Show file tree
Hide file tree
Showing 19 changed files with 2,813 additions and 331 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Update OpenAI instrumentation to Semantic Conventions v1.28.0: add new attributes
and switch prompts and completions to log-based events.
([#2925](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2925))

- Initial OpenAI instrumentation
([#2759](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2759))
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
OpenTelemetry OpenAI Instrumentation
====================================
====================================

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-openai-v2.svg
:target: https://pypi.org/project/opentelemetry-instrumentation-openai-v2/

Instrumentation with OpenAI that supports the openai library and is
Instrumentation with OpenAI that supports the OpenAI library and is
specified to trace_integration using 'OpenAI'.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
.. code:: python
from openai import OpenAI
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
OpenAIInstrumentor().instrument()
Expand All @@ -44,8 +44,10 @@

from wrapt import wrap_function_wrapper

from opentelemetry._events import get_event_logger
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.openai_v2.package import _instruments
from opentelemetry.instrumentation.openai_v2.utils import is_content_enabled
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.semconv.schemas import Schemas
from opentelemetry.trace import get_tracer
Expand All @@ -64,15 +66,25 @@ def _instrument(self, **kwargs):
__name__,
"",
tracer_provider,
schema_url=Schemas.V1_27_0.value,
schema_url=Schemas.V1_28_0.value,
)
event_logger_provider = kwargs.get("event_logger_provider")
event_logger = get_event_logger(
__name__,
"",
schema_url=Schemas.V1_28_0.value,
event_logger_provider=event_logger_provider,
)

wrap_function_wrapper(
module="openai.resources.chat.completions",
name="Completions.create",
wrapper=chat_completions_create(tracer),
wrapper=chat_completions_create(
tracer, event_logger, is_content_enabled()
),
)

def _uninstrument(self, **kwargs):
import openai
import openai # pylint: disable=import-outside-toplevel

unwrap(openai.resources.chat.completions.Completions, "create")
Loading

0 comments on commit 07c3324

Please sign in to comment.