Skip to content

Commit eeb574f

Browse files
authored
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
1 parent ccde882 commit eeb574f

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ private static T ServerOverrides<T>(T server, T local) where T : class
23822382
return server ?? local;
23832383
}
23842384

2385-
private List<string> EnvironmentOverrides(List<string> local, params string[] environmentVariableNames)
2385+
private IEnumerable<string> EnvironmentOverrides(IEnumerable<string> local, params string[] environmentVariableNames)
23862386
{
23872387
var envValue = (environmentVariableNames ?? Enumerable.Empty<string>())
23882388
.Select(_environment.GetEnvironmentVariable)
@@ -2628,10 +2628,10 @@ private void ParseExpectedErrorConfigurations()
26282628
var expectedStatusCodesArrayLocal = _localConfiguration.errorCollector.expectedStatusCodes?.Split(StringSeparators.Comma, StringSplitOptions.RemoveEmptyEntries);
26292629
var expectedStatusCodesArrayServer = _serverConfiguration.RpmConfig.ErrorCollectorExpectedStatusCodes;
26302630

2631-
var expectedStatusCodesArray = ServerOverrides(expectedStatusCodesArrayServer, expectedStatusCodesArrayLocal);
2631+
var expectedStatusCodesArray = EnvironmentOverrides(ServerOverrides(expectedStatusCodesArrayServer, expectedStatusCodesArrayLocal), "NEW_RELIC_ERROR_COLLECTOR_EXPECTED_ERROR_CODES");
26322632

26332633
ExpectedStatusCodes = ParseExpectedStatusCodesArray(expectedStatusCodesArray);
2634-
ExpectedErrorStatusCodesForAgentSettings = expectedStatusCodesArray ?? new string[0];
2634+
ExpectedErrorStatusCodesForAgentSettings = expectedStatusCodesArray ?? new List<string>();
26352635

26362636
ExpectedErrorsConfiguration = new ReadOnlyDictionary<string, IEnumerable<string>>(expectedErrorInfo);
26372637
ExpectedErrorMessagesForAgentSettings = new ReadOnlyDictionary<string, IEnumerable<string>>(expectedMessages);
@@ -2677,7 +2677,7 @@ private void ParseIgnoreErrorConfigurations()
26772677
}
26782678
}
26792679

2680-
var ignoreStatusCodes = _serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore;
2680+
IEnumerable<string> ignoreStatusCodes = EnvironmentOverrides(_serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore, "NEW_RELIC_ERROR_COLLECTOR_IGNORE_ERROR_CODES");
26812681
if (ignoreStatusCodes == null)
26822682
{
26832683
ignoreStatusCodes = _localConfiguration.errorCollector.ignoreStatusCodes.code

tests/Agent/UnitTests/Core.UnitTest/Configuration/DefaultConfigurationTests.cs

+29-4
Original file line numberDiff line numberDiff line change
@@ -999,19 +999,44 @@ public string IgnoreErrorsAndIgnoreClassesCombineTests(string[] ignoreClasses, s
999999
return string.Join(",", _defaultConfig.IgnoreErrorsConfiguration.Keys);
10001000
}
10011001

1002-
[TestCase("401", new[] { "405" }, ExpectedResult = new[] { "405" })]
1003-
[TestCase("401", new string[0], ExpectedResult = new string[0])]
1004-
[TestCase("401", null, ExpectedResult = new[] { "401" })]
1005-
public string[] ExpectedStatusCodesSetFromLocalAndServerOverrides(string local, string[] server)
1002+
[TestCase("401", new[] { "405" }, null, ExpectedResult = new[] { "405" })]
1003+
[TestCase("401", new string[0], null, ExpectedResult = new string[0])]
1004+
[TestCase("401", null, null, ExpectedResult = new[] { "401" })]
1005+
[TestCase(null, null, "401", ExpectedResult = new[] { "401" })]
1006+
[TestCase(null, new[] { "405" }, "401", ExpectedResult = new[] { "401" })]
1007+
[TestCase("402", new string[0], "401", ExpectedResult = new[] { "401" })]
1008+
[TestCase("402", new string[0], "401, 503", ExpectedResult = new[] { "401", "503" })]
1009+
[TestCase("402", new string[0], "401, 500-505", ExpectedResult = new[] { "401", "500-505" })]
1010+
public string[] ExpectedStatusCodesSetFromLocalServerAndEnvironmentOverrides(string local, string[] server, string env)
10061011
{
10071012
_serverConfig.RpmConfig.ErrorCollectorExpectedStatusCodes = server;
10081013
_localConfig.errorCollector.expectedStatusCodes = (local);
1014+
Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_ERROR_COLLECTOR_EXPECTED_ERROR_CODES")).Returns(env);
10091015

10101016
CreateDefaultConfiguration();
10111017

10121018
return _defaultConfig.ExpectedErrorStatusCodesForAgentSettings.ToArray();
10131019
}
10141020

1021+
[TestCase(new[] { 401f }, new[] { "405" }, null, ExpectedResult = new[] { "405" })]
1022+
[TestCase(new[] { 401f }, new string[0], null, ExpectedResult = new string[0])]
1023+
[TestCase(new[] { 401f }, null, null, ExpectedResult = new[] { "401" })]
1024+
[TestCase(new[] { 401.5f }, null, null, ExpectedResult = new[] { "401.5" })]
1025+
[TestCase(new float[0], null, "401", ExpectedResult = new[] { "401" })]
1026+
[TestCase(new float[0], new[] { "405" }, "401", ExpectedResult = new[] { "401" })]
1027+
[TestCase(new[] { 401f }, new string[0], "402", ExpectedResult = new[] { "402" })]
1028+
[TestCase(new[] { 401f }, new string[0], "401.5, 503", ExpectedResult = new[] { "401.5", "503" })]
1029+
public string[] IgnoredStatusCodesSetFromLocalServerAndEnvironmentOverrides(float[] local, string[] server, string env)
1030+
{
1031+
_serverConfig.RpmConfig.ErrorCollectorStatusCodesToIgnore = server;
1032+
_localConfig.errorCollector.ignoreStatusCodes.code = (local.ToList());
1033+
Mock.Arrange(() => _environment.GetEnvironmentVariable("NEW_RELIC_ERROR_COLLECTOR_IGNORE_ERROR_CODES")).Returns(env);
1034+
1035+
CreateDefaultConfiguration();
1036+
1037+
return _defaultConfig.HttpStatusCodesToIgnore.ToArray();
1038+
}
1039+
10151040
[TestCase("401-404", new string[] { "401.5", "402.3" }, new bool[] { false, false })] //does not support full status codes
10161041
[TestCase("400,401,404", new string[] { "400", "401", "402", "403", "404" }, new bool[] { true, true, false, false, true })]
10171042
[TestCase("400, 401 ,404", new string[] { "400", "401", "402", "403", "404" }, new bool[] { true, true, false, false, true })]

0 commit comments

Comments
 (0)