Fix configuration sources (#1724)
This commit is contained in:
parent
4096276663
commit
27e41c676e
|
|
@ -61,7 +61,7 @@ internal class CompositeConfigurationSource : IConfigurationSource, IEnumerable<
|
|||
public string GetString(string key)
|
||||
{
|
||||
return _sources.Select(source => source.GetString(key))
|
||||
.FirstOrDefault(value => value != null);
|
||||
.FirstOrDefault(value => !string.IsNullOrEmpty(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -24,8 +24,7 @@ namespace OpenTelemetry.AutoInstrumentation.Configuration;
|
|||
/// </summary>
|
||||
internal class EnvironmentConfigurationSource : StringConfigurationSource
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string GetString(string key)
|
||||
protected override string GetStringInternal(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -36,8 +36,7 @@ internal class NameValueConfigurationSource : StringConfigurationSource
|
|||
_nameValueCollection = nameValueCollection;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string GetString(string key)
|
||||
protected override string GetStringInternal(string key)
|
||||
{
|
||||
return _nameValueCollection[key];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,16 @@ namespace OpenTelemetry.AutoInstrumentation.Configuration;
|
|||
internal abstract class StringConfigurationSource : IConfigurationSource
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public abstract string GetString(string key);
|
||||
public virtual string GetString(string key)
|
||||
{
|
||||
var value = GetStringInternal(key);
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual int? GetInt32(string key)
|
||||
|
|
@ -53,4 +62,6 @@ internal abstract class StringConfigurationSource : IConfigurationSource
|
|||
var value = GetString(key);
|
||||
return bool.TryParse(value, out bool result) ? result : null;
|
||||
}
|
||||
|
||||
protected abstract string GetStringInternal(string key);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using Moq;
|
||||
using OpenTelemetry.AutoInstrumentation.Configuration;
|
||||
using OpenTelemetry.AutoInstrumentation.Util;
|
||||
using Xunit;
|
||||
|
|
@ -126,4 +128,51 @@ public class ConfigurationSourceExtensionsTests
|
|||
act.Should().Throw<FormatException>()
|
||||
.WithMessage("Invalid enum value: invalid");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseEmptyAsNull_StringConfigurationSource()
|
||||
{
|
||||
var source = new DummyStringConfigurationSource();
|
||||
|
||||
using (new AssertionScope())
|
||||
{
|
||||
source.GetString("TEST_NULL_VALUE").Should().BeNull();
|
||||
source.GetString("TEST_EMPTY_VALUE").Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseEmptyAsNull_CompositeConfigurationSource()
|
||||
{
|
||||
var mockSource = new Mock<IConfigurationSource>();
|
||||
var compositeSource = new CompositeConfigurationSource();
|
||||
|
||||
mockSource.Setup(x => x.GetString(It.Is<string>(x => x == "TEST_NULL_VALUE"))).Returns<string>(k => null);
|
||||
mockSource.Setup(x => x.GetString(It.Is<string>(x => x == "TEST_EMPTY_VALUE"))).Returns<string>(k => string.Empty);
|
||||
|
||||
compositeSource.Add(mockSource.Object);
|
||||
|
||||
using (new AssertionScope())
|
||||
{
|
||||
compositeSource.GetString("TEST_NULL_VALUE").Should().BeNull();
|
||||
compositeSource.GetString("TEST_EMPTY_VALUE").Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
private class DummyStringConfigurationSource : StringConfigurationSource
|
||||
{
|
||||
protected override string GetStringInternal(string key)
|
||||
{
|
||||
if (key == "TEST_NULL_VALUE")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (key == "TEST_EMPTY_VALUE")
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
throw new NotImplementedException($"Invalid key '{key}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue