Skip to content

Commit 5aa970e

Browse files
authored
Merge branch 'main' into sigv4_support
2 parents c7d8410 + 0c3ade0 commit 5aa970e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing_extensions import override
66

7+
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_REMOTE_SERVICE
78
from amazon.opentelemetry.distro.metric_attribute_generator import MetricAttributeGenerator
89
from opentelemetry.context import Context
910
from opentelemetry.metrics import Histogram
@@ -21,6 +22,11 @@
2122
_FAULT_CODE_UPPER_BOUND: int = 599
2223

2324

25+
# EC2 Metadata API IP Address
26+
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instancedata-inside-access
27+
_EC2_METADATA_API_IP: str = "169.254.169.254"
28+
29+
2430
class AwsSpanMetricsProcessor(SpanProcessor):
2531
"""AwsSpanMetricsProcessor is SpanProcessor that generates metrics from spans
2632
@@ -79,6 +85,7 @@ def on_end(self, span: ReadableSpan) -> None:
7985
attribute_dict: Dict[str, BoundedAttributes] = self._generator.generate_metric_attributes_dict_from_span(
8086
span, self._resource
8187
)
88+
8289
for attributes in attribute_dict.values():
8390
self._record_metrics(span, attributes)
8491

@@ -93,7 +100,7 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool:
93100

94101
def _record_metrics(self, span: ReadableSpan, attributes: BoundedAttributes) -> None:
95102
# Only record metrics if non-empty attributes are returned.
96-
if len(attributes) > 0:
103+
if len(attributes) > 0 and not _is_ec2_metadata_api_span(attributes):
97104
self._record_error_or_fault(span, attributes)
98105
self._record_latency(span, attributes)
99106

@@ -130,3 +137,7 @@ def _is_not_error_or_fault(http_status_code: int) -> bool:
130137
or http_status_code < _ERROR_CODE_LOWER_BOUND
131138
or http_status_code > _FAULT_CODE_UPPER_BOUND
132139
)
140+
141+
142+
def _is_ec2_metadata_api_span(attributes: BoundedAttributes) -> bool:
143+
return attributes.get(AWS_REMOTE_SERVICE) == _EC2_METADATA_API_IP

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_span_metrics_processor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import TestCase
66
from unittest.mock import MagicMock, call
77

8+
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_REMOTE_SERVICE
89
from amazon.opentelemetry.distro._aws_span_processing_util import (
910
should_generate_dependency_metric_attributes,
1011
should_generate_service_metric_attributes,
@@ -238,6 +239,15 @@ def test_on_end_metrics_generation_with_status_data_ok(self):
238239
self._validate_metrics_generated_for_status_data_ok(599, self.ExpectedStatusMetric.FAULT)
239240
self._validate_metrics_generated_for_status_data_ok(600, self.ExpectedStatusMetric.NEITHER)
240241

242+
def test_on_end_metrics_generation_from_ec2_metadata_api(self):
243+
span_attributes: Attributes = {AWS_REMOTE_SERVICE: "169.254.169.254"}
244+
span: ReadableSpan = _build_readable_span_mock(span_attributes)
245+
metric_attributes_dict = _build_ec2_metadata_api_metric_attributes()
246+
self._configure_mock_for_on_end(span, metric_attributes_dict)
247+
248+
self.aws_span_metrics_processor.on_end(span)
249+
self._verify_histogram_record(metric_attributes_dict, 0, 0)
250+
241251
def _configure_mock_for_on_end(self, span: Span, attribute_map: {str: Attributes}):
242252
def generate_m_a_from_span_side_effect(input_span, resource):
243253
if input_span == span and resource == self.test_resource:
@@ -373,3 +383,10 @@ def _build_metric_attributes(contain_attributes: bool, span: Span) -> Attributes
373383
attributes = {"new dependency key": "new dependency value"}
374384
attribute_map[DEPENDENCY_METRIC] = attributes
375385
return attribute_map
386+
387+
388+
def _build_ec2_metadata_api_metric_attributes() -> Attributes:
389+
attribute_map: Attributes = {}
390+
attributes = {AWS_REMOTE_SERVICE: "169.254.169.254"}
391+
attribute_map[DEPENDENCY_METRIC] = attributes
392+
return attribute_map

0 commit comments

Comments
 (0)