66using OpenTelemetry ;
77using OpenTelemetry . Metrics ;
88using OpenTelemetry . Resources ;
9+ using static AWS . Distro . OpenTelemetry . AutoInstrumentation . AwsAttributeKeys ;
910using static AWS . Distro . OpenTelemetry . AutoInstrumentation . AwsSpanProcessingUtil ;
1011
1112namespace AWS . Distro . OpenTelemetry . AutoInstrumentation ;
@@ -32,6 +33,8 @@ public class AwsSpanMetricsProcessor : BaseProcessor<Activity>
3233 private const int FaultCodeLowerBound = 500 ;
3334 private const int FaultCodeUpperBound = 599 ;
3435
36+ private const string Ec2MetadataApiIp = "169.254.169.254" ;
37+
3538 // Metric instruments
3639 private Histogram < long > errorHistogram ;
3740 private Histogram < long > faultHistogram ;
@@ -74,9 +77,11 @@ public override void OnEnd(Activity activity)
7477 Dictionary < string , ActivityTagsCollection > attributeDictionary =
7578 this . generator . GenerateMetricAttributeMapFromSpan ( activity , this . resource ) ;
7679
77- foreach ( KeyValuePair < string , ActivityTagsCollection > attribute in attributeDictionary )
78- {
79- this . RecordMetrics ( activity , attribute . Value ) ;
80+ if ( ! IsEc2MetadataApiSpan ( attributeDictionary ) ) {
81+ foreach ( KeyValuePair < string , ActivityTagsCollection > attribute in attributeDictionary )
82+ {
83+ this . RecordMetrics ( activity , attribute . Value ) ;
84+ }
8085 }
8186 }
8287
@@ -168,4 +173,17 @@ private void RecordMetrics(Activity span, ActivityTagsCollection attributes)
168173 this . RecordLatency ( span , attributes ) ;
169174 }
170175 }
176+
177+ private bool IsEc2MetadataApiSpan ( Dictionary < string , ActivityTagsCollection > attributeDict )
178+ {
179+ if ( attributeDict . TryGetValue ( MetricAttributeGeneratorConstants . DependencyMetric , out ActivityTagsCollection ? activityTagsCollection ) &&
180+ activityTagsCollection != null &&
181+ activityTagsCollection . TryGetValue ( AttributeAWSRemoteService , out object ? value ) &&
182+ value is string ip &&
183+ ip == Ec2MetadataApiIp )
184+ {
185+ return true ;
186+ }
187+ return false ;
188+ }
171189}
0 commit comments