|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +from mock_collector_client import ResourceScopeMetric, ResourceScopeSpan |
| 6 | +from testcontainers.postgres import PostgresContainer |
| 7 | +from typing_extensions import override |
| 8 | + |
| 9 | +from amazon.base.contract_test_base import NETWORK_NAME, ContractTestBase |
| 10 | +from amazon.utils.app_signals_constants import ( |
| 11 | + AWS_LOCAL_OPERATION, |
| 12 | + AWS_LOCAL_SERVICE, |
| 13 | + AWS_REMOTE_OPERATION, |
| 14 | + AWS_REMOTE_SERVICE, |
| 15 | + AWS_SPAN_KIND, |
| 16 | +) |
| 17 | +from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue |
| 18 | +from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric |
| 19 | +from opentelemetry.proto.trace.v1.trace_pb2 import Span |
| 20 | +from opentelemetry.trace import StatusCode |
| 21 | + |
| 22 | + |
| 23 | +class Psycopg2Test(ContractTestBase): |
| 24 | + @override |
| 25 | + @classmethod |
| 26 | + def set_up_dependency_container(cls) -> None: |
| 27 | + cls.container = ( |
| 28 | + PostgresContainer(user="dbuser", password="example", dbname="postgres") |
| 29 | + .with_kwargs(network=NETWORK_NAME) |
| 30 | + .with_name("mydb") |
| 31 | + ) |
| 32 | + cls.container.start() |
| 33 | + |
| 34 | + @override |
| 35 | + @classmethod |
| 36 | + def tear_down_dependency_container(cls) -> None: |
| 37 | + cls.container.stop() |
| 38 | + |
| 39 | + @override |
| 40 | + def get_application_extra_environment_variables(self) -> Dict[str, str]: |
| 41 | + return { |
| 42 | + "DB_HOST": "mydb", |
| 43 | + "DB_USER": "dbuser", |
| 44 | + "DB_PASS": "example", |
| 45 | + "DB_NAME": "postgres", |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + def get_application_image_name(self) -> str: |
| 50 | + return "aws-appsignals-tests-psycopg2-app" |
| 51 | + |
| 52 | + def test_success(self) -> None: |
| 53 | + self.mock_collector_client.clear_signals() |
| 54 | + self.do_test_requests("success", "GET", 200, 0, 0, sql_command="SELECT") |
| 55 | + |
| 56 | + def test_fault(self) -> None: |
| 57 | + self.mock_collector_client.clear_signals() |
| 58 | + self.do_test_requests("fault", "GET", 500, 0, 1, sql_command="SELECT DISTINCT") |
| 59 | + |
| 60 | + @override |
| 61 | + def _assert_aws_span_attributes(self, resource_scope_spans: List[ResourceScopeSpan], path: str, **kwargs) -> None: |
| 62 | + target_spans: List[Span] = [] |
| 63 | + for resource_scope_span in resource_scope_spans: |
| 64 | + # pylint: disable=no-member |
| 65 | + if resource_scope_span.span.kind == Span.SPAN_KIND_CLIENT: |
| 66 | + target_spans.append(resource_scope_span.span) |
| 67 | + |
| 68 | + self.assertEqual(len(target_spans), 1) |
| 69 | + self._assert_aws_attributes(target_spans[0].attributes, **kwargs) |
| 70 | + |
| 71 | + @override |
| 72 | + def _assert_aws_attributes(self, attributes_list: List[KeyValue], **kwargs) -> None: |
| 73 | + attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list) |
| 74 | + self._assert_str_attribute(attributes_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name()) |
| 75 | + # InternalOperation as OTEL does not instrument the basic server we are using, so the client span is a local |
| 76 | + # root. |
| 77 | + self._assert_str_attribute(attributes_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 78 | + self._assert_str_attribute(attributes_dict, AWS_REMOTE_SERVICE, "postgresql") |
| 79 | + command: str = kwargs.get("sql_command") |
| 80 | + self._assert_str_attribute(attributes_dict, AWS_REMOTE_OPERATION, f"{command}") |
| 81 | + # See comment above AWS_LOCAL_OPERATION |
| 82 | + self._assert_str_attribute(attributes_dict, AWS_SPAN_KIND, "LOCAL_ROOT") |
| 83 | + |
| 84 | + def _get_attributes_dict(self, attributes_list: List[KeyValue]) -> Dict[str, AnyValue]: |
| 85 | + attributes_dict: Dict[str, AnyValue] = {} |
| 86 | + for attribute in attributes_list: |
| 87 | + key: str = attribute.key |
| 88 | + value: AnyValue = attribute.value |
| 89 | + if key in attributes_dict: |
| 90 | + old_value: AnyValue = attributes_dict[key] |
| 91 | + self.fail(f"Attribute {key} unexpectedly duplicated. Value 1: {old_value} Value 2: {value}") |
| 92 | + attributes_dict[key] = value |
| 93 | + return attributes_dict |
| 94 | + |
| 95 | + @override |
| 96 | + def _assert_semantic_conventions_span_attributes( |
| 97 | + self, resource_scope_spans: List[ResourceScopeSpan], method: str, path: str, status_code: int, **kwargs |
| 98 | + ) -> None: |
| 99 | + target_spans: List[Span] = [] |
| 100 | + for resource_scope_span in resource_scope_spans: |
| 101 | + # pylint: disable=no-member |
| 102 | + if resource_scope_span.span.kind == Span.SPAN_KIND_CLIENT: |
| 103 | + target_spans.append(resource_scope_span.span) |
| 104 | + |
| 105 | + self.assertEqual(target_spans[0].name, kwargs.get("sql_command").split()[0]) |
| 106 | + if status_code == 200: |
| 107 | + self.assertEqual(target_spans[0].status.code, StatusCode.UNSET.value) |
| 108 | + else: |
| 109 | + self.assertEqual(target_spans[0].status.code, StatusCode.ERROR.value) |
| 110 | + |
| 111 | + self._assert_semantic_conventions_attributes(target_spans[0].attributes, kwargs.get("sql_command")) |
| 112 | + |
| 113 | + def _assert_semantic_conventions_attributes(self, attributes_list: List[KeyValue], command: str) -> None: |
| 114 | + attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list) |
| 115 | + self.assertTrue(attributes_dict.get("db.statement").string_value.startswith(command)) |
| 116 | + self._assert_str_attribute(attributes_dict, "db.system", "postgresql") |
| 117 | + self._assert_str_attribute(attributes_dict, "db.name", "postgres") |
| 118 | + self.assertTrue("db.operation" not in attributes_dict) |
| 119 | + |
| 120 | + @override |
| 121 | + def _assert_metric_attributes( |
| 122 | + self, resource_scope_metrics: List[ResourceScopeMetric], metric_name: str, expected_sum: int, **kwargs |
| 123 | + ) -> None: |
| 124 | + target_metrics: List[Metric] = [] |
| 125 | + for resource_scope_metric in resource_scope_metrics: |
| 126 | + if resource_scope_metric.metric.name.lower() == metric_name.lower(): |
| 127 | + target_metrics.append(resource_scope_metric.metric) |
| 128 | + |
| 129 | + self.assertEqual(len(target_metrics), 1) |
| 130 | + target_metric: Metric = target_metrics[0] |
| 131 | + dp_list: List[ExponentialHistogramDataPoint] = target_metric.exponential_histogram.data_points |
| 132 | + |
| 133 | + self.assertEqual(len(dp_list), 2) |
| 134 | + dependency_dp: ExponentialHistogramDataPoint = dp_list[0] |
| 135 | + service_dp: ExponentialHistogramDataPoint = dp_list[1] |
| 136 | + if len(dp_list[1].attributes) > len(dp_list[0].attributes): |
| 137 | + dependency_dp = dp_list[1] |
| 138 | + service_dp = dp_list[0] |
| 139 | + attribute_dict: Dict[str, AnyValue] = self._get_attributes_dict(dependency_dp.attributes) |
| 140 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name()) |
| 141 | + # See comment on AWS_LOCAL_OPERATION in _assert_aws_attributes |
| 142 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 143 | + self._assert_str_attribute(attribute_dict, AWS_REMOTE_SERVICE, "postgresql") |
| 144 | + self._assert_str_attribute(attribute_dict, AWS_REMOTE_OPERATION, kwargs.get("sql_command")) |
| 145 | + self._assert_str_attribute(attribute_dict, AWS_SPAN_KIND, "CLIENT") |
| 146 | + self.check_sum(metric_name, dependency_dp.sum, expected_sum) |
| 147 | + |
| 148 | + attribute_dict: Dict[str, AnyValue] = self._get_attributes_dict(service_dp.attributes) |
| 149 | + # See comment on AWS_LOCAL_OPERATION in _assert_aws_attributes |
| 150 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 151 | + self._assert_str_attribute(attribute_dict, AWS_SPAN_KIND, "LOCAL_ROOT") |
| 152 | + self.check_sum(metric_name, service_dp.sum, expected_sum) |
0 commit comments