Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cache the AgentEnabled setting value. #1723

Merged
merged 6 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,31 @@ private int TryGetAppSettingAsIntWithDefault(string key, int defaultValue)

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

private bool _agentEnabled;

private static string _agentEnabledAppSettingString;
private static bool? _agentEnabledAppSettingRead;
private static bool _agentEnabledAppSettingParsed;


public virtual bool AgentEnabled
{
get
{
var agentEnabledAsString = _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled");
// read from app setting one time only and cache the result in a static
if (!_agentEnabledAppSettingRead.HasValue)
{
_agentEnabledAppSettingString = _configurationManagerStatic.GetAppSetting("NewRelic.AgentEnabled");
_agentEnabledAppSettingRead = true;

bool agentEnabled;
if (!bool.TryParse(agentEnabledAsString, out agentEnabled))
return _localConfiguration.agentEnabled;
_agentEnabledAppSettingParsed = bool.TryParse(_agentEnabledAppSettingString, out _agentEnabled);
}

// read from local config if we couldn't parse from app settings
if (!_agentEnabledAppSettingParsed)
_agentEnabled = _localConfiguration.agentEnabled;

return agentEnabled;
return _agentEnabled;
}
}

Expand Down Expand Up @@ -2681,6 +2695,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 @@ -56,9 +56,31 @@ public void SetUp()
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