Skip to content

Commit

Permalink
feat: Enable configuration of ignored and expected HTTP status code e…
Browse files Browse the repository at this point in the history
…rrors 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
  • Loading branch information
nr-ahemsath committed May 17, 2024
1 parent ccde882 commit eeb574f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,7 @@ private static T ServerOverrides<T>(T server, T local) where T : class
return server ?? local;
}

private List<string> EnvironmentOverrides(List<string> local, params string[] environmentVariableNames)
private IEnumerable<string> EnvironmentOverrides(IEnumerable<string> local, params string[] environmentVariableNames)
{
var envValue = (environmentVariableNames ?? Enumerable.Empty<string>())
.Select(_environment.GetEnvironmentVariable)
Expand Down Expand Up @@ -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<string>();

ExpectedErrorsConfiguration = new ReadOnlyDictionary<string, IEnumerable<string>>(expectedErrorInfo);
ExpectedErrorMessagesForAgentSettings = new ReadOnlyDictionary<string, IEnumerable<string>>(expectedMessages);
Expand Down Expand Up @@ -2677,7 +2677,7 @@ private void ParseIgnoreErrorConfigurations()
}
}

var ignoreStatusCodes = _serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore;
IEnumerable<string> ignoreStatusCodes = EnvironmentOverrides(_serverConfiguration.RpmConfig.ErrorCollectorStatusCodesToIgnore, "NEW_RELIC_ERROR_COLLECTOR_IGNORE_ERROR_CODES");
if (ignoreStatusCodes == null)
{
ignoreStatusCodes = _localConfiguration.errorCollector.ignoreStatusCodes.code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })]
Expand Down

0 comments on commit eeb574f

Please sign in to comment.