From da13c620f31eafb9ed2f17d76543207698c04420 Mon Sep 17 00:00:00 2001 From: chynesNR Date: Tue, 10 Dec 2024 15:13:27 -0800 Subject: [PATCH] Some cleanup and code coverage --- src/Agent/NewRelic/Agent/Core/Segments/Segment.cs | 14 +++++++------- .../UnitTests/CompositeTests/AgentApiTests.cs | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Core/Segments/Segment.cs b/src/Agent/NewRelic/Agent/Core/Segments/Segment.cs index 462d937395..0e6d995c26 100644 --- a/src/Agent/NewRelic/Agent/Core/Segments/Segment.cs +++ b/src/Agent/NewRelic/Agent/Core/Segments/Segment.cs @@ -38,7 +38,7 @@ public class Segment : IInternalSpan, ISegmentDataState public IAttributeDefinitions AttribDefs => _transactionSegmentState.AttribDefs; public string TypeName => MethodCallData.TypeName; - private SpanAttributeValueCollection _customAttribValues; + private SpanAttributeValueCollection _attribValues; public Segment(ITransactionSegmentState transactionSegmentState, MethodCallData methodCallData) { @@ -318,7 +318,7 @@ public TimeSpan ExclusiveDurationOrZero public SpanAttributeValueCollection GetAttributeValues() { - var attribValues = _customAttribValues ?? new SpanAttributeValueCollection(); + var attribValues = _attribValues ?? new SpanAttributeValueCollection(); AttribDefs.Duration.TrySetValue(attribValues, DurationOrZero); AttribDefs.NameForSpan.TrySetValue(attribValues, GetTransactionTraceName()); @@ -434,14 +434,14 @@ public ISegmentExperimental MakeLeaf() return this; } - private readonly object _customAttribValuesSyncRoot = new object(); + private readonly object _attribValuesSyncRoot = new object(); public ISpan AddCustomAttribute(string key, object value) { SpanAttributeValueCollection customAttribValues; - lock (_customAttribValuesSyncRoot) + lock (_attribValuesSyncRoot) { - customAttribValues = _customAttribValues ?? (_customAttribValues = new SpanAttributeValueCollection()); + customAttribValues = _attribValues ?? (_attribValues = new SpanAttributeValueCollection()); } AttribDefs.GetCustomAttributeForSpan(key).TrySetValue(customAttribValues, value); @@ -452,9 +452,9 @@ public ISpan AddCustomAttribute(string key, object value) public ISpan AddCloudSdkAttribute(string key, object value) { SpanAttributeValueCollection customAttribValues; - lock (_customAttribValuesSyncRoot) + lock (_attribValuesSyncRoot) { - customAttribValues = _customAttribValues ?? (_customAttribValues = new SpanAttributeValueCollection()); + customAttribValues = _attribValues ?? (_attribValues = new SpanAttributeValueCollection()); } AttribDefs.GetCloudSdkAttribute(key).TrySetValue(customAttribValues, value); diff --git a/tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs b/tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs index c0da43590b..206275898b 100644 --- a/tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs +++ b/tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs @@ -2211,10 +2211,10 @@ public void Test_GetLinkingMetadataReturnsValidValuesIfDTEnabled() #endregion GetLinkingMetadataCATS - #region Span Custom Attributes + #region Span Attributes [Test] - public void SpanCustomAttributes() + public void SpanAttributes() { var agentWrapperApi = _compositeTestAgent.GetAgent(); var dtm1 = DateTime.Now; @@ -2238,6 +2238,9 @@ public void SpanCustomAttributes() segment.AddCustomAttribute("key7", dtm2); segment.AddCustomAttribute("key8", null); segment.AddCustomAttribute("", dtm2); + segment.AddCloudSdkAttribute("cloud.platform", "aws_lambda"); + segment.AddCloudSdkAttribute("aws.region", "us-west-2"); + segment.AddCloudSdkAttribute("cloud.resource_id", "arn:aws:lambda:us-west-2:123456789012:function:myfunction"); var singleStringValue = new StringValues("avalue"); var multiStringValue = new StringValues(new[] { "onevalue", "twovalue", "threevalue" }); @@ -2257,6 +2260,13 @@ public void SpanCustomAttributes() new ExpectedAttribute(){ Key = "key9b", Value = "onevalue,twovalue,threevalue"} }; + var expectedCloudSdkAttributes = new[] + { + new ExpectedAttribute(){ Key = "cloud.platform", Value = "aws_lambda"}, + new ExpectedAttribute(){ Key = "aws.region", Value = "us-west-2"}, + new ExpectedAttribute(){ Key = "cloud.resource_id", Value = "arn:aws:lambda:us-west-2:123456789012:function:myfunction"}, + }; + var unexpectedAttributes = new[] { "key8", @@ -2275,6 +2285,7 @@ public void SpanCustomAttributes() ( () => Assert.That(allSpans, Has.Count.EqualTo(2)), () => SpanAssertions.HasAttributes(expectedAttributes, AttributeClassification.UserAttributes, testSpan), + () => SpanAssertions.HasAttributes(expectedCloudSdkAttributes, AttributeClassification.AgentAttributes, testSpan), () => SpanAssertions.DoesNotHaveAttributes(unexpectedAttributes, AttributeClassification.UserAttributes, testSpan) ); }