Skip to content

Commit

Permalink
fix: Cache the AgentEnabled setting value. (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr authored Jun 15, 2023
1 parent 269023c commit 1624938
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,28 @@ private int TryGetAppSettingAsIntWithDefault(string key, int defaultValue)

public object AgentRunId { get { return _serverConfiguration.AgentRunId; } }

// protected to allow unit test wrapper to manipulate
protected static bool? _agentEnabledAppSettingParsed;
protected static bool _appSettingAgentEnabled;
private static readonly object _lockObj = new object();


public virtual bool AgentEnabled
{
get
{
var agentEnabledAsString = _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled");

bool agentEnabled;
if (!bool.TryParse(agentEnabledAsString, out agentEnabled))
return _localConfiguration.agentEnabled;
// read from app setting one time only and cache the result
if (!_agentEnabledAppSettingParsed.HasValue)
{
lock (_lockObj)
{
_agentEnabledAppSettingParsed ??= bool.TryParse(_configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled"),
out _appSettingAgentEnabled);
}
}

return agentEnabled;
// read from local config if we couldn't parse from app settings
return _agentEnabledAppSettingParsed.Value ? _appSettingAgentEnabled : _localConfiguration.agentEnabled;
}
}

Expand Down Expand Up @@ -2681,6 +2692,7 @@ private void LogDisabledPropertyUse(string disabledPropertyName, string newPrope
TryGetAppSettingAsIntWithDefault("SqlStatementCacheCapacity", DefaultSqlStatementCacheCapacity)).Value;

private bool? _codeLevelMetricsEnabled;

public bool CodeLevelMetricsEnabled
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ internal class TestableDefaultConfiguration : DefaultConfiguration
{
public TestableDefaultConfiguration(IEnvironment environment, configuration localConfig, ServerConfiguration serverConfig, RunTimeConfiguration runTimeConfiguration, SecurityPoliciesConfiguration securityPoliciesConfiguration, IProcessStatic processStatic, IHttpRuntimeStatic httpRuntimeStatic, IConfigurationManagerStatic configurationManagerStatic, IDnsStatic dnsStatic)
: base(environment, localConfig, serverConfig, runTimeConfiguration, securityPoliciesConfiguration, processStatic, httpRuntimeStatic, configurationManagerStatic, dnsStatic) { }

public static void ResetStatics()
{
_agentEnabledAppSettingParsed = null;
_appSettingAgentEnabled = false;
}
}

[TestFixture, Category("Configuration")]
Expand Down Expand Up @@ -50,15 +56,40 @@ public void SetUp()
_dnsStatic = Mock.Create<IDnsStatic>();

_defaultConfig = new TestableDefaultConfiguration(_environment, _localConfig, _serverConfig, _runTimeConfig, _securityPoliciesConfiguration, _processStatic, _httpRuntimeStatic, _configurationManagerStatic, _dnsStatic);

TestableDefaultConfiguration.ResetStatics();
}

[Test]
public void AgentEnabledShouldPassThroughToLocalConfig()
{
Assert.IsTrue(_defaultConfig.AgentEnabled);

_localConfig.agentEnabled = false;
Assert.IsFalse(_defaultConfig.AgentEnabled);

_localConfig.agentEnabled = true;
Assert.IsTrue(_defaultConfig.AgentEnabled);
_localConfig.agentEnabled = false;
}

[Test]
public void AgentEnabledShouldUseCachedAppSetting()
{
Mock.Arrange(() => _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled")).Returns("false");

Assert.IsFalse(_defaultConfig.AgentEnabled);
Assert.IsFalse(_defaultConfig.AgentEnabled);

Mock.Assert(() => _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled"), Occurs.Once());
}

[Test]
public void AgentEnabledShouldPreferAppSettingOverLocalConfig()
{
Mock.Arrange(() => _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled")).Returns("false");

_localConfig.agentEnabled = true;

Assert.IsFalse(_defaultConfig.AgentEnabled);
}

Expand Down

0 comments on commit 1624938

Please sign in to comment.