diff --git a/src/Elastic.OpenTelemetry.Core/Configuration/CompositeElasticOpenTelemetryOptions.cs b/src/Elastic.OpenTelemetry.Core/Configuration/CompositeElasticOpenTelemetryOptions.cs index ba1f718..2a9a512 100644 --- a/src/Elastic.OpenTelemetry.Core/Configuration/CompositeElasticOpenTelemetryOptions.cs +++ b/src/Elastic.OpenTelemetry.Core/Configuration/CompositeElasticOpenTelemetryOptions.cs @@ -551,7 +551,7 @@ internal void LogConfigSources(ILogger logger) LogConfig(logger, _skipInstrumentationAssemblyScanning); LogConfig(logger, _opAmpEndpoint); - // TODO - Log headers but redact auth header value + LogConfig(logger, _opAmpHeaders); LogConfig(logger, _resourceAttributes); static void LogConfig(ILogger logger, ConfigCell cell) diff --git a/src/Elastic.OpenTelemetry.Core/Configuration/ConfigCell.cs b/src/Elastic.OpenTelemetry.Core/Configuration/ConfigCell.cs index c543698..3aea006 100644 --- a/src/Elastic.OpenTelemetry.Core/Configuration/ConfigCell.cs +++ b/src/Elastic.OpenTelemetry.Core/Configuration/ConfigCell.cs @@ -6,13 +6,13 @@ namespace Elastic.OpenTelemetry.Configuration; internal class ConfigCell { - internal ConfigCell(string key, T value) + internal ConfigCell(string key, T? value) { Key = key; Value = value; } - internal ConfigCell(string key, T value, Func valueRedactor) + internal ConfigCell(string key, T? value, Func valueRedactor) { Key = key; Value = value; @@ -21,9 +21,9 @@ internal ConfigCell(string key, T value, Func valueRedactor) internal string Key { get; } - internal Func ValueRedactor { get; } = original => original; + internal Func ValueRedactor { get; } = original => original; - internal T Value { get; private set; } + internal T? Value { get; private set; } internal ConfigSource Source { get; private set; } = ConfigSource.Default; @@ -45,5 +45,5 @@ private void Assign(T value, ConfigSource source) Source = source; } - public override string ToString() => $"{Key}: '{ValueRedactor(Value)}' from [{Source}]"; + public override string ToString() => $"{Key}: '{(Value == null ? "" : ValueRedactor(Value))}' from [{Source}]"; } diff --git a/tests/Elastic.OpenTelemetry.Tests/Configuration/CompositeElasticOpenTelemetryOptionsTests.cs b/tests/Elastic.OpenTelemetry.Tests/Configuration/CompositeElasticOpenTelemetryOptionsTests.cs index 8cefa26..6e8c8bf 100644 --- a/tests/Elastic.OpenTelemetry.Tests/Configuration/CompositeElasticOpenTelemetryOptionsTests.cs +++ b/tests/Elastic.OpenTelemetry.Tests/Configuration/CompositeElasticOpenTelemetryOptionsTests.cs @@ -14,7 +14,7 @@ namespace Elastic.OpenTelemetry.Tests.Configuration; public class CompositeElasticOpenTelemetryOptionsTests(ITestOutputHelper output) { - private const int ExpectedLogsLength = 11; + private const int ExpectedLogsLength = 12; [Fact] public void DefaultCtor_SetsExpectedDefaults_WhenNoEnvironmentVariablesAreConfigured() @@ -365,6 +365,26 @@ public void TwoInstancesAreNotEqual_WhenValuesDoNotMatch() Assert.NotEqual(options1, options2); } + [Fact] + public void LogValueRedaction_WorksAsExpected() + { + var options = new ElasticOpenTelemetryOptions + { + OpAmpClientOptions = new OpAmpClientOptions + { + Endpoint = "http://my-endpoint.com", + Headers = "Custom=123,Authorization=ApiKey ABC123,Custom2=ABC" + } + }; + + var sut = new CompositeElasticOpenTelemetryOptions(options); + var logger = new TestLogger(output); + + sut.LogConfigSources(logger); + + Assert.Contains(logger.Messages, s => s.EndsWith("Configured value for OpAmpHeaders: 'Custom=123,Authorization=,Custom2=ABC' from [Options]", StringComparison.Ordinal)); + } + [Theory] [MemberData(nameof(OpAmpIsEnabledTestData))] public void IsOpAmpEnabled_ReturnsExpectedValue(IConfiguration configuration, ElasticOpenTelemetryOptions options, IDictionary dictionary, bool isEnabled)