Skip to content

Commit

Permalink
regex attribute parsing issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liustve committed Nov 22, 2024
1 parent e76f607 commit 17d1dae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions contract-tests/tests/test/amazon/base/contract_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 17 additions & 23 deletions contract-tests/tests/test/amazon/botocore/botocore_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 17d1dae

Please sign in to comment.