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.
OpenAI instrumentation docs fixes (open-telemetry#2988)
* Add openai docs config and improve readme * up * Add manual sample, add no-content tests * update headers * lint * use grpc endpoint in openai samples, add extra env vars to readme * move distro fix to another PR * nits * Ignore examples for pylint * Update .pylintrc * ignroe lint for example * Fix README docs * Update openai.rst * Update conf.py * Update docs-requirements.txt * docs --------- Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
- Loading branch information
1 parent
8656a06
commit 142b86c
Showing
21 changed files
with
555 additions
and
14 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
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 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
16 changes: 16 additions & 0 deletions
16
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/.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,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 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
53 changes: 53 additions & 0 deletions
53
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual/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,53 @@ | ||
# pylint: skip-file | ||
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 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 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 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 | ||
====================================================== | ||
|
||
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. | ||
|
||
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 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 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
Oops, something went wrong.