-
Notifications
You must be signed in to change notification settings - Fork 637
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
Add metrics to the Python OpenAI instrumentation #3180
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of nits but LGTM. Not sure that setting the views for having proper buckets it's useful but no big deal.
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
- Add example to `opentelemetry-instrumentation-openai-v2` | |||
([#3006](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3006)) | |||
- Support for `AsyncOpenAI/AsyncCompletions` ([#2984](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2984)) | |||
- Add metrics to the Python OpenAI instrumentation ([#3180](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3180)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add metrics to the Python OpenAI instrumentation ([#3180](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3180)) | |
- Add metrics ([#3180](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3180)) |
@@ -23,6 +24,7 @@ | |||
) | |||
from opentelemetry.trace import Span, SpanKind, Tracer | |||
|
|||
from .meters import Meters # Import the Meters class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from .meters import Meters # Import the Meters class | |
from .meters import Meters |
@@ -52,6 +69,62 @@ def fixture_event_logger_provider(log_exporter): | |||
return event_logger_provider | |||
|
|||
|
|||
@pytest.fixture(scope="function", name="meter_provider") | |||
def fixture_meter_provider(metric_reader): | |||
token_usage_histogram_view = View( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am not sure adding the views is useful since it's outside of the instrumentation concern, maybe wait for advisory support to get in instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think without setting them you'd not get any value from metrics - all measurements in sec go to the smallest bucket :( So I'd rather have them initially and remove as unnecessary once advisory params are in.
@@ -0,0 +1,38 @@ | |||
OpenTelemetry OpenAI Instrumentation Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure there is anything openai specific here, should we document the views more generally instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, just a few comments on recording errors
from opentelemetry.semconv._incubating.metrics import gen_ai_metrics | ||
|
||
|
||
class Meters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
class Meters: | |
class Instruments: |
would be more precise
meters, | ||
duration, | ||
result, | ||
span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should pass error type and record it on the histogram
|
||
completion_attributes = { | ||
**common_attributes, | ||
GenAIAttributes.GEN_AI_TOKEN_TYPE: GenAIAttributes.GenAiTokenTypeValues.COMPLETION.value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one is deprecated, it should be OUTPUT
, no?
GenAIAttributes.GEN_AI_OPERATION_NAME: GenAIAttributes.GenAiOperationNameValues.CHAT.value, | ||
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value, | ||
GenAIAttributes.GEN_AI_REQUEST_MODEL: request_model, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also need to record error.type
, server.address
and server.port
similarly to spans
Also we have extra attributes defined for openai - https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/openai.md#metric-gen_aiclientoperationduration - can we populate them here too?
|
||
@pytest.mark.vcr() | ||
@pytest.mark.asyncio() | ||
async def test_async_chat_completion_metrics( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be nice to add metrics checks for failure cases (or just update existing tests to record and assert metrics too)
@@ -52,6 +69,62 @@ def fixture_event_logger_provider(log_exporter): | |||
return event_logger_provider | |||
|
|||
|
|||
@pytest.fixture(scope="function", name="meter_provider") | |||
def fixture_meter_provider(metric_reader): | |||
token_usage_histogram_view = View( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think without setting them you'd not get any value from metrics - all measurements in sec go to the smallest bucket :( So I'd rather have them initially and remove as unnecessary once advisory params are in.
Description
This PR implements the GenAI semantic conventions for the two client metrics so they are collected along with spans when instrumenting a Python application.
Basic implementation of two client metrics defined in the GenAI semantic conventions:
gen_ai.client.token.usage
- Documentationgen_ai.client.operation.duration
- DocumentationThere is an example added to show end users who to configure the explicit bucket boundaries as defined in the semantic convention spec.
Fixes #3177
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.