Skip to content

Commit 936b6f6

Browse files
authored
feat: Add config option for providing AWS account ID for linking (#2851)
1 parent f24a5da commit 936b6f6

File tree

8 files changed

+219
-1
lines changed

8 files changed

+219
-1
lines changed

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

+88
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public partial class configuration
9898

9999
private configurationCodeLevelMetrics codeLevelMetricsField;
100100

101+
private configurationCloud cloudField;
102+
101103
private bool agentEnabledField;
102104

103105
private bool rootAgentEnabledField;
@@ -121,6 +123,7 @@ public partial class configuration
121123
/// </summary>
122124
public configuration()
123125
{
126+
this.cloudField = new configurationCloud();
124127
this.codeLevelMetricsField = new configurationCodeLevelMetrics();
125128
this.processHostField = new configurationProcessHost();
126129
this.utilizationField = new configurationUtilization();
@@ -600,6 +603,18 @@ public configurationCodeLevelMetrics codeLevelMetrics
600603
}
601604
}
602605

606+
public configurationCloud cloud
607+
{
608+
get
609+
{
610+
return this.cloudField;
611+
}
612+
set
613+
{
614+
this.cloudField = value;
615+
}
616+
}
617+
603618
/// <summary>
604619
/// Set this to true to enable the agent.
605620
/// </summary>
@@ -6013,6 +6028,79 @@ public virtual configurationCodeLevelMetrics Clone()
60136028
#endregion
60146029
}
60156030

6031+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.6.0.20097")]
6032+
[System.SerializableAttribute()]
6033+
[System.ComponentModel.DesignerCategoryAttribute("code")]
6034+
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:newrelic-config")]
6035+
public partial class configurationCloud
6036+
{
6037+
6038+
private configurationCloudAws awsField;
6039+
6040+
/// <summary>
6041+
/// configurationCloud class constructor
6042+
/// </summary>
6043+
public configurationCloud()
6044+
{
6045+
this.awsField = new configurationCloudAws();
6046+
}
6047+
6048+
public configurationCloudAws aws
6049+
{
6050+
get
6051+
{
6052+
return this.awsField;
6053+
}
6054+
set
6055+
{
6056+
this.awsField = value;
6057+
}
6058+
}
6059+
6060+
#region Clone method
6061+
/// <summary>
6062+
/// Create a clone of this configurationCloud object
6063+
/// </summary>
6064+
public virtual configurationCloud Clone()
6065+
{
6066+
return ((configurationCloud)(this.MemberwiseClone()));
6067+
}
6068+
#endregion
6069+
}
6070+
6071+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.6.0.20097")]
6072+
[System.SerializableAttribute()]
6073+
[System.ComponentModel.DesignerCategoryAttribute("code")]
6074+
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:newrelic-config")]
6075+
public partial class configurationCloudAws
6076+
{
6077+
6078+
private string accountIdField;
6079+
6080+
[System.Xml.Serialization.XmlAttributeAttribute()]
6081+
public string accountId
6082+
{
6083+
get
6084+
{
6085+
return this.accountIdField;
6086+
}
6087+
set
6088+
{
6089+
this.accountIdField = value;
6090+
}
6091+
}
6092+
6093+
#region Clone method
6094+
/// <summary>
6095+
/// Create a clone of this configurationCloudAws object
6096+
/// </summary>
6097+
public virtual configurationCloudAws Clone()
6098+
{
6099+
return ((configurationCloudAws)(this.MemberwiseClone()));
6100+
}
6101+
#endregion
6102+
}
6103+
60166104
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.6.0.20097")]
60176105
[System.SerializableAttribute()]
60186106
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:newrelic-config")]

src/Agent/NewRelic/Agent/Core/Config/Configuration.xsd

+24
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,30 @@
20252025
</xs:attribute>
20262026
</xs:complexType>
20272027
</xs:element>
2028+
2029+
<xs:element name="cloud" minOccurs="0" maxOccurs="1">
2030+
<xs:annotation>
2031+
<xs:documentation>
2032+
Provide linking data needed for certain cloud services.
2033+
</xs:documentation>
2034+
</xs:annotation>
2035+
2036+
<xs:complexType>
2037+
<xs:all>
2038+
<xs:element name="aws" minOccurs="0" maxOccurs="1">
2039+
<xs:complexType>
2040+
<xs:attribute name="accountId" type="xs:string">
2041+
<xs:annotation>
2042+
<xs:documentation>
2043+
The AWS Account ID used by this application.
2044+
</xs:documentation>
2045+
</xs:annotation>
2046+
</xs:attribute>
2047+
</xs:complexType>
2048+
</xs:element>
2049+
</xs:all>
2050+
</xs:complexType>
2051+
</xs:element>
20282052
</xs:all>
20292053

20302054
<xs:attribute name="agentEnabled" type="xs:boolean" default="true">

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

+17
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,23 @@ public bool CodeLevelMetricsEnabled
29782978

29792979
#endregion
29802980

2981+
#region Cloud
2982+
private string _awsAccountId;
2983+
public string AwsAccountId
2984+
{
2985+
get
2986+
{
2987+
if (_awsAccountId != null)
2988+
{
2989+
return _awsAccountId;
2990+
}
2991+
_awsAccountId = EnvironmentOverrides(_localConfiguration.cloud.aws.accountId, "NEW_RELIC_CLOUD_AWS_ACCOUNT_ID");
2992+
2993+
return _awsAccountId;
2994+
}
2995+
}
2996+
#endregion
2997+
29812998
public static bool GetLoggingEnabledValue(IEnvironment environment, configurationLog localLogConfiguration)
29822999
{
29833000
return EnvironmentOverrides(environment, localLogConfiguration.enabled, "NEW_RELIC_LOG_ENABLED");

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

+3
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ public ReportedConfiguration(IConfiguration configuration)
711711

712712
public string AzureFunctionResourceIdWithFunctionName(string functionName) => _configuration.AzureFunctionResourceIdWithFunctionName(functionName);
713713

714+
[JsonIgnore]
715+
public string AwsAccountId => _configuration.AwsAccountId;
716+
714717
[JsonProperty("gc_sampler_v2.enabled")]
715718
public bool GCSamplerV2Enabled => _configuration.GCSamplerV2Enabled;
716719

src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Configuration/IConfiguration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public interface IConfiguration
232232

233233
bool UtilizationDetectAzureFunction { get; }
234234

235+
string AwsAccountId { get; }
235236
bool GCSamplerV2Enabled { get; }
236237
}
237238
}

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

+84
Original file line numberDiff line numberDiff line change
@@ -4438,9 +4438,93 @@ public void AzureFunctionServiceName_ShouldReturnServiceName_WhenEnvironmentVari
44384438
// Assert
44394439
Assert.That(result, Is.EqualTo("some-service-name"));
44404440
}
4441+
#endregion
4442+
4443+
#region Cloud
4444+
[Test]
4445+
public void Cloud_Section_Parsing_And_Override()
4446+
{
4447+
string xmlString = """
4448+
<?xml version="1.0"?>
4449+
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
4450+
<cloud>
4451+
<aws accountId="123456789012" />
4452+
</cloud>
4453+
</configuration>
4454+
""";
4455+
var config = GenerateConfigFromXml(xmlString);
4456+
4457+
Assert.That(config.AwsAccountId, Is.EqualTo("123456789012"));
4458+
4459+
xmlString = """
4460+
<?xml version="1.0"?>
4461+
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
4462+
<cloud>
4463+
<aws />
4464+
</cloud>
4465+
</configuration>
4466+
""";
4467+
config = GenerateConfigFromXml(xmlString);
4468+
4469+
Assert.That(config.AwsAccountId, Is.Null);
4470+
4471+
xmlString = """
4472+
<?xml version="1.0"?>
4473+
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
4474+
<cloud>
4475+
</cloud>
4476+
</configuration>
4477+
""";
4478+
config = GenerateConfigFromXml(xmlString);
4479+
4480+
Assert.That(config.AwsAccountId, Is.Null);
4481+
4482+
xmlString = """
4483+
<?xml version="1.0"?>
4484+
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
4485+
</configuration>
4486+
""";
4487+
config = GenerateConfigFromXml(xmlString);
4488+
4489+
Assert.That(config.AwsAccountId, Is.Null);
4490+
4491+
// null from the last test, but env override should work
4492+
Mock.Arrange(() => _environment.GetEnvironmentVariableFromList("NEW_RELIC_CLOUD_AWS_ACCOUNT_ID")).Returns("444488881212");
4493+
4494+
Assert.That(config.AwsAccountId, Is.EqualTo("444488881212"));
4495+
4496+
// A second call should use the cached value
4497+
Assert.That(config.AwsAccountId, Is.EqualTo("444488881212"));
4498+
4499+
// If it exists in the config, the env variable should still override
4500+
xmlString = """
4501+
<?xml version="1.0"?>
4502+
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
4503+
<cloud>
4504+
<aws accountId="123456789012" />
4505+
</cloud>
4506+
</configuration>
4507+
""";
4508+
config = GenerateConfigFromXml(xmlString);
4509+
Assert.That(config.AwsAccountId, Is.EqualTo("444488881212"));
4510+
}
44414511

44424512
#endregion
44434513

4514+
private DefaultConfiguration GenerateConfigFromXml(string xml)
4515+
{
4516+
var root = new XmlRootAttribute { ElementName = "configuration", Namespace = "urn:newrelic-config" };
4517+
var serializer = new XmlSerializer(typeof(configuration), root);
4518+
4519+
configuration localConfiguration;
4520+
using (var reader = new StringReader(xml))
4521+
{
4522+
localConfiguration = serializer.Deserialize(reader) as configuration;
4523+
}
4524+
4525+
return new TestableDefaultConfiguration(_environment, localConfiguration, _serverConfig, _runTimeConfig, _securityPoliciesConfiguration, _bootstrapConfiguration, _processStatic, _httpRuntimeStatic, _configurationManagerStatic, _dnsStatic);
4526+
}
4527+
44444528
private void CreateDefaultConfiguration()
44454529
{
44464530
_defaultConfig = new TestableDefaultConfiguration(_environment, _localConfig, _serverConfig, _runTimeConfig, _securityPoliciesConfiguration, _bootstrapConfiguration, _processStatic, _httpRuntimeStatic, _configurationManagerStatic, _dnsStatic);

tests/Agent/UnitTests/Core.UnitTest/DataTransport/AgentSettingsTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ public void serializes_correctly()
360360
Assert.That(agentSettings.ServerlessFunctionName, Is.Null);
361361
Assert.That(agentSettings.ServerlessFunctionVersion, Is.Null);
362362
Assert.That(json, Is.EqualTo(expectedJson.Condense()));
363+
Assert.That(agentSettings.AwsAccountId, Is.Empty);
363364
});
364365
}
365366
}

tests/Agent/UnitTests/Core.UnitTest/DataTransport/ExhaustiveTestConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public IReadOnlyDictionary<string, string> GetAppSettings()
493493

494494
public string LoggingLevel => "info";
495495

496+
public string AwsAccountId => "";
496497
public bool GCSamplerV2Enabled => true;
497-
498498
}
499499
}

0 commit comments

Comments
 (0)