diff --git a/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj b/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj index 418225a47c1..836c1d9d11c 100644 --- a/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj +++ b/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Components/Aspire.OpenAI/OpenAISettings.cs b/src/Components/Aspire.OpenAI/OpenAISettings.cs index 8dd4cc5c570..1f9b80f8c93 100644 --- a/src/Components/Aspire.OpenAI/OpenAISettings.cs +++ b/src/Components/Aspire.OpenAI/OpenAISettings.cs @@ -40,13 +40,16 @@ public sealed class OpenAISettings /// /// if potentially sensitive information should be included in telemetry; /// if telemetry shouldn't include raw inputs and outputs. - /// The default value is . + /// The default value is , unless the OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + /// environment variable is set to "true" (case-insensitive). /// /// /// By default, telemetry includes metadata, such as token counts, but not raw inputs /// and outputs, such as message content, function call arguments, and function call results. + /// The default value can be overridden by setting the OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT + /// environment variable to "true". Explicitly setting this property will override the environment variable. /// - public bool EnableSensitiveTelemetryData { get; set; } + public bool EnableSensitiveTelemetryData { get; set; } = TelemetryHelpers.EnableSensitiveDataDefault; internal void ParseConnectionString(string? connectionString) { diff --git a/tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs b/tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs index 9afff0932d9..586b8762f9b 100644 --- a/tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs +++ b/tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs @@ -230,4 +230,46 @@ public void BindsToNamedClientOptions() Assert.NotNull(options); Assert.Equal("myproject2", options.ProjectId); } + + [Fact] + public void EnableSensitiveTelemetryData_DefaultsToEnvironmentVariable() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + builder.Configuration.AddInMemoryCollection([ + new KeyValuePair("ConnectionStrings:openai", ConnectionString) + ]); + + OpenAISettings? localSettings = null; + + builder.AddOpenAIClient("openai", settings => + { + localSettings = settings; + }); + + Assert.NotNull(localSettings); + // The default should be based on TelemetryHelpers.EnableSensitiveDataDefault + // which reads from OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable + // In test environment without the env var set, it should default to false + Assert.False(localSettings.EnableSensitiveTelemetryData); + } + + [Fact] + public void EnableSensitiveTelemetryData_CanBeOverriddenByConfiguration() + { + var builder = Host.CreateEmptyApplicationBuilder(null); + builder.Configuration.AddInMemoryCollection([ + new KeyValuePair("ConnectionStrings:openai", ConnectionString), + new KeyValuePair("Aspire:OpenAI:EnableSensitiveTelemetryData", "true") + ]); + + OpenAISettings? localSettings = null; + + builder.AddOpenAIClient("openai", settings => + { + localSettings = settings; + }); + + Assert.NotNull(localSettings); + Assert.True(localSettings.EnableSensitiveTelemetryData); + } }