-
Notifications
You must be signed in to change notification settings - Fork 710
Add metrics to the Python OpenAI instrumentation #3180
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 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1e3fd4d
Add metrics to instrumentation
drewby ca3659f
Use semconv for metric
drewby 11d0a4d
Add histogram buckets test
drewby fd22316
Add histogram explicit boundaries example
drewby aa82c1e
Merge branch 'open-telemetry:main' into main
drewby c156fe0
Update documentation
drewby b77a4dc
Update CHANGELOG
drewby 1e0d6b7
Fix linting errors
drewby bf16dfe
Use metric name constant
drewby cc1e05e
Change Meters to Instruments. Add missing attributes.
drewby 41bfc2a
Shorten changlog description
drewby 4dae8b5
Check metrics created in error response
drewby 344c893
Move metric tests to seperate module
drewby d8997f0
Remove buckets example, add to main README
drewby 596a4d5
Merge branch 'open-telemetry:main' into main
drewby 9ab6c5b
Use correct error name
drewby beaae86
Merge branch 'open-telemetry:main' into main
drewby a172bbb
Merge branch 'main' into main
lzchen 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
14 changes: 14 additions & 0 deletions
14
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/buckets/.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,14 @@ | ||
# 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 | ||
|
38 changes: 38 additions & 0 deletions
38
...ation-genai/opentelemetry-instrumentation-openai-v2/examples/buckets/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,38 @@ | ||
OpenTelemetry OpenAI Instrumentation Example | ||
codefromthecrypt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
============================================ | ||
|
||
This is an example of how to instrument OpenAI calls when configuring OpenTelemetry SDK and Instrumentations manually for metrics. | ||
|
||
When `main.py <main.py>`_ is run, it exports metrics to an OTLP compatible endpoint. Metrics include details such as token usage and operation duration, with specific bucket boundaries for each metric. | ||
|
||
The bucket boundaries are defined as follows: | ||
|
||
- For `gen_ai.client.token.usage`: [1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864] | ||
- For `gen_ai.client.operation.duration`: [0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, 20.48, 40.96, 81.92] | ||
|
||
These are documented in the `OpenTelemetry GenAI Metrics documentation <https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-metrics/>`_. | ||
|
||
Setup | ||
----- | ||
|
||
Minimally, update the `.env <.env>`_ file with your "OPENAI_API_KEY". An OTLP compatible endpoint should be listening for metrics 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 -- python main.py | ||
|
||
You should see metrics being exported to your configured observability tool, with the specified bucket boundaries for token usage and operation duration. |
92 changes: 92 additions & 0 deletions
92
instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/buckets/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,92 @@ | ||
import os | ||
|
||
from openai import OpenAI | ||
|
||
from opentelemetry import metrics | ||
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( | ||
OTLPMetricExporter, | ||
) | ||
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
from opentelemetry.sdk.metrics import Histogram, MeterProvider | ||
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader | ||
from opentelemetry.sdk.metrics.view import ( | ||
ExplicitBucketHistogramAggregation, | ||
View, | ||
) | ||
|
||
# configure metrics | ||
metric_exporter = OTLPMetricExporter() | ||
metric_reader = PeriodicExportingMetricReader(metric_exporter) | ||
|
||
TokenUsageHistogramView = View( | ||
instrument_type=Histogram, | ||
instrument_name="gen_ai.client.token.usage", | ||
aggregation=ExplicitBucketHistogramAggregation( | ||
boundaries=[ | ||
1, | ||
4, | ||
16, | ||
64, | ||
256, | ||
1024, | ||
4096, | ||
16384, | ||
65536, | ||
262144, | ||
1048576, | ||
4194304, | ||
16777216, | ||
67108864, | ||
] | ||
), | ||
) | ||
|
||
DurationHistogramView = View( | ||
instrument_type=Histogram, | ||
instrument_name="gen_ai.client.operation.duration", | ||
aggregation=ExplicitBucketHistogramAggregation( | ||
boundaries=[ | ||
0.01, | ||
0.02, | ||
0.04, | ||
0.08, | ||
0.16, | ||
0.32, | ||
0.64, | ||
1.28, | ||
2.56, | ||
5.12, | ||
10.24, | ||
20.48, | ||
40.96, | ||
81.92, | ||
] | ||
), | ||
) | ||
|
||
meter_provider = MeterProvider( | ||
metric_readers=[metric_reader], | ||
views=[TokenUsageHistogramView, DurationHistogramView], | ||
) | ||
metrics.set_meter_provider(meter_provider) | ||
|
||
# instrument OpenAI | ||
OpenAIInstrumentor().instrument(meter_provider=meter_provider) | ||
|
||
|
||
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
...mentation-genai/opentelemetry-instrumentation-openai-v2/examples/buckets/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.57.3 | ||
|
||
opentelemetry-sdk~=1.29.0 | ||
opentelemetry-exporter-otlp-proto-grpc~=1.29.0 | ||
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
11 changes: 11 additions & 0 deletions
11
...telemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/meters.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,11 @@ | ||
from opentelemetry.semconv._incubating.metrics import gen_ai_metrics | ||
|
||
|
||
class Meters: | ||
drewby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, meter): | ||
self.operation_duration_histogram = ( | ||
gen_ai_metrics.create_gen_ai_client_operation_duration(meter) | ||
) | ||
self.token_usage_histogram = ( | ||
gen_ai_metrics.create_gen_ai_client_token_usage(meter) | ||
) |
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.