Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/invoke-lambda-instrument…
Browse files Browse the repository at this point in the history
…ation' into feature/dynamodb-entity-relationship
  • Loading branch information
tippmar-nr committed Dec 11, 2024
2 parents 0a5cb25 + 4537d5d commit cad4613
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
18 changes: 9 additions & 9 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 @@ -451,13 +451,13 @@ public ISpan AddCustomAttribute(string key, object value)

public ISpan AddCloudSdkAttribute(string key, object value)
{
SpanAttributeValueCollection customAttribValues;
lock (_customAttribValuesSyncRoot)
SpanAttributeValueCollection attribValues;
lock (_attribValuesSyncRoot)
{
customAttribValues = _customAttribValues ?? (_customAttribValues = new SpanAttributeValueCollection());
attribValues = _attribValues ?? (_attribValues = new SpanAttributeValueCollection());
}

AttribDefs.GetCloudSdkAttribute(key).TrySetValue(customAttribValues, value);
AttribDefs.GetCloudSdkAttribute(key).TrySetValue(attribValues, value);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,8 @@ public string BuildFromPartialLambdaArn(string invocationName)
return null;
}

// The member Region cannot be blank (it has a default) so we don't need to check it here
region = !string.IsNullOrEmpty(region) ? region : Region;
if (string.IsNullOrEmpty(region))
{
return null;
}

if (!string.IsNullOrEmpty(alias))
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Agent/UnitTests/CompositeTests/AgentApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,5 +2280,50 @@ public void SpanCustomAttributes()
}

#endregion

#region Span Cloud SDK Attributes
[Test]
public void SpanCloudSdkAttributes()
{
var agentWrapperApi = _compositeTestAgent.GetAgent();
var dtm1 = DateTime.Now;
var dtm2 = DateTimeOffset.Now;

// ACT
var transaction = agentWrapperApi.CreateTransaction(
isWeb: true,
category: EnumNameCache<WebTransactionType>.GetName(WebTransactionType.Action),
transactionDisplayName: "name",
doNotTrackAsUnitOfWork: true);

var segment = agentWrapperApi.StartTransactionSegmentOrThrow("segment");

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 expectedAttributes = 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"},
};

segment.End();
transaction.End();

_compositeTestAgent.Harvest();

var allSpans = _compositeTestAgent.SpanEvents;
var testSpan = allSpans.LastOrDefault();

NrAssert.Multiple
(
() => Assert.That(allSpans, Has.Count.EqualTo(2)),
() => SpanAssertions.HasAttributes(expectedAttributes, AttributeClassification.AgentAttributes, testSpan)
);
}

#endregion
}
}
52 changes: 52 additions & 0 deletions tests/Agent/UnitTests/Core.UnitTest/Segments/SegmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,57 @@ public void DurationOrZero_ReturnsDuration_IfDurationIsSet()

Assert.That(duration, Is.EqualTo(TimeSpan.FromSeconds(1)));
}
[Test]
public void Misc_Segment_Setters()
{
var segment = new Segment(TransactionSegmentStateHelpers.GetItransactionSegmentState(), new MethodCallData("Type", "Method", 1));
Assert.That(segment.IsLeaf, Is.False);
segment.MakeLeaf();
Assert.That(segment.IsLeaf, Is.True);

segment.SetName("foo");
Assert.That(segment.GetTransactionTraceName(), Is.EqualTo("foo"));

Assert.That(segment.Combinable, Is.False);
segment.MakeCombinable();
Assert.That(segment.Combinable, Is.True);

Assert.That(segment.IsExternal, Is.False);
}

[Test]
public void NoOpSegment()
{
var segment = new NoOpSegment();
Assert.That(segment.IsDone, Is.True);
Assert.That(segment.IsValid, Is.False);
Assert.That(segment.IsDone, Is.True);
Assert.That(segment.DurationShouldBeDeductedFromParent, Is.False);
Assert.That(segment.IsLeaf, Is.False);
Assert.That(segment.IsExternal, Is.False);
Assert.That(segment.SpanId, Is.Null);
Assert.That(segment.SegmentData, Is.Not.Null);
Assert.That(segment.AttribDefs, Is.Not.Null);
Assert.That(segment.AttribValues, Is.Not.Null);
Assert.That(segment.TypeName, Is.EqualTo(string.Empty));
Assert.That(segment.UserCodeFunction, Is.EqualTo(string.Empty));
Assert.That(segment.UserCodeNamespace, Is.EqualTo(string.Empty));
Assert.That(segment.SegmentNameOverride, Is.Null);

Assert.DoesNotThrow(() => segment.End());
Assert.DoesNotThrow(() => segment.End(new Exception()));
Assert.DoesNotThrow(() => segment.EndStackExchangeRedis());
Assert.DoesNotThrow(() => segment.MakeCombinable());
Assert.DoesNotThrow(() => segment.MakeLeaf());
Assert.DoesNotThrow(() => segment.RemoveSegmentFromCallStack());
Assert.DoesNotThrow(() => segment.SetMessageBrokerDestination("destination"));
Assert.DoesNotThrow(() => segment.SetSegmentData(null));
Assert.DoesNotThrow(() => segment.AddCustomAttribute("key", "value"));
Assert.DoesNotThrow(() => segment.AddCloudSdkAttribute("key", "value"));
Assert.DoesNotThrow(() => segment.SetName("name"));
Assert.That(segment.GetCategory(), Is.EqualTo(string.Empty));
Assert.That(segment.DurationOrZero, Is.EqualTo(TimeSpan.Zero));

}
}
}

0 comments on commit cad4613

Please sign in to comment.