Skip to content

Commit

Permalink
fix: Agent license key should be optional in Serverless mode. (#2499) (
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr authored May 22, 2024
1 parent f1a7bf6 commit 801edc3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/Agent/NewRelic/Agent/Core/AgentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ private void AssertAgentEnabled()
if (!Configuration.AgentEnabled)
throw new Exception(string.Format("The New Relic agent is disabled. Update {0} to re-enable it.", Configuration.AgentEnabledAt));

if ("REPLACE_WITH_LICENSE_KEY".Equals(Configuration.AgentLicenseKey))
throw new Exception("Please set your license key.");
if (!Configuration.ServerlessModeEnabled) // license key is not required to be set in serverless mode
{
if ("REPLACE_WITH_LICENSE_KEY".Equals(Configuration.AgentLicenseKey))
throw new Exception("Please set your license key.");
}
}

private void Initialize(bool serverlessModeEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ public string GetScript(IInternalTransaction transaction, string nonce)
}

var licenseKey = _configurationService.Configuration.AgentLicenseKey;
if (licenseKey == null)
throw new NullReferenceException(nameof(licenseKey));
if (licenseKey.Length <= 0)
throw new Exception("License key is empty");
// serverless mode doesn't require a license key, so this check can fail.
if (string.IsNullOrEmpty(licenseKey) || "REPLACE_WITH_LICENSE_KEY".Equals(licenseKey))
{
Log.Debug("Skipping RUM injection because license key is not set.");
return null;
}

var browserConfigurationData = GetBrowserConfigurationData(transaction, transactionMetricName, licenseKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,14 @@ public void GetScript_ReturnsNull_IfBrowserMonitoringJavaScriptAgentIsEmpty()
}

[Test]
public void GetScript_Throws_IfAgentLicenseKeyIsNull()
public void GetScript_ReturnsNull_IfAgentLicenseKeyIsNotSet()
{
Mock.Arrange(() => _configuration.AgentLicenseKey).Returns(null as string);
var transaction = BuildTestTransaction();

Assert.Throws<NullReferenceException>(() => _browserMonitoringScriptMaker.GetScript(transaction, null));
}

[Test]
public void GetScript_Throws_IfAgentLicenseKeyIsEmpty()
{
Mock.Arrange(() => _configuration.AgentLicenseKey).Returns(string.Empty);
var transaction = BuildTestTransaction();
var script = _browserMonitoringScriptMaker.GetScript(transaction, null);

Assert.Throws<Exception>(() => _browserMonitoringScriptMaker.GetScript(transaction, null));
Assert.That(script, Is.Null);
}

[Test]
Expand Down

0 comments on commit 801edc3

Please sign in to comment.