Skip to content

Commit 611ce5c

Browse files
Add OpenAI example
Signed-off-by: Adrian Cole <[email protected]>
1 parent 803bb32 commit 611ce5c

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
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.

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

16+
- Add example to `opentelemetry-instrumentation-openai-v2`
17+
([#0000](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/0000))
1618
- `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2
1719
([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976))
18-
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
20+
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
1921
([#2996](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2996))
2022

2123
### Fixed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Uncomment to use Ollama instead of OpenAI
2+
# OPENAI_BASE_URL=http://localhost:11434/v1
3+
# OPENAI_API_KEY=unused
4+
# CHAT_MODEL=qwen2.5:0.5b
5+
6+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
7+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
8+
OTEL_SERVICE_NAME=opentelemetry-instrumentation-openai-v2
9+
10+
# Set to false or remove to disable log events
11+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
12+
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
13+
OTEL_LOGS_EXPORTER=otlp_proto_http
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
8+
Installation
9+
------------
10+
11+
::
12+
13+
python3 -m venv .venv
14+
source .venv/bin/activate
15+
pip install "python-dotenv[cli]"
16+
pip install -r requirements.txt
17+
18+
Running the Example
19+
-------------------
20+
21+
Update the `.env` file with your OpenAI API key, or to change where to export
22+
traces and logs. Then, run the example like this:
23+
24+
::
25+
26+
dotenv run -- opentelemetry-instrument python main.py
27+
28+
You should see a poem generated by OpenAI, with traces and logs exported to
29+
and OTLP compatible endpoint on localhost. You can then view them in your
30+
preferred observability tool.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
# TODO: remove after opentelemetry-sdk 1.28.2
6+
from opentelemetry import _events
7+
from opentelemetry.sdk._events import EventLoggerProvider
8+
9+
_events.set_event_logger_provider(EventLoggerProvider())
10+
11+
12+
def main():
13+
client = OpenAI()
14+
messages = [
15+
{
16+
"role": "user",
17+
"content": "Write a short poem on OpenTelemetry.",
18+
},
19+
]
20+
model = os.getenv("CHAT_MODEL", "gpt-4o-mini")
21+
chat_completion = client.chat.completions.create(model=model, messages=messages)
22+
print(chat_completion.choices[0].message.content)
23+
24+
25+
if __name__ == "__main__":
26+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openai~=1.54.4
2+
3+
opentelemetry-sdk~=1.28.1
4+
opentelemetry-exporter-otlp-proto-http~=1.28.1
5+
opentelemetry-distro~=0.49b1
6+
opentelemetry-instrumentation-openai-v2~=2.0b0

0 commit comments

Comments
 (0)