OpenAI Instrumentation Package
Project description
OpenTelemetry Instrumentation for OpenAI
An OpenTelemetry instrumentation for the openai
client library.
This instrumentation currently only supports instrumenting the Chat completions APIs.
We currently support the following features:
sync
andasync
chat completions- Streaming support
- Functions calling with tools
- Client side metrics
- Following 1.27.0 Gen AI Semantic Conventions
Installation
pip install elastic-opentelemetry-instrumentation-openai
Usage
This instrumentation supports 0-code / auto instrumentation:
opentelemetry-instrument python use_openai.py
Or manual instrumentation:
import openai
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument()
# assumes at least the OPENAI_API_KEY environment variable set
client = openai.Client()
messages = [
{
"role": "user",
"content": "Answer in up to 3 words: Which ocean contains the canarian islands?",
}
]
chat_completion = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
Instrumentation specific environment variable configuration
ELASTIC_OTEL_GENAI_CAPTURE_CONTENT
(default:false
): when sets totrue
collect more informations about prompts and responses by enabling content capture
Development
We use pytest to execute tests written with the standard library unittest framework.
Test dependencies need to be installed before running.
python3 -m venv .venv
source .venv/bin/activate
pip install -r dev-requirements.txt
pytest
Refreshing HTTP payloads
We use VCR.py to automatically record HTTP responses from LLMs to reuse in tests without running the LLM. Refreshing HTTP payloads may be needed in these cases
- Adding a new unit test
- Extending a unit test with functionality that requires an up-to-date HTTP response
Integration tests default to using ollama, to avoid cost and leaking sensitive information. However, unit test recordings should use the authoritative OpenAI platform unless the test is about a specific portability corner case.
To refresh a test, delete its cassette file in tests/cassettes and make sure you have the following environment variables set:
OPENAI_API_KEY
- from https://platform.openai.com/settings/profile?tab=api-keys- It should look like
sk-...
- It should look like
OPENAI_ORG_ID
- from https://platform.openai.com/settings/organization/general- It should look like
org-...
- It should look like
OPENAI_PROJECT_ID
- from https://platform.openai.com/settings/profile (click Project)- It should look like
proj_...
- It should look like
If writing a new test, start with the test logic with no assertions. If extending an existing unit test rather than writing a new one, remove the corresponding recorded response from cassettes instead.
Then, run pytest
as normal. It will execute a request against the LLM and record it. Update the
test with correct assertions until it passes. Following executions of pytest
will use the recorded
response without querying the LLM.
License
This software is licensed under the Apache License, version 2 ("Apache-2.0").
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file elastic_opentelemetry_instrumentation_openai-0.3.0.tar.gz
.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.3.0.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6acc74ed383d99bdbab028dbfe5b6db06febdfe0d6e6d627d4f3b5f931c5514c |
|
MD5 | d33a55a381e49dbfe4495c3ffe40f297 |
|
BLAKE2b-256 | b1b5c3636aff31923dcefd0398faac125d381cb6bc0945d2e63b3a4a4300c010 |
Provenance
The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.3.0.tar.gz
:
Publisher:
release-openai.yml
on elastic/elastic-otel-python-instrumentations
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
elastic_opentelemetry_instrumentation_openai-0.3.0.tar.gz
- Subject digest:
6acc74ed383d99bdbab028dbfe5b6db06febdfe0d6e6d627d4f3b5f931c5514c
- Sigstore transparency entry: 140931974
- Sigstore integration time:
- Predicate type:
File details
Details for the file elastic_opentelemetry_instrumentation_openai-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: elastic_opentelemetry_instrumentation_openai-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a922da07e933bdcbebf6df1b8f925f2f711dadbee3ae65fbcbd25af43f2c703 |
|
MD5 | 1722a3a4007a1a9e59b5a59595f08317 |
|
BLAKE2b-256 | 08d73c551c842c62dbcbc8c31c8e1d9bb3a0dd930055466ddc8ede030c08c815 |
Provenance
The following attestation bundles were made for elastic_opentelemetry_instrumentation_openai-0.3.0-py3-none-any.whl
:
Publisher:
release-openai.yml
on elastic/elastic-otel-python-instrumentations
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
elastic_opentelemetry_instrumentation_openai-0.3.0-py3-none-any.whl
- Subject digest:
3a922da07e933bdcbebf6df1b8f925f2f711dadbee3ae65fbcbd25af43f2c703
- Sigstore transparency entry: 140931981
- Sigstore integration time:
- Predicate type: