forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into pymysql-sqlcomment
- Loading branch information
Showing
11 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/.env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
39 changes: 39 additions & 0 deletions
39
instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/README.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
OpenTelemetry OpenAI Instrumentation Example | ||
============================================ | ||
|
||
This is an example of how to instrument OpenAI calls with zero code changes, | ||
using `opentelemetry-instrument`. | ||
|
||
When `main.py <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. | ||
|
||
Next, set up a virtual environment like this: | ||
|
||
:: | ||
|
||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install "python-dotenv[cli]" | ||
pip install -r requirements.txt | ||
|
||
Run | ||
--- | ||
|
||
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. |
21 changes: 21 additions & 0 deletions
21
instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
6 changes: 6 additions & 0 deletions
6
instrumentation-genai/opentelemetry-instrumentation-openai-v2/example/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters