Skip to content

Commit 90fae47

Browse files
codefromthecryptxrmx
authored andcommitted
Add OpenAI example (open-telemetry#3006)
1 parent ca6099f commit 90fae47

File tree

8 files changed

+94
-4
lines changed

8 files changed

+94
-4
lines changed

Diff for: .pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension-pkg-whitelist=cassandra
77

88
# Add list of files or directories to be excluded. They should be base names, not
99
# paths.
10-
ignore=CVS,gen,Dockerfile,docker-compose.yml,README.md,requirements.txt,docs
10+
ignore=CVS,gen,Dockerfile,docker-compose.yml,README.md,requirements.txt,docs,.venv
1111

1212
# Add files or directories matching the regex patterns to be excluded. The
1313
# regex matches against base names, not paths.

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2
1717
([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976))
18-
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
18+
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
1919
([#2996](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2996))
2020
- `opentelemetry-instrumentation-sqlalchemy` Add sqlcomment to `db.statement` attribute
2121
([#2937](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2937))

Diff for: instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Add example to `opentelemetry-instrumentation-openai-v2`
11+
([#3006](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3006))
1012
- Support for `AsyncOpenAI/AsyncCompletions` ([#2984](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2984))
1113

1214
## Version 2.0b0 (2024-11-08)

Diff for: instrumentation-genai/opentelemetry-instrumentation-openai-v2/README.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ OpenTelemetry OpenAI Instrumentation
66
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-openai-v2.svg
77
:target: https://pypi.org/project/opentelemetry-instrumentation-openai-v2/
88

9-
Instrumentation with OpenAI that supports the OpenAI library and is
10-
specified to trace_integration using 'OpenAI'.
9+
This library allows tracing LLM requests and logging of messages made by the
10+
`OpenAI Python API library <https://pypi.org/project/openai/>`_.
1111

1212

1313
Installation
1414
------------
1515

16+
If your application is already instrumented with OpenTelemetry, add this
17+
package to your requirements.
1618
::
1719

1820
pip install opentelemetry-instrumentation-openai-v2
1921

22+
If you don't have an OpenAI application, yet, try our `example <example>`_
23+
which only needs a valid OpenAI API key.
2024

2125
References
2226
----------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Update this with your real OpenAI API key
2+
OPENAI_API_KEY=sk-YOUR_API_KEY
3+
4+
# Uncomment to use Ollama instead of OpenAI
5+
# OPENAI_BASE_URL=http://localhost:11434/v1
6+
# OPENAI_API_KEY=unused
7+
# CHAT_MODEL=qwen2.5:0.5b
8+
9+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
10+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
11+
OTEL_SERVICE_NAME=opentelemetry-python-openai
12+
13+
# Change to 'false' to disable logging
14+
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
15+
# Change to 'console' if your OTLP endpoint doesn't support logs
16+
OTEL_LOGS_EXPORTER=otlp_proto_http
17+
# Change to 'false' to hide prompt and completion content
18+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
OpenTelemetry OpenAI Instrumentation Example
2+
============================================
3+
4+
This is an example of how to instrument OpenAI calls with zero code changes,
5+
using `opentelemetry-instrument`.
6+
7+
When `main.py <main.py>`_ is run, it exports traces and logs to an OTLP
8+
compatible endpoint. Traces include details such as the model used and the
9+
duration of the chat request. Logs capture the chat request and the generated
10+
response, providing a comprehensive view of the performance and behavior of
11+
your OpenAI requests.
12+
13+
Setup
14+
-----
15+
16+
Minimally, update the `.env <.env>`_ file with your "OPENAI_API_KEY". An
17+
OTLP compatible endpoint should be listening for traces and logs on
18+
http://localhost:4318. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well.
19+
20+
Next, set up a virtual environment like this:
21+
22+
::
23+
24+
python3 -m venv .venv
25+
source .venv/bin/activate
26+
pip install "python-dotenv[cli]"
27+
pip install -r requirements.txt
28+
29+
Run
30+
---
31+
32+
Run the example like this:
33+
34+
::
35+
36+
dotenv run -- opentelemetry-instrument python main.py
37+
38+
You should see a poem generated by OpenAI while traces and logs export to your
39+
configured observability tool.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
6+
def main():
7+
client = OpenAI()
8+
chat_completion = client.chat.completions.create(
9+
model=os.getenv("CHAT_MODEL", "gpt-4o-mini"),
10+
messages=[
11+
{
12+
"role": "user",
13+
"content": "Write a short poem on OpenTelemetry.",
14+
},
15+
],
16+
)
17+
print(chat_completion.choices[0].message.content)
18+
19+
20+
if __name__ == "__main__":
21+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openai~=1.54.4
2+
3+
opentelemetry-sdk~=1.28.2
4+
opentelemetry-exporter-otlp-proto-http~=1.28.2
5+
opentelemetry-distro~=0.49b2
6+
opentelemetry-instrumentation-openai-v2~=2.0b0

0 commit comments

Comments
 (0)