openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docs/Observability.md

57lines · modecode

1## Observability with OpenTelemetry
2
3> Note:
4> OpenAI .NET SDK instrumentation is in development and is not complete. See [Available sources and meters](#available-sources-and-meters) section for the list of covered operations.
5
6OpenAI .NET library is instrumented with distributed tracing and metrics using .NET [tracing](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing)
7and [metrics](https://learn.microsoft.com/dotnet/core/diagnostics/metrics-instrumentation) API and supports [OpenTelemetry](https://learn.microsoft.com/dotnet/core/diagnostics/observability-with-otel).
8
9OpenAI .NET instrumentation follows [OpenTelemetry Semantic Conventions for Generative AI systems](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/gen-ai).
10
11### How to enable
12
13The instrumentation is **experimental** - volume and semantics of the telemetry items may change.
14
15To enable the instrumentation:
16
171. Set instrumentation feature-flag using one of the following options:
18
19 - set the `OPENAI_EXPERIMENTAL_ENABLE_OPEN_TELEMETRY` environment variable to `"true"`
20 - set the `OpenAI.Experimental.EnableOpenTelemetry` context switch to true in your application code when application
21 is starting and before initializing any OpenAI clients. For example:
22
23 ```csharp
24 AppContext.SetSwitch("OpenAI.Experimental.EnableOpenTelemetry", true);
25 ```
26
272. Enable OpenAI telemetry:
28
29 ```csharp
30 builder.Services.AddOpenTelemetry()
31 .WithTracing(b =>
32 {
33 b.AddSource("OpenAI.*")
34 ...
35 .AddOtlpExporter();
36 })
37 .WithMetrics(b =>
38 {
39 b.AddMeter("OpenAI.*")
40 ...
41 .AddOtlpExporter();
42 });
43 ```
44
45 Distributed tracing is enabled with `AddSource("OpenAI.*")` which tells OpenTelemetry to listen to all [ActivitySources](https://learn.microsoft.com/dotnet/api/system.diagnostics.activitysource) with names starting with `OpenAI.*`.
46
47 Similarly, metrics are configured with `AddMeter("OpenAI.*")` which enables all OpenAI-related [Meters](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter).
48
49Consider enabling [HTTP client instrumentation](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http) to see all HTTP client
50calls made by your application including those done by the OpenAI SDK.
51Check out [OpenTelemetry documentation](https://opentelemetry.io/docs/languages/net/getting-started/) for more details.
52
53### Available sources and meters
54
55The following sources and meters are available:
56
57- `OpenAI.ChatClient` - records traces and metrics for `ChatClient` operations (except streaming and protocol methods which are not instrumented yet)
58