Skip to content

Commit

Permalink
Some cleanup and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chynesNR committed Dec 10, 2024
1 parent 8d41f54 commit da13c62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/Agent/NewRelic/Agent/Core/Segments/Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" });
Expand All @@ -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",
Expand All @@ -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)
);
}
Expand Down

0 comments on commit da13c62

Please sign in to comment.