Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -17,6 +17,7 @@

import static io.opentelemetry.semconv.SemanticAttributes.HTTP_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.SemanticAttributes.HTTP_STATUS_CODE;
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_REMOTE_SERVICE;
import static software.amazon.opentelemetry.javaagent.providers.AwsSpanProcessingUtil.isKeyPresent;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -61,6 +62,10 @@ public final class AwsSpanMetricsProcessor implements SpanProcessor {
private static final int FAULT_CODE_LOWER_BOUND = 500;
private static final int FAULT_CODE_UPPER_BOUND = 599;

// EC2 Metadata API IP Address
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instancedata-inside-access
private final String EC2_METADATA_API_IP = "169.254.169.254";

// Metric instruments
private final LongHistogram errorHistogram;
private final LongHistogram faultHistogram;
Expand Down Expand Up @@ -117,8 +122,10 @@ public void onEnd(ReadableSpan span) {
Map<String, Attributes> attributeMap =
generator.generateMetricAttributeMapFromSpan(spanData, resource);

for (Map.Entry<String, Attributes> attribute : attributeMap.entrySet()) {
recordMetrics(span, spanData, attribute.getValue());
if (!isEc2MetadataSpan(attributeMap)) {
for (Map.Entry<String, Attributes> attribute : attributeMap.entrySet()) {
recordMetrics(span, spanData, attribute.getValue());
}
}
}

Expand Down Expand Up @@ -177,4 +184,18 @@ private void recordMetrics(ReadableSpan span, SpanData spanData, Attributes attr
recordLatency(span, attributes);
}
}

private boolean isEc2MetadataSpan(Map<String, Attributes> attributeMap) {
if (attributeMap.get(MetricAttributeGenerator.DEPENDENCY_METRIC) != null
&& attributeMap.get(MetricAttributeGenerator.DEPENDENCY_METRIC).get(AWS_REMOTE_SERVICE)
!= null
&& attributeMap
.get(MetricAttributeGenerator.DEPENDENCY_METRIC)
.get(AWS_REMOTE_SERVICE)
.equals(EC2_METADATA_API_IP)) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_REMOTE_SERVICE;
import static software.amazon.opentelemetry.javaagent.providers.MetricAttributeGenerator.DEPENDENCY_METRIC;
import static software.amazon.opentelemetry.javaagent.providers.MetricAttributeGenerator.SERVICE_METRIC;

Expand Down Expand Up @@ -378,6 +379,21 @@ public void testOnEndMetricsGenerationWithStatusDataOk() {
validateMetricsGeneratedForStatusDataOk(600L, ExpectedStatusMetric.NEITHER);
}

@Test
public void testOnEndMetricsGenerationFromEc2MetadataApi() {
Attributes spanAttributes = Attributes.of(AWS_REMOTE_SERVICE, "169.254.169.254");
ReadableSpan readableSpanMock =
buildReadableSpanMock(
spanAttributes, SpanKind.CLIENT, SpanContext.getInvalid(), StatusData.unset());
Map<String, Attributes> metricAttributesMap = buildEc2MetadataApiMetricAttributes();
configureMocksForOnEnd(readableSpanMock, metricAttributesMap);

awsSpanMetricsProcessor.onEnd(readableSpanMock);
verifyNoInteractions(errorHistogramMock);
verifyNoInteractions(faultHistogramMock);
verifyNoInteractions(latencyHistogramMock);
}

private static Attributes buildSpanAttributes(boolean containsAttribute) {
if (containsAttribute) {
return Attributes.of(AttributeKey.stringKey("original key"), "original value");
Expand All @@ -404,6 +420,14 @@ private static Map<String, Attributes> buildMetricAttributes(
return attributesMap;
}

private static Map<String, Attributes> buildEc2MetadataApiMetricAttributes() {
Map<String, Attributes> attributesMap = new HashMap<>();
Attributes attributes =
Attributes.of(AttributeKey.stringKey(AWS_REMOTE_SERVICE.toString()), "169.254.169.254");
attributesMap.put(MetricAttributeGenerator.DEPENDENCY_METRIC, attributes);
return attributesMap;
}

private static ReadableSpan buildReadableSpanMock(Attributes spanAttributes) {
return buildReadableSpanMock(spanAttributes, SpanKind.SERVER, null, StatusData.unset());
}
Expand Down
Loading