|
| 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 typing_extensions import override |
| 7 | + |
| 8 | +from amazon.base.contract_test_base import ContractTestBase |
| 9 | +from amazon.utils.application_signals_constants import ( |
| 10 | + AWS_LOCAL_OPERATION, |
| 11 | + AWS_LOCAL_SERVICE, |
| 12 | + AWS_REMOTE_OPERATION, |
| 13 | + AWS_REMOTE_RESOURCE_IDENTIFIER, |
| 14 | + AWS_REMOTE_RESOURCE_TYPE, |
| 15 | + AWS_REMOTE_SERVICE, |
| 16 | + AWS_SPAN_KIND, |
| 17 | +) |
| 18 | +from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue |
| 19 | +from opentelemetry.proto.metrics.v1.metrics_pb2 import ExponentialHistogramDataPoint, Metric |
| 20 | +from opentelemetry.proto.trace.v1.trace_pb2 import Span |
| 21 | +from opentelemetry.trace import StatusCode |
| 22 | + |
| 23 | +DATABASE_HOST: str = "mydb" |
| 24 | +DATABASE_USER: str = "root" |
| 25 | +DATABASE_PASSWORD: str = "example" |
| 26 | +DATABASE_NAME: str = "testdb" |
| 27 | + |
| 28 | + |
| 29 | +class DatabaseContractTestBase(ContractTestBase): |
| 30 | + @staticmethod |
| 31 | + def get_remote_service() -> str: |
| 32 | + return None |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def get_database_port() -> int: |
| 36 | + return None |
| 37 | + |
| 38 | + def get_remote_resource_identifier(self) -> str: |
| 39 | + return f"{DATABASE_NAME}|{DATABASE_HOST}|{self.get_database_port()}" |
| 40 | + |
| 41 | + @override |
| 42 | + def get_application_extra_environment_variables(self) -> Dict[str, str]: |
| 43 | + return { |
| 44 | + "DB_HOST": DATABASE_HOST, |
| 45 | + "DB_USER": DATABASE_USER, |
| 46 | + "DB_PASS": DATABASE_PASSWORD, |
| 47 | + "DB_NAME": DATABASE_NAME, |
| 48 | + } |
| 49 | + |
| 50 | + def assert_drop_table_succeeds(self) -> None: |
| 51 | + self.mock_collector_client.clear_signals() |
| 52 | + self.do_test_requests("drop_table", "GET", 200, 0, 0, sql_command="DROP TABLE") |
| 53 | + |
| 54 | + def assert_create_database_succeeds(self) -> None: |
| 55 | + self.mock_collector_client.clear_signals() |
| 56 | + self.do_test_requests("create_database", "GET", 200, 0, 0, sql_command="CREATE DATABASE") |
| 57 | + |
| 58 | + def assert_fault(self) -> None: |
| 59 | + self.mock_collector_client.clear_signals() |
| 60 | + self.do_test_requests("fault", "GET", 500, 0, 1, sql_command="SELECT DISTINCT") |
| 61 | + |
| 62 | + @override |
| 63 | + def _assert_aws_span_attributes(self, resource_scope_spans: List[ResourceScopeSpan], path: str, **kwargs) -> None: |
| 64 | + target_spans: List[Span] = [] |
| 65 | + for resource_scope_span in resource_scope_spans: |
| 66 | + # pylint: disable=no-member |
| 67 | + if resource_scope_span.span.kind == Span.SPAN_KIND_CLIENT: |
| 68 | + target_spans.append(resource_scope_span.span) |
| 69 | + |
| 70 | + self.assertEqual(len(target_spans), 1) |
| 71 | + self._assert_aws_attributes(target_spans[0].attributes, **kwargs) |
| 72 | + |
| 73 | + @override |
| 74 | + def _assert_semantic_conventions_span_attributes( |
| 75 | + self, resource_scope_spans: List[ResourceScopeSpan], method: str, path: str, status_code: int, **kwargs |
| 76 | + ) -> None: |
| 77 | + target_spans: List[Span] = [] |
| 78 | + for resource_scope_span in resource_scope_spans: |
| 79 | + # pylint: disable=no-member |
| 80 | + if resource_scope_span.span.kind == Span.SPAN_KIND_CLIENT: |
| 81 | + target_spans.append(resource_scope_span.span) |
| 82 | + |
| 83 | + self.assertEqual(target_spans[0].name, kwargs.get("sql_command").split()[0]) |
| 84 | + if status_code == 200: |
| 85 | + self.assertEqual(target_spans[0].status.code, StatusCode.UNSET.value) |
| 86 | + else: |
| 87 | + self.assertEqual(target_spans[0].status.code, StatusCode.ERROR.value) |
| 88 | + |
| 89 | + self._assert_semantic_conventions_attributes(target_spans[0].attributes, kwargs.get("sql_command")) |
| 90 | + |
| 91 | + def _assert_semantic_conventions_attributes(self, attributes_list: List[KeyValue], command: str) -> None: |
| 92 | + attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list) |
| 93 | + self.assertTrue(attributes_dict.get("db.statement").string_value.startswith(command)) |
| 94 | + self._assert_str_attribute(attributes_dict, "db.system", self.get_remote_service()) |
| 95 | + self._assert_str_attribute(attributes_dict, "db.name", DATABASE_NAME) |
| 96 | + self._assert_str_attribute(attributes_dict, "net.peer.name", DATABASE_HOST) |
| 97 | + self._assert_int_attribute(attributes_dict, "net.peer.port", self.get_database_port()) |
| 98 | + self.assertTrue("server.address" not in attributes_dict) |
| 99 | + self.assertTrue("server.port" not in attributes_dict) |
| 100 | + self.assertTrue("db.operation" not in attributes_dict) |
| 101 | + |
| 102 | + @override |
| 103 | + def _assert_aws_attributes(self, attributes_list: List[KeyValue], **kwargs) -> None: |
| 104 | + attributes_dict: Dict[str, AnyValue] = self._get_attributes_dict(attributes_list) |
| 105 | + self._assert_str_attribute(attributes_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name()) |
| 106 | + # InternalOperation as OTEL does not instrument the basic server we are using, so the client span is a local |
| 107 | + # root. |
| 108 | + self._assert_str_attribute(attributes_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 109 | + self._assert_str_attribute(attributes_dict, AWS_REMOTE_SERVICE, self.get_remote_service()) |
| 110 | + self._assert_str_attribute(attributes_dict, AWS_REMOTE_OPERATION, kwargs.get("sql_command")) |
| 111 | + self._assert_str_attribute(attributes_dict, AWS_REMOTE_RESOURCE_TYPE, "DB::Connection") |
| 112 | + self._assert_str_attribute( |
| 113 | + attributes_dict, AWS_REMOTE_RESOURCE_IDENTIFIER, self.get_remote_resource_identifier() |
| 114 | + ) |
| 115 | + # See comment above AWS_LOCAL_OPERATION |
| 116 | + self._assert_str_attribute(attributes_dict, AWS_SPAN_KIND, "LOCAL_ROOT") |
| 117 | + |
| 118 | + @override |
| 119 | + def _assert_metric_attributes( |
| 120 | + self, resource_scope_metrics: List[ResourceScopeMetric], metric_name: str, expected_sum: int, **kwargs |
| 121 | + ) -> None: |
| 122 | + target_metrics: List[Metric] = [] |
| 123 | + for resource_scope_metric in resource_scope_metrics: |
| 124 | + if resource_scope_metric.metric.name.lower() == metric_name.lower(): |
| 125 | + target_metrics.append(resource_scope_metric.metric) |
| 126 | + |
| 127 | + self.assertEqual(len(target_metrics), 1) |
| 128 | + target_metric: Metric = target_metrics[0] |
| 129 | + dp_list: List[ExponentialHistogramDataPoint] = target_metric.exponential_histogram.data_points |
| 130 | + |
| 131 | + self.assertEqual(len(dp_list), 2) |
| 132 | + dependency_dp: ExponentialHistogramDataPoint = dp_list[0] |
| 133 | + service_dp: ExponentialHistogramDataPoint = dp_list[1] |
| 134 | + if len(dp_list[1].attributes) > len(dp_list[0].attributes): |
| 135 | + dependency_dp = dp_list[1] |
| 136 | + service_dp = dp_list[0] |
| 137 | + attribute_dict: Dict[str, AnyValue] = self._get_attributes_dict(dependency_dp.attributes) |
| 138 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_SERVICE, self.get_application_otel_service_name()) |
| 139 | + # See comment on AWS_LOCAL_OPERATION in _assert_aws_attributes |
| 140 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 141 | + self._assert_str_attribute(attribute_dict, AWS_REMOTE_SERVICE, self.get_remote_service()) |
| 142 | + self._assert_str_attribute(attribute_dict, AWS_REMOTE_OPERATION, kwargs.get("sql_command")) |
| 143 | + self._assert_str_attribute(attribute_dict, AWS_REMOTE_RESOURCE_TYPE, "DB::Connection") |
| 144 | + self._assert_str_attribute( |
| 145 | + attribute_dict, AWS_REMOTE_RESOURCE_IDENTIFIER, self.get_remote_resource_identifier() |
| 146 | + ) |
| 147 | + self._assert_str_attribute(attribute_dict, AWS_SPAN_KIND, "CLIENT") |
| 148 | + self.check_sum(metric_name, dependency_dp.sum, expected_sum) |
| 149 | + |
| 150 | + attribute_dict: Dict[str, AnyValue] = self._get_attributes_dict(service_dp.attributes) |
| 151 | + # See comment on AWS_LOCAL_OPERATION in _assert_aws_attributes |
| 152 | + self._assert_str_attribute(attribute_dict, AWS_LOCAL_OPERATION, "InternalOperation") |
| 153 | + self._assert_str_attribute(attribute_dict, AWS_SPAN_KIND, "LOCAL_ROOT") |
| 154 | + self.check_sum(metric_name, service_dp.sum, expected_sum) |
0 commit comments