openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Utility/Telemetry/OpenTelemetrySource.cs
30lines · modecode
| 1 | using OpenAI.Chat; |
| 2 | using System; |
| 3 | |
| 4 | namespace OpenAI.Telemetry; |
| 5 | |
| 6 | internal class OpenTelemetrySource |
| 7 | { |
| 8 | private const string ChatOperationName = "chat"; |
| 9 | private readonly bool IsOTelEnabled = AppContextSwitchHelper |
| 10 | .GetConfigValue("OpenAI.Experimental.EnableOpenTelemetry", "OPENAI_EXPERIMENTAL_ENABLE_OPEN_TELEMETRY"); |
| 11 | |
| 12 | private readonly string _serverAddress; |
| 13 | private readonly int _serverPort; |
| 14 | private readonly string _model; |
| 15 | |
| 16 | public OpenTelemetrySource(string model, Uri endpoint) |
| 17 | { |
| 18 | _serverAddress = endpoint.Host; |
| 19 | _serverPort = endpoint.Port; |
| 20 | _model = model; |
| 21 | } |
| 22 | |
| 23 | public OpenTelemetryScope StartChatScope(ChatCompletionOptions completionsOptions) |
| 24 | { |
| 25 | return IsOTelEnabled |
| 26 | ? OpenTelemetryScope.StartChat(_model, ChatOperationName, _serverAddress, _serverPort, completionsOptions) |
| 27 | : null; |
| 28 | } |
| 29 | |
| 30 | } |
| 31 | |