From eeb574f3adfa1d90fcf39cd68b11e936d28292b4 Mon Sep 17 00:00:00 2001 From: Alex Hemsath <57361211+nr-ahemsath@users.noreply.github.com> Date: Fri, 17 May 2024 13:28:23 -0700 Subject: [PATCH] feat: Enable configuration of ignored and expected HTTP status code errors with environment variables (#2487) * Environment override for expected error status codes * Ignored status codes from env var * Improve unit tests * List->Enumerable, save on ToList() calls * Add a test for range setting via env var --- .../Configuration/DefaultConfiguration.cs | 8 ++--- .../DefaultConfigurationTests.cs | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs b/src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs index 958b30f65f..1df9bb67a5 100644 --- a/src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs +++ b/src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs @@ -2382,7 +2382,7 @@ private static T ServerOverrides(T server, T local) where T : class return server ?? local; } - private List EnvironmentOverrides(List local, params string[] environmentVariableNames) + private IEnumerable EnvironmentOverrides(IEnumerable local, params string[] environmentVariableNames) { var envValue = (environmentVariableNames ?? Enumerable.Empty()) .Select(_environment.GetEnvironmentVariable) @@ -2628,10 +2628,10 @@ private void ParseExpectedErrorConfigurations() var expectedStatusCodesArrayLocal = _localConfiguration.errorCollector.expectedStatusCodes?.Split(StringSeparators.Comma, StringSplitOptions.RemoveEmptyEntries); var expectedStatusCodesArrayServer = _serverConfiguration.RpmConfig.ErrorCollectorExpectedStatusCodes; - var expectedStatusCodesArray = ServerOverrides(expectedStatusCodesArrayServer, expectedStatusCodesArrayLocal); + var expectedStatusCodesArray = EnvironmentOverrides(ServerOverrides(expectedStatusCodesArrayServer, expectedStatusCodesArrayLocal), "NEW_RELIC_ERROR_COLLECTOR_EXPECTED_ERROR_CODES"); ExpectedStatusCodes = ParseExpectedStatusCodesArray(expectedStatusCodesArray); - ExpectedErrorStatusCodesForAgentSettings = expectedStatusCodesArray ?? new string[0]; + ExpectedErrorStatusCodesForAgentSettings = expectedStatusCodesArray ?? new List(); ExpectedErrorsConfiguration = new ReadOnlyDictionary>(expectedErrorInfo); ExpectedErrorMessagesForAgentSettings = new ReadOnlyDictionary>(expectedMessages); @@ -2677,7 +2677,7 @@ private void ParseIgnoreErrorConfigurations() } } - var ignoreStatusCodes = _serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore; + IEnumerable ignoreStatusCodes = EnvironmentOverrides(_serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore, "NEW_RELIC_ERROR_COLLECTOR_IGNORE_ERROR_CODES"); if (ignoreStatusCodes == null) { ignoreStatusCodes = _localConfiguration.errorCollector.ignoreStatusCodes.code diff --git a/tests/Agent/UnitTests/Core.UnitTest/Configuration/DefaultConfigurationTests.cs b/tests/Agent/UnitTests/Core.UnitTest/Configuration/DefaultConfigurationTests.cs index 2571710761..f0d728441c 100644 --- a/tests/Agent/UnitTests/Core.UnitTest/Configuration/DefaultConfigurationTests.cs +++ b/tests/Agent/UnitTests/Core.UnitTest/Configuration/DefaultConfigurationTests.cs @@ -999,19 +999,44 @@ public string IgnoreErrorsAndIgnoreClassesCombineTests(string[] ignoreClasses, s return string.Join(",", _defaultConfig.IgnoreErrorsConfiguration.Keys); } - [TestCase("401", new[] { "405" }, ExpectedResult = new[] { "405" })] - [TestCase("401", new string[0], ExpectedResult = new string[0])] - [TestCase("401", null, ExpectedResult = new[] { "401" })] - public string[] ExpectedStatusCodesSetFromLocalAndServerOverrides(string local, string[] server) + [TestCase("401", new[] { "405" }, null, ExpectedResult = new[] { "405" })] + [TestCase("401", new string[0], null, ExpectedResult = new string[0])] + [TestCase("401", null, null, ExpectedResult = new[] { "401" })] + [TestCase(null, null, "401", ExpectedResult = new[] { "401" })] + [TestCase(null, new[] { "405" }, "401", ExpectedResult = new[] { "401" })] + [TestCase("402", new string[0], "401", ExpectedResult = new[] { "401" })] + [TestCase("402", new string[0], "401, 503", ExpectedResult = new[] { "401", "503" })] + [TestCase("402", new string[0], "401, 500-505", ExpectedResult = new[] { "401", "500-505" })] + public string[] ExpectedStatusCodesSetFromLocalServerAndEnvironmentOverrides(string local, string[] server, string env) { _serverConfig.RpmConfig.ErrorCollectorExpectedStatusCodes = server; _localConfig.errorCollector.expectedStatusCodes = (local); + Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_ERROR_COLLECTOR_EXPECTED_ERROR_CODES")).Returns(env); CreateDefaultConfiguration(); return _defaultConfig.ExpectedErrorStatusCodesForAgentSettings.ToArray(); } + [TestCase(new[] { 401f }, new[] { "405" }, null, ExpectedResult = new[] { "405" })] + [TestCase(new[] { 401f }, new string[0], null, ExpectedResult = new string[0])] + [TestCase(new[] { 401f }, null, null, ExpectedResult = new[] { "401" })] + [TestCase(new[] { 401.5f }, null, null, ExpectedResult = new[] { "401.5" })] + [TestCase(new float[0], null, "401", ExpectedResult = new[] { "401" })] + [TestCase(new float[0], new[] { "405" }, "401", ExpectedResult = new[] { "401" })] + [TestCase(new[] { 401f }, new string[0], "402", ExpectedResult = new[] { "402" })] + [TestCase(new[] { 401f }, new string[0], "401.5, 503", ExpectedResult = new[] { "401.5", "503" })] + public string[] IgnoredStatusCodesSetFromLocalServerAndEnvironmentOverrides(float[] local, string[] server, string env) + { + _serverConfig.RpmConfig.ErrorCollectorStatusCodesToIgnore = server; + _localConfig.errorCollector.ignoreStatusCodes.code = (local.ToList()); + Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_ERROR_COLLECTOR_IGNORE_ERROR_CODES")).Returns(env); + + CreateDefaultConfiguration(); + + return _defaultConfig.HttpStatusCodesToIgnore.ToArray(); + } + [TestCase("401-404", new string[] { "401.5", "402.3" }, new bool[] { false, false })] //does not support full status codes [TestCase("400,401,404", new string[] { "400", "401", "402", "403", "404" }, new bool[] { true, true, false, false, true })] [TestCase("400, 401 ,404", new string[] { "400", "401", "402", "403", "404" }, new bool[] { true, true, false, false, true })]