openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
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 | |
| 6 | OpenAI .NET library is instrumented with distributed tracing and metrics using .NET [tracing](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing) |
| 7 | and [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 | |
| 9 | OpenAI .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 | |
| 13 | The instrumentation is **experimental** - volume and semantics of the telemetry items may change. |
| 14 | |
| 15 | To enable the instrumentation: |
| 16 | |
| 17 | 1. 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 | |
| 27 | 2. 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 | |
| 49 | Consider enabling [HTTP client instrumentation](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http) to see all HTTP client |
| 50 | calls made by your application including those done by the OpenAI SDK. |
| 51 | Check out [OpenTelemetry documentation](https://opentelemetry.io/docs/languages/net/getting-started/) for more details. |
| 52 | |
| 53 | ### Available sources and meters |
| 54 | |
| 55 | The 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 | |