diff --git a/contract-tests/tests/test/amazon/misc/configuration_test.py b/contract-tests/tests/test/amazon/misc/configuration_test.py index 50c4a5d59..647e0804e 100644 --- a/contract-tests/tests/test/amazon/misc/configuration_test.py +++ b/contract-tests/tests/test/amazon/misc/configuration_test.py @@ -1,21 +1,15 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from typing import Dict, List import time +from typing import List from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan +from requests import Response, request from typing_extensions import override -from opentelemetry.sdk.metrics.export import AggregationTemporality from amazon.base.contract_test_base import ContractTestBase -from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue -from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric -from opentelemetry.proto.trace.v1.trace_pb2 import Span -from requests import Response, request - from amazon.utils.app_signals_constants import ERROR_METRIC, FAULT_METRIC, LATENCY_METRIC - -import re +from opentelemetry.sdk.metrics.export import AggregationTemporality class ResourceAttributesTest(ContractTestBase): @@ -48,10 +42,13 @@ def _assert_metric_configuration(self, metrics: List[ResourceScopeMetric], metri for metric in metrics: if metric.metric.name == metric_name: self.assertIsNotNone(metric.metric.exponential_histogram) - self.assertEqual(metric.metric.exponential_histogram.aggregation_temporality, AggregationTemporality.DELTA) + self.assertEqual( + metric.metric.exponential_histogram.aggregation_temporality, AggregationTemporality.DELTA + ) def test_xray_id_format(self): - seen: List[int] + global seen + seen = [] for _ in range(20): address: str = self.application.get_container_host_ip() port: str = self.application.get_exposed_port(self.get_application_port()) @@ -64,40 +61,11 @@ def test_xray_id_format(self): resource_scope_spans: List[ResourceScopeSpan] = self.mock_collector_client.get_traces() target_span: ResourceScopeSpan = resource_scope_spans[0] self.assertEqual(target_span.span.name, "GET success") + + self.assertTrue(target_span.span.trace_id.hex() not in seen) + seen.append(target_span.span.trace_id.hex()) + trace_id_time_stamp_int: int = int(target_span.span.trace_id.hex()[:8], 16) self.assertGreater(trace_id_time_stamp_int, start_time_sec - 60) self.assertGreater(start_time_sec + 60, trace_id_time_stamp_int) self.mock_collector_client.clear_signals() - - - - def assert_resource_attributes(self, service_name): - resource_scope_spans: List[ResourceScopeSpan] = self.mock_collector_client.get_traces() - metrics: List[ResourceScopeMetric] = self.mock_collector_client.get_metrics( - {LATENCY_METRIC, ERROR_METRIC, FAULT_METRIC} - ) - target_spans: List[Span] = [] - for resource_scope_span in resource_scope_spans: - print("XYXYX") - print(resource_scope_span.span.attributes) - # pylint: disable=no-member - if resource_scope_span.span.name == "GET success": - target_spans.append(resource_scope_span.span) - - self.assertEqual(len(target_spans), 1) - attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(target_spans[0].attributes) - for key, value in self._get_k8s_attributes().items(): - self.assertEqual(attributes_dict[key], value) - self.assertEqual(attributes_dict["service.name"], service_name) - - target_metrics: List[Metric] = [] - for resource_scope_metric in metrics: - if resource_scope_metric.metric.name.lower() in ["Error", "Fault", "Latency"]: - target_metrics.append(resource_scope_metric.metric) - for target_metric in target_metrics: - dp_list: List[ExponentialHistogramDataPoint] = target_metric.exponential_histogram.data_points - self.assertEqual(len(dp_list), 1) - metric_attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(dp_list[0].attributes) - for key, value in self._get_k8s_attributes().items(): - self.assertEqual(metric_attributes_dict[key], value) - self.assertEqual(metric_attributes_dict["service.name"], service_name) \ No newline at end of file diff --git a/contract-tests/tests/test/amazon/misc/resource_attributes_test_base.py b/contract-tests/tests/test/amazon/misc/resource_attributes_test_base.py index 89402541a..3145cb684 100644 --- a/contract-tests/tests/test/amazon/misc/resource_attributes_test_base.py +++ b/contract-tests/tests/test/amazon/misc/resource_attributes_test_base.py @@ -3,17 +3,22 @@ from typing import Dict, List from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan +from requests import Response, request from typing_extensions import override from amazon.base.contract_test_base import ContractTestBase -from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue -from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric +from amazon.utils.app_signals_constants import ERROR_METRIC, FAULT_METRIC, LATENCY_METRIC +from opentelemetry.proto.common.v1.common_pb2 import AnyValue +from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric from opentelemetry.proto.trace.v1.trace_pb2 import Span -from requests import Response, request -from amazon.utils.app_signals_constants import ERROR_METRIC, FAULT_METRIC, LATENCY_METRIC -import re +def _get_k8s_attributes(): + return { + "k8s.namespace.name": "namespace-name", + "k8s.pod.name": "pod-name", + "k8s.deployment.name": "deployment-name", + } class ResourceAttributesTest(ContractTestBase): @@ -29,11 +34,6 @@ def get_application_wait_pattern(self) -> str: def get_application_extra_environment_variables(self): return {"DJANGO_SETTINGS_MODULE": "django_server.settings"} - def _get_k8s_attributes(self): - return {"k8s.namespace.name": "namespace-name", - "k8s.pod.name": "pod-name", - "k8s.deployment.name": "deployment-name"} - def do_misc_test_request(self, pattern): address: str = self.application.get_container_host_ip() port: str = self.application.get_exposed_port(self.get_application_port()) @@ -55,7 +55,7 @@ def assert_resource_attributes(self, service_name): self.assertEqual(len(target_spans), 1) attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(target_spans[0].resource.attributes) - for key, value in self._get_k8s_attributes().items(): + for key, value in _get_k8s_attributes().items(): self._assert_str_attribute(attributes_dict, key, value) self._assert_str_attribute(attributes_dict, "service.name", service_name) @@ -65,6 +65,6 @@ def assert_resource_attributes(self, service_name): target_metrics.append(resource_scope_metric.resource_metrics) for target_metric in target_metrics: metric_attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(target_metric.resource.attributes) - for key, value in self._get_k8s_attributes().items(): + for key, value in _get_k8s_attributes().items(): self._assert_str_attribute(metric_attributes_dict, key, value) - self._assert_str_attribute(metric_attributes_dict, "service.name", service_name) \ No newline at end of file + self._assert_str_attribute(metric_attributes_dict, "service.name", service_name) diff --git a/contract-tests/tests/test/amazon/misc/service_name_in_env_var_test.py b/contract-tests/tests/test/amazon/misc/service_name_in_env_var_test.py index 78cd967ab..f0b68419d 100644 --- a/contract-tests/tests/test/amazon/misc/service_name_in_env_var_test.py +++ b/contract-tests/tests/test/amazon/misc/service_name_in_env_var_test.py @@ -1,30 +1,18 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from typing import Dict, List - -from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan +from resource_attributes_test_base import ResourceAttributesTest, _get_k8s_attributes from typing_extensions import override -from resource_attributes_test_base import ResourceAttributesTest -from amazon.utils.app_signals_constants import AWS_LOCAL_OPERATION, AWS_LOCAL_SERVICE, AWS_SPAN_KIND -from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue -from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric -from opentelemetry.proto.trace.v1.trace_pb2 import Span -from opentelemetry.semconv.trace import SpanAttributes -from requests import Response, request class ServiceNameInResourceAttributesTest(ResourceAttributesTest): @override - def get_application_extra_environment_variables(self) -> str: - return {"DJANGO_SETTINGS_MODULE": "django_server.settings"} - - @override + # pylint: disable=no-self-use def get_application_otel_resource_attributes(self): pairlist = [] - for key, value in self._get_k8s_attributes().items(): + for key, value in _get_k8s_attributes().items(): pairlist.append(key + "=" + value) - return ','.join(pairlist) + return ",".join(pairlist) def test_service(self): self.do_misc_test_request("unknown_service") diff --git a/contract-tests/tests/test/amazon/misc/service_name_in_resource_attributes_test.py b/contract-tests/tests/test/amazon/misc/service_name_in_resource_attributes_test.py index 9efeebc8d..de1fa5377 100644 --- a/contract-tests/tests/test/amazon/misc/service_name_in_resource_attributes_test.py +++ b/contract-tests/tests/test/amazon/misc/service_name_in_resource_attributes_test.py @@ -1,27 +1,20 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from typing import Dict, List -from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan +from resource_attributes_test_base import ResourceAttributesTest, _get_k8s_attributes from typing_extensions import override -from resource_attributes_test_base import ResourceAttributesTest -from amazon.utils.app_signals_constants import AWS_LOCAL_OPERATION, AWS_LOCAL_SERVICE, AWS_SPAN_KIND -from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue -from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric -from opentelemetry.proto.trace.v1.trace_pb2 import Span -from opentelemetry.semconv.trace import SpanAttributes -from requests import Response, request class ServiceNameInResourceAttributesTest(ResourceAttributesTest): @override + # pylint: disable=no-self-use def get_application_otel_resource_attributes(self): pairlist = [] - for key, value in self._get_k8s_attributes().items(): + for key, value in _get_k8s_attributes().items(): pairlist.append(key + "=" + value) pairlist.append("service.name=service-name") - return ','.join(pairlist) + return ",".join(pairlist) def test_service(self): self.do_misc_test_request("service-name") diff --git a/contract-tests/tests/test/amazon/misc/unknown_service_name_test.py b/contract-tests/tests/test/amazon/misc/unknown_service_name_test.py index aa935c842..40e33afdf 100644 --- a/contract-tests/tests/test/amazon/misc/unknown_service_name_test.py +++ b/contract-tests/tests/test/amazon/misc/unknown_service_name_test.py @@ -1,31 +1,23 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from typing import Dict, List - -from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan +from resource_attributes_test_base import ResourceAttributesTest, _get_k8s_attributes from typing_extensions import override -from resource_attributes_test_base import ResourceAttributesTest -from amazon.utils.app_signals_constants import AWS_LOCAL_OPERATION, AWS_LOCAL_SERVICE, AWS_SPAN_KIND -from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue -from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric -from opentelemetry.proto.trace.v1.trace_pb2 import Span -from opentelemetry.semconv.trace import SpanAttributes -from requests import Response, request class ServiceNameInResourceAttributesTest(ResourceAttributesTest): @override + # pylint: disable=no-self-use def get_application_extra_environment_variables(self) -> str: - return {"DJANGO_SETTINGS_MODULE": "django_server.settings", - "OTEL_SERVICE_NAME": "service-name-test"} + return {"DJANGO_SETTINGS_MODULE": "django_server.settings", "OTEL_SERVICE_NAME": "service-name-test"} @override + # pylint: disable=no-self-use def get_application_otel_resource_attributes(self): pairlist = [] - for key, value in self._get_k8s_attributes().items(): + for key, value in _get_k8s_attributes().items(): pairlist.append(key + "=" + value) - return ','.join(pairlist) + return ",".join(pairlist) def test_service(self): self.do_misc_test_request("service-name-test")