Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using static AWS.Distro.OpenTelemetry.AutoInstrumentation.AwsAttributeKeys;
using static AWS.Distro.OpenTelemetry.AutoInstrumentation.AwsSpanProcessingUtil;

namespace AWS.Distro.OpenTelemetry.AutoInstrumentation;
Expand All @@ -32,6 +33,8 @@
private const int FaultCodeLowerBound = 500;
private const int FaultCodeUpperBound = 599;

private const string Ec2MetadataApiIp = "169.254.169.254";

// Metric instruments
private Histogram<long> errorHistogram;
private Histogram<long> faultHistogram;
Expand Down Expand Up @@ -162,10 +165,21 @@
private void RecordMetrics(Activity span, ActivityTagsCollection attributes)
{
// Only record metrics if non-empty attributes are returned.
if (attributes.Count > 0)
if (attributes.Count > 0 && !IsEc2MetadataApiSpan(attributes))

Check warning on line 168 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Check warning on line 168 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 168 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 168 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

{
this.RecordErrorOrFault(span, attributes);
this.RecordLatency(span, attributes);
}
}

private bool IsEc2MetadataApiSpan(ActivityTagsCollection attributes)
{
if (attributes.TryGetValue(AttributeAWSRemoteService, out object? value) &&
value is string ip &&
ip == Ec2MetadataApiIp)
{
return true;
}

Check warning on line 182 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Check warning on line 182 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 182 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 182 in src/AWS.Distro.OpenTelemetry.AutoInstrumentation/AwsSpanMetricsProcessor.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ public void TestOnEndMetricsGenerationWithStatusDataOk()
this.ValidateMetricsGeneratedForStatusDataOk(600, ExpectedStatusMetric.NEITHER);
}

[Fact]
public void TestOnEndMetricsGenerationFromEc2MetadataApi()
{
Activity? spanDataMock = this.activitySource.StartActivity("test", ActivityKind.Client);
this.SetLatency(spanDataMock);
Dictionary<string, ActivityTagsCollection> expectAttributes = this.BuildEc2MetadataApiMetricAttributes();
this.generator.Setup(g => g.GenerateMetricAttributeMapFromSpan(spanDataMock, this.resource))
.Returns(expectAttributes);
this.awsSpanMetricsProcessor.OnEnd(spanDataMock);
this.VerifyHistogramRecords(expectAttributes, 0, 0);
}

private void ValidateMetricsGeneratedForAttributeStatusCode(
int? awsStatusCode, ExpectedStatusMetric expectedStatusMetric)
{
Expand Down Expand Up @@ -524,6 +536,13 @@ private Dictionary<string, ActivityTagsCollection> BuildMetricAttributes(bool co
return attributes;
}

private Dictionary<string, ActivityTagsCollection> BuildEc2MetadataApiMetricAttributes()
{
Dictionary<string, ActivityTagsCollection> attributes = new Dictionary<string, ActivityTagsCollection>();
attributes.Add(MetricAttributeGeneratorConstants.DependencyMetric, new ActivityTagsCollection([new KeyValuePair<string, object?>(AttributeAWSRemoteService, "169.254.169.254")]));
return attributes;
}

// Configure latency
private void SetLatency(Activity? spanDataMock, double latency = -1)
{
Expand Down
Loading