From 6e3a92bcfb88ef13a32df5f87fc1a05b267ed230 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 15 Nov 2024 09:09:53 +0800 Subject: [PATCH 1/4] Add OpenAI example Signed-off-by: Adrian Cole --- .pylintrc | 2 +- CHANGELOG.md | 4 +- .../example/.env | 18 +++++++ .../example/Dockerfile | 10 ++++ .../example/README.rst | 52 +++++++++++++++++++ .../example/docker-compose.yml | 9 ++++ .../example/main.py | 21 ++++++++ .../example/requirements.txt | 6 +++ 8 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/.env create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/main.py create mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/requirements.txt diff --git a/.pylintrc b/.pylintrc index 39ea7b5d35..bc3b25c978 100644 --- a/.pylintrc +++ b/.pylintrc @@ -7,7 +7,7 @@ extension-pkg-whitelist=cassandra # Add list of files or directories to be excluded. They should be base names, not # paths. -ignore=CVS,gen,Dockerfile,docker-compose.yml,README.md,requirements.txt,docs +ignore=CVS,gen,Dockerfile,docker-compose.yml,README.md,requirements.txt,docs,.venv # Add files or directories matching the regex patterns to be excluded. The # regex matches against base names, not paths. diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e366402a..74c767f3b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Add example to `opentelemetry-instrumentation-openai-v2` + ([#3006](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3006)) - `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2 ([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976)) - - Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap` +- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap` ([#2996](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2996)) - `opentelemetry-instrumentation-sqlalchemy` Add sqlcomment to `db.statement` attribute ([#2937](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2937)) diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/.env b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/.env new file mode 100644 index 0000000000..d6afa66723 --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/.env @@ -0,0 +1,18 @@ +# Update this with your real OpenAI API key +OPENAI_API_KEY=sk-YOUR_API_KEY + +# Uncomment to use Ollama instead of OpenAI +# OPENAI_BASE_URL=http://localhost:11434/v1 +# OPENAI_API_KEY=unused +# CHAT_MODEL=qwen2.5:0.5b + +OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 +OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf +OTEL_SERVICE_NAME=opentelemetry-python-openai + +# Change to 'false' to disable logging +OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true +# Change to 'console' if your OTLP endpoint doesn't support logs +OTEL_LOGS_EXPORTER=otlp_proto_http +# Change to 'false' to hide prompt and completion content +OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile new file mode 100644 index 0000000000..a5b68c41c4 --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile @@ -0,0 +1,10 @@ +# Use an alpine image to make the runtime smaller +FROM docker.io/python:3.12.7-alpine3.20 +RUN python -m pip install --upgrade pip + +COPY /requirements.txt /tmp/requirements.txt +RUN pip install -r /tmp/requirements.txt + +COPY main.py / + +CMD [ "opentelemetry-instrument", "python", "main.py" ] diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst new file mode 100644 index 0000000000..b079930d04 --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst @@ -0,0 +1,52 @@ +OpenTelemetry OpenAI Instrumentation Example +============================================ + +This is an example of how to instrument OpenAI calls with zero code changes, +using `opentelemetry-instrument`. + +When `main.py `_ is run, it exports traces and logs to an OTLP +compatible endpoint. Traces include details such as the model used and the +duration of the chat request. Logs capture the chat request and the generated +response, providing a comprehensive view of the performance and behavior of +your OpenAI requests. + +Setup +----- + +Minimally, update the `.env <.env>`_ file with your "OPENAI_API_KEY". An +OTLP compatible endpoint should be listening for traces and logs on +http://localhost:4318. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well. + +Run with Docker +--------------- + +If you have Docker installed, you can run the example in one step: + +:: + + docker-compose run --build --rm python-opentelemetry-openai + +You should see a poem generated by OpenAI while traces and logs export to your +configured observability tool. + +Run with Python +--------------- + +If you prefer to run the example with Python, set up a virtual environment for +the example like this: + +:: + + python3 -m venv .venv + source .venv/bin/activate + pip install "python-dotenv[cli]" + pip install -r requirements.txt + +Now, run the example like this: + +:: + + dotenv run -- opentelemetry-instrument python main.py + +You should see a poem generated by OpenAI while traces and logs export to your +configured observability tool. diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml new file mode 100644 index 0000000000..27e247e85d --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml @@ -0,0 +1,9 @@ +services: + opentelemetry-python-openai: + container_name: opentelemetry-python-openai + build: + context: . + env_file: + - .env + environment: + OTEL_EXPORTER_OTLP_ENDPOINT: "http://host.docker.internal:4318" diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/main.py b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/main.py new file mode 100644 index 0000000000..d41b1dd933 --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/main.py @@ -0,0 +1,21 @@ +import os + +from openai import OpenAI + + +def main(): + client = OpenAI() + chat_completion = client.chat.completions.create( + model=os.getenv("CHAT_MODEL", "gpt-4o-mini"), + messages=[ + { + "role": "user", + "content": "Write a short poem on OpenTelemetry.", + }, + ], + ) + print(chat_completion.choices[0].message.content) + + +if __name__ == "__main__": + main() diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/requirements.txt b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/requirements.txt new file mode 100644 index 0000000000..9ec9bff320 --- /dev/null +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/requirements.txt @@ -0,0 +1,6 @@ +openai~=1.54.4 + +opentelemetry-sdk~=1.28.2 +opentelemetry-exporter-otlp-proto-http~=1.28.2 +opentelemetry-distro~=0.49b2 +opentelemetry-instrumentation-openai-v2~=2.0b0 From 91d97e17e753eeedef3413b965841226dbcea79b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 19 Nov 2024 09:06:41 +0800 Subject: [PATCH 2/4] link from package Signed-off-by: Adrian Cole --- .../opentelemetry-instrumentation-openai-v2/README.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/README.rst b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/README.rst index 85817896ff..bbd142a97e 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/README.rst +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/README.rst @@ -6,17 +6,21 @@ OpenTelemetry OpenAI Instrumentation .. |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 -specified to trace_integration using 'OpenAI'. +This library allows tracing LLM requests and logging of messages made by the +`OpenAI Python API library `_. Installation ------------ +If your application is already instrumented with OpenTelemetry, add this +package to your requirements. :: pip install opentelemetry-instrumentation-openai-v2 +If you don't have an OpenAI application, yet, try our `example `_ +which only needs a valid OpenAI API key. References ---------- From 6b798c86199ac46630a3d1017010def3f034c7e1 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 19 Nov 2024 09:35:35 +0800 Subject: [PATCH 3/4] nuke docker option Signed-off-by: Adrian Cole --- .../example/Dockerfile | 10 -------- .../example/README.rst | 23 ++++--------------- .../example/docker-compose.yml | 9 -------- 3 files changed, 5 insertions(+), 37 deletions(-) delete mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile delete mode 100644 instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile deleted file mode 100644 index a5b68c41c4..0000000000 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# Use an alpine image to make the runtime smaller -FROM docker.io/python:3.12.7-alpine3.20 -RUN python -m pip install --upgrade pip - -COPY /requirements.txt /tmp/requirements.txt -RUN pip install -r /tmp/requirements.txt - -COPY main.py / - -CMD [ "opentelemetry-instrument", "python", "main.py" ] diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst index b079930d04..019e141c70 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst @@ -17,23 +17,7 @@ Minimally, update the `.env <.env>`_ file with your "OPENAI_API_KEY". An OTLP compatible endpoint should be listening for traces and logs on http://localhost:4318. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well. -Run with Docker ---------------- - -If you have Docker installed, you can run the example in one step: - -:: - - docker-compose run --build --rm python-opentelemetry-openai - -You should see a poem generated by OpenAI while traces and logs export to your -configured observability tool. - -Run with Python ---------------- - -If you prefer to run the example with Python, set up a virtual environment for -the example like this: +Next, set up a virtual environment like this: :: @@ -42,7 +26,10 @@ the example like this: pip install "python-dotenv[cli]" pip install -r requirements.txt -Now, run the example like this: +Run +--- + +Run the example like this: :: diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml deleted file mode 100644 index 27e247e85d..0000000000 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -services: - opentelemetry-python-openai: - container_name: opentelemetry-python-openai - build: - context: . - env_file: - - .env - environment: - OTEL_EXPORTER_OTLP_ENDPOINT: "http://host.docker.internal:4318" From 2515a7906caf74550207fbfca13f8ce8fd3d6050 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 20 Nov 2024 09:06:03 +0800 Subject: [PATCH 4/4] move changelog Signed-off-by: Adrian Cole --- CHANGELOG.md | 2 -- .../opentelemetry-instrumentation-openai-v2/CHANGELOG.md | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c767f3b9..19a819cbe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add example to `opentelemetry-instrumentation-openai-v2` - ([#3006](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3006)) - `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2 ([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976)) - Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap` diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md index 07b10615c1..2c10498511 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Add example to `opentelemetry-instrumentation-openai-v2` + ([#3006](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3006)) - Support for `AsyncOpenAI/AsyncCompletions` ([#2984](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2984)) ## Version 2.0b0 (2024-11-08)