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 instrumentation for simple convenience chat calls #107

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

Conversation

lmolkova
Copy link

@lmolkova lmolkova commented Jul 2, 2024

This PR builds foundation for OpenAI SDK tracing and metrics instrumentation (using Otel-compatible .NET primitives).

It's limited to convenience ChatClient methods without streaming. The PR implements instrumentation according to OpenTelemetry GenAI semantic conventions.

The intention is to add instrumentation to other methods and client types and evolve it along with OTel GenAI semantic conventions.

TODO (in this PR):

  • add samples/docs
  • add experimental feature-flag required to enable instrumentation - we don't know when OTel semantic conventions will be stable and expect breaking changes.

TODO (in next PRs):

  • add instrumentation to streaming calls and protocol methods
  • track prompts and completions in events
  • ...

_duration = Stopwatch.StartNew();
_commonTags = new TagList
{
{ Constants.GenAiSystemKey, Constants.GenAiSystemValue },

Choose a reason for hiding this comment

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

is this fully static? Perhaps a better fit for Resource?

Copy link
Author

@lmolkova lmolkova Jul 3, 2024

Choose a reason for hiding this comment

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

resource attributes are per-all-tracers + only otel SDK can set them up. Instrumentation scope attributes are not available on activity sources and in many other languages.

So it'd be nice to optimize it in the future, but it'd need changes in otel beyond gen-ai or even semconv.

@lmolkova lmolkova marked this pull request as ready for review July 5, 2024 23:14

1. Set instrumentation feature-flag using one of the following options:

- set the `AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE` environment variable to `"true"`

Choose a reason for hiding this comment

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

Curious, why its named "AZURE_..." ? Is this library connected to Azure SDKs in general?

calls made by your application including those done by the OpenAI SDK.
Check out [OpenTelemetry documentation](https://opentelemetry.io/docs/languages/net/getting-started/) for more details.

### Available sources and meters

Choose a reason for hiding this comment

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

it might not be obvious what are "sources" and "meters"..

{
private static readonly ActivitySource s_chatSource = new ActivitySource("OpenAI.ChatClient");
private static readonly Meter s_chatMeter = new Meter("OpenAI.ChatClient");
private static readonly Histogram<double> s_duration = s_chatMeter.CreateHistogram<double>(Constants.GenAiClientOperationDurationMetricName, "s", "Measures GenAI operation duration.");

Choose a reason for hiding this comment

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

please add a TODO to provide the explicit buckets, when DS 9.0 can be used.

if (inputTokensUsage != null)
{
// tags is a struct, let's copy them
TagList inputUsageTags = tags;

Choose a reason for hiding this comment

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

Token usage metrics has no attribute for error.type, but tags here will have error.type.


public TestActivityListener(string sourceName)
{
_listener = new ActivityListener()

Choose a reason for hiding this comment

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

if the tests can take OTel dependency sdk, then consider using InMemoryExporter for these tests.

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.

None yet

2 participants