Skip to content

Commit

Permalink
Remove spans with ec2 metadata ip address from metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
harrryr committed Feb 7, 2025
1 parent da8291e commit 3daa168
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { AttributeValue, Attributes, Context, Histogram, SpanStatusCode } from '
import { Resource } from '@opentelemetry/resources';
import { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import { SEMATTRS_HTTP_STATUS_CODE } from '@opentelemetry/semantic-conventions';
import { AttributeMap, MetricAttributeGenerator } from './metric-attribute-generator';
import { AttributeMap, DEPENDENCY_METRIC, MetricAttributeGenerator } from './metric-attribute-generator';
import { ForceFlushFunction } from './aws-span-processing-util';
import { AWS_ATTRIBUTE_KEYS } from './aws-attribute-keys';

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

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

// Metric instruments
private errorHistogram: Histogram;
private faultHistogram: Histogram;
Expand Down Expand Up @@ -83,8 +88,10 @@ export class AwsSpanMetricsProcessor implements SpanProcessor {
public onEnd(span: ReadableSpan): void {
const attributeMap: AttributeMap = this.generator.generateMetricAttributeMapFromSpan(span, this.resource);

for (const attribute in attributeMap) {
this.recordMetrics(span, attributeMap[attribute]);
if (!this.isEc2MetadataApiSpan(attributeMap)) {
for (const attribute in attributeMap) {
this.recordMetrics(span, attributeMap[attribute]);
}
}
}

Expand Down Expand Up @@ -143,4 +150,8 @@ export class AwsSpanMetricsProcessor implements SpanProcessor {
public forceFlush(): Promise<void> {
return this.forceFlushFunction();
}
}

private isEc2MetadataApiSpan(attributeMap: AttributeMap): boolean {
return attributeMap[DEPENDENCY_METRIC]?.[AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE] === this.EC2_METADATA_API_IP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ describe('AwsSpanMetricsProcessorTest', () => {
validateMetricsGeneratedForStatusDataOk(600, ExpectedStatusMetric.NEITHER);
});

it('testOnEndMetricsGenerationFromEc2MetadataApi', () => {
const spanAttributes: Attributes = { [AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE]: '169.254.169.254' };
const readableSpanMock: ReadableSpan = buildReadableSpanMock(
spanAttributes,
SpanKind.CLIENT,
INVALID_SPAN_CONTEXT,
{ code: SpanStatusCode.UNSET }
);
const metricAttributesMap: AttributeMap = buildEc2MetadataApiMetricAttributes();
configureMocksForOnEnd(readableSpanMock, metricAttributesMap);
awsSpanMetricsProcessor.onEnd(readableSpanMock);
sinon.assert.notCalled(errorHistogramMockRecord);
sinon.assert.notCalled(faultHistogramMockRecord);
sinon.assert.notCalled(latencyHistogramMockRecord);
});

function buildSpanAttributes(containsAttribute: boolean): Attributes {
if (containsAttribute) {
return { 'original key': 'original value' };
Expand All @@ -421,6 +437,13 @@ describe('AwsSpanMetricsProcessorTest', () => {
return attributesMap;
}

function buildEc2MetadataApiMetricAttributes(): AttributeMap {
const attributesMap: AttributeMap = {};
const attributes: Attributes = { [AWS_ATTRIBUTE_KEYS.AWS_REMOTE_SERVICE]: '169.254.169.254' };
attributesMap[DEPENDENCY_METRIC] = attributes;
return attributesMap;
}

function buildReadableSpanMock(
spanAttributes: Attributes,
spanKind: SpanKind = SpanKind.SERVER,
Expand Down

0 comments on commit 3daa168

Please sign in to comment.