Add validation to disabled instrumentation and metric key (#1303)

This commit is contained in:
Rasmus Kuusmann 2022-09-29 16:08:25 +03:00 committed by GitHub
parent b752d79045
commit cb5e267584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -39,7 +39,7 @@ public class MetricSettings : Settings
EnabledInstrumentations = source.ParseEnabledEnumList<MetricInstrumentation>(
enabledConfiguration: ConfigurationKeys.Metrics.Instrumentations,
disabledConfiguration: ConfigurationKeys.Metrics.DisabledInstrumentations,
error: "The \"{0}\" is not recognized as supported metrics instrumentation and cannot be enabled");
error: "The \"{0}\" is not recognized as supported metrics instrumentation and cannot be enabled or disabled.");
var additionalSources = source.GetString(ConfigurationKeys.Metrics.AdditionalSources);
if (additionalSources != null)

View File

@ -40,7 +40,7 @@ public class TracerSettings : Settings
EnabledInstrumentations = source.ParseEnabledEnumList<TracerInstrumentation>(
enabledConfiguration: ConfigurationKeys.Traces.Instrumentations,
disabledConfiguration: ConfigurationKeys.Traces.DisabledInstrumentations,
error: "The \"{0}\" is not recognized as supported trace instrumentation and cannot be enabled");
error: "The \"{0}\" is not recognized as supported trace instrumentation and cannot be enabled or disabled.");
var additionalSources = source.GetString(ConfigurationKeys.Traces.AdditionalSources);
if (additionalSources != null)

View File

@ -61,7 +61,14 @@ internal static class ConfigurationSourceExtensions
{
foreach (var instrumentation in disabledInstrumentations.Split(Constants.ConfigurationValues.Separator))
{
instrumentations.Remove(instrumentation);
if (Enum.TryParse(instrumentation, out TEnum _))
{
instrumentations.Remove(instrumentation);
}
else
{
throw new FormatException(string.Format(error, instrumentation));
}
}
}

View File

@ -109,4 +109,21 @@ public class ConfigurationSourceExtensionsTests
act.Should().Throw<FormatException>()
.WithMessage("Invalid enum value: invalid");
}
[Fact]
public void ParseDisabledEnumList_Invalid()
{
var source = new NameValueConfigurationSource(new NameValueCollection()
{
{ "TEST_DISABLED_VALUES", "invalid" }
});
var act = () => source.ParseEnabledEnumList<TestEnum>(
enabledConfiguration: "TEST_ENABLED_VALUES",
disabledConfiguration: "TEST_DISABLED_VALUES",
error: "Invalid enum value: {0}");
act.Should().Throw<FormatException>()
.WithMessage("Invalid enum value: invalid");
}
}