Skip to content

Commit 7a766a8

Browse files
authored
Remove spans with ec2 metadata ip address from metrics (#150)
*Issue #, if available:* ADOT SDK resource detectors by default have enabled a few AWS resource detector which will call EC2 metadata API endpoints. These activities have been captured by auto-instrumentation and generated AppSignals metrics. *Description of changes:* Suppress AwsSpanMetricsProcessor from generating metrics when the RemoteService points to 169.254.169.254 *Testing*: Tested using Java Adot: aws-observability/aws-otel-java-instrumentation#1015 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 8f11e11 commit 7a766a8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-metrics-processor.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base
77
import { SEMATTRS_HTTP_STATUS_CODE } from '@opentelemetry/semantic-conventions';
88
import { AttributeMap, MetricAttributeGenerator } from './metric-attribute-generator';
99
import { ForceFlushFunction } from './aws-span-processing-util';
10+
import { AWS_ATTRIBUTE_KEYS } from './aws-attribute-keys';
1011

1112
/**
1213
* This processor will generate metrics based on span data. It depends on a
@@ -33,6 +34,10 @@ export class AwsSpanMetricsProcessor implements SpanProcessor {
3334
private FAULT_CODE_LOWER_BOUND: number = 500;
3435
private FAULT_CODE_UPPER_BOUND: number = 599;
3536

37+
// EC2 Metadata API IP Address
38+
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instancedata-inside-access
39+
private EC2_METADATA_API_IP: string = '169.254.169.254';
40+
3641
// Metric instruments
3742
private errorHistogram: Histogram;
3843
private faultHistogram: Histogram;
@@ -130,7 +135,7 @@ export class AwsSpanMetricsProcessor implements SpanProcessor {
130135

131136
private recordMetrics(span: ReadableSpan, attributes: Attributes): void {
132137
// Only record metrics if non-empty attributes are returned.
133-
if (Object.keys(attributes).length > 0) {
138+
if (Object.keys(attributes).length > 0 && !this.isEc2MetadataApiSpan(attributes)) {
134139
this.recordErrorOrFault(span, attributes);
135140
this.recordLatency(span, attributes);
136141
}
@@ -143,4 +148,8 @@ export class AwsSpanMetricsProcessor implements SpanProcessor {
143148
public forceFlush(): Promise<void> {
144149
return this.forceFlushFunction();
145150
}
151+
152+
private isEc2MetadataApiSpan(attributes: Attributes): boolean {
153+
return attributes[AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE] === this.EC2_METADATA_API_IP;
154+
}
146155
}

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-metrics-processor.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ describe('AwsSpanMetricsProcessorTest', () => {
397397
validateMetricsGeneratedForStatusDataOk(600, ExpectedStatusMetric.NEITHER);
398398
});
399399

400+
it('testOnEndMetricsGenerationFromEc2MetadataApi', () => {
401+
const spanAttributes: Attributes = { [AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE]: '169.254.169.254' };
402+
const readableSpanMock: ReadableSpan = buildReadableSpanMock(
403+
spanAttributes,
404+
SpanKind.CLIENT,
405+
INVALID_SPAN_CONTEXT,
406+
{ code: SpanStatusCode.UNSET }
407+
);
408+
const metricAttributesMap: AttributeMap = buildEc2MetadataApiMetricAttributes();
409+
configureMocksForOnEnd(readableSpanMock, metricAttributesMap);
410+
awsSpanMetricsProcessor.onEnd(readableSpanMock);
411+
sinon.assert.notCalled(errorHistogramMockRecord);
412+
sinon.assert.notCalled(faultHistogramMockRecord);
413+
sinon.assert.notCalled(latencyHistogramMockRecord);
414+
});
415+
400416
function buildSpanAttributes(containsAttribute: boolean): Attributes {
401417
if (containsAttribute) {
402418
return { 'original key': 'original value' };
@@ -421,6 +437,13 @@ describe('AwsSpanMetricsProcessorTest', () => {
421437
return attributesMap;
422438
}
423439

440+
function buildEc2MetadataApiMetricAttributes(): AttributeMap {
441+
const attributesMap: AttributeMap = {};
442+
const attributes: Attributes = { [AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE]: '169.254.169.254' };
443+
attributesMap[DEPENDENCY_METRIC] = attributes;
444+
return attributesMap;
445+
}
446+
424447
function buildReadableSpanMock(
425448
spanAttributes: Attributes,
426449
spanKind: SpanKind = SpanKind.SERVER,

0 commit comments

Comments
 (0)