-
Notifications
You must be signed in to change notification settings - Fork 713
OpenAI instrumentation docs fixes #2988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bbdd07d
Add openai docs config and improve readme
lmolkova 926f842
up
lmolkova 2520d3d
Add manual sample, add no-content tests
lmolkova 0c27e0b
update headers
lmolkova 48f0b38
lint
lmolkova b442f8f
use grpc endpoint in openai samples, add extra env vars to readme
lmolkova b16159d
move distro fix to another PR
lmolkova 0d39525
nits
lmolkova 87e9f95
Ignore examples for pylint
lzchen 613c5a5
Update .pylintrc
lzchen 7c5b5b7
ignroe lint for example
lzchen fd33323
Fix README docs
lzchen ef50f98
Update openai.rst
lzchen 357ad1a
Update conf.py
lzchen 581baee
Update docs-requirements.txt
lzchen 4ce1bed
docs
lzchen b334a86
Merge branch 'main' into openai-docs-fixes
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
OpenTelemetry Python - OpenAI Instrumentation | ||
============================================ | ||
|
||
.. automodule:: opentelemetry.instrumentation.openai_v2 | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains hidden or 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
16 changes: 16 additions & 0 deletions
16
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/.env
This file contains hidden or 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,16 @@ | ||
# 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 | ||
|
||
# Uncomment and change to your OTLP endpoint | ||
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 | ||
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc | ||
|
||
OTEL_SERVICE_NAME=opentelemetry-python-openai | ||
|
||
# Change to 'false' to hide prompt and completion content | ||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true |
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/main.py
This file contains hidden or 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,52 @@ | ||
import os | ||
|
||
from openai import OpenAI | ||
|
||
# NOTE: OpenTelemetry Python Logs and Events APIs are in beta | ||
from opentelemetry import _events, _logs, trace | ||
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import ( | ||
OTLPLogExporter, | ||
) | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
OTLPSpanExporter, | ||
) | ||
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
from opentelemetry.sdk._events import EventLoggerProvider | ||
from opentelemetry.sdk._logs import LoggerProvider | ||
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
# configure tracing | ||
trace.set_tracer_provider(TracerProvider()) | ||
trace.get_tracer_provider().add_span_processor( | ||
BatchSpanProcessor(OTLPSpanExporter()) | ||
) | ||
|
||
# configure logging and events | ||
_logs.set_logger_provider(LoggerProvider()) | ||
_logs.get_logger_provider().add_log_record_processor( | ||
BatchLogRecordProcessor(OTLPLogExporter()) | ||
) | ||
_events.set_event_logger_provider(EventLoggerProvider()) | ||
|
||
# instrument OpenAI | ||
OpenAIInstrumentor().instrument() | ||
|
||
|
||
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() |
5 changes: 5 additions & 0 deletions
5
...umentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/requirements.txt
This file contains hidden or 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,5 @@ | ||
openai~=1.54.4 | ||
|
||
opentelemetry-sdk~=1.28.2 | ||
opentelemetry-exporter-otlp-proto-grpc~=1.28.2 | ||
opentelemetry-instrumentation-openai-v2~=2.0b0 |
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
...ion-genai/opentelemetry-instrumentation-openai-v2/examples/zero-code/README.rst
This file contains hidden or 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,48 @@ | ||
OpenTelemetry OpenAI Zero-Code Instrumentation Example | ||
============================================ | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This is an example of how to instrument OpenAI calls with zero code changes, | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
|
||
Note: `.env <.env>`_ file configures additional environment variables: | ||
|
||
- `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true` configures | ||
OpenTelemetry SDK to export logs and events. | ||
- `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true` configures | ||
OpenAI instrumentation to capture prompt and completion contents on | ||
events. | ||
- `OTEL_LOGS_EXPORTER=otlp` to specify exporter type. | ||
|
||
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:4317. 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. |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...tation-openai-v2/example/requirements.txt → ...ai-v2/examples/zero-code/requirements.txt
This file contains hidden or 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
openai~=1.54.4 | ||
|
||
opentelemetry-sdk~=1.28.2 | ||
opentelemetry-exporter-otlp-proto-http~=1.28.2 | ||
opentelemetry-exporter-otlp-proto-grpc~=1.28.2 | ||
opentelemetry-distro~=0.49b2 | ||
opentelemetry-instrumentation-openai-v2~=2.0b0 |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.