Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

drewby
Copy link
Member

@drewby drewby commented Jan 10, 2025

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 - Documentation
gen_ai.client.operation.duration - Documentation

There 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.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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

  • test_chat_completion_metrics - tests that metrics are correctly emitted when calling OpenAI synchronously.
  • test_async_chat_completion_metrics - tests that metrics are correctly emitted when calling OpenAI asynchronously.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link
Contributor

@xrmx xrmx left a 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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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(
Copy link
Contributor

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?

@@ -0,0 +1,38 @@
OpenTelemetry OpenAI Instrumentation Example
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add metrics to the Python OpenAI instrumentation
9 participants