From 17d1daea1381e7ae30c67340e59303a783fbaf64 Mon Sep 17 00:00:00 2001 From: liustve Date: Fri, 22 Nov 2024 00:43:25 +0000 Subject: [PATCH] regex attribute parsing issue fix --- .../test/amazon/base/contract_test_base.py | 4 +- .../test/amazon/botocore/botocore_test.py | 40 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/contract-tests/tests/test/amazon/base/contract_test_base.py b/contract-tests/tests/test/amazon/base/contract_test_base.py index 8d30cab4c..f274c9786 100644 --- a/contract-tests/tests/test/amazon/base/contract_test_base.py +++ b/contract-tests/tests/test/amazon/base/contract_test_base.py @@ -239,9 +239,9 @@ def _assert_metric_attributes( ): self.fail("Tests must implement this function") - def _is_valid_regex(self, pattern: str) -> bool: + def _is_valid_regex(self, pattern: str): try: re.compile(pattern) return True - except re.error: + except (re.error, StopIteration, RuntimeError, KeyError): return False diff --git a/contract-tests/tests/test/amazon/botocore/botocore_test.py b/contract-tests/tests/test/amazon/botocore/botocore_test.py index dfefd779e..12acf3908 100644 --- a/contract-tests/tests/test/amazon/botocore/botocore_test.py +++ b/contract-tests/tests/test/amazon/botocore/botocore_test.py @@ -927,30 +927,24 @@ def _assert_semantic_conventions_attributes( self._assert_int_attribute(attributes_dict, SpanAttributes.HTTP_STATUS_CODE, status_code) # TODO: botocore instrumentation is not respecting PEER_SERVICE # self._assert_str_attribute(attributes_dict, SpanAttributes.PEER_SERVICE, "backend:8080") - self._assert_specific_attributes(attributes_dict, request_specific_attributes) - self._assert_specific_attributes(attributes_dict, response_specific_attributes) - - def _assert_specific_attributes( - self, attributes_dict: Dict[str, AnyValue], specific_attributes: Dict[str, AnyValue] - ) -> None: - for key, value in specific_attributes.items(): - is_valid_regex = False - - try: - is_valid_regex = self._is_valid_regex(value) - except (StopIteration, RuntimeError, KeyError): - is_valid_regex = False - - if is_valid_regex: - self._assert_match_attribute(attributes_dict, key, value) - elif isinstance(value, str): - self._assert_str_attribute(attributes_dict, key, value) - elif isinstance(value, int): - self._assert_int_attribute(attributes_dict, key, value) - elif isinstance(value, float): - self._assert_float_attribute(attributes_dict, key, value) + for key, value in request_specific_attributes.items(): + self._assert_attribute(attributes_dict, key, value) + + for key, value in response_specific_attributes.items(): + self._assert_attribute(attributes_dict, key, value) + + def _assert_attribute(self, attributes_dict: Dict[str, AnyValue], key, value) -> None: + if isinstance(value, str): + if self._is_valid_regex(value): + self._assert_match_attribute(attributes_dict, key, value) else: - self._assert_array_value_ddb_table_name(attributes_dict, key, value) + self._assert_str_attribute(attributes_dict, key, value) + elif isinstance(value, int): + self._assert_int_attribute(attributes_dict, key, value) + elif isinstance(value, float): + self._assert_float_attribute(attributes_dict, key, value) + else: + self._assert_array_value_ddb_table_name(attributes_dict, key, value) @override def _assert_metric_attributes(