From 3d12e9ca249e9f8c76482eccca87c352b9766b99 Mon Sep 17 00:00:00 2001 From: Evgeny Gusarov Date: Wed, 27 Sep 2023 13:14:44 +0300 Subject: [PATCH 1/2] Crop log uri --- sw_utils/consensus.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/sw_utils/consensus.py b/sw_utils/consensus.py index 39284a9..9cb3c3a 100644 --- a/sw_utils/consensus.py +++ b/sw_utils/consensus.py @@ -64,10 +64,12 @@ def __init__( base_urls: list[str], timeout: int = 60, retry_timeout: int = 0, + log_uri_max_len: int | None = None, ) -> None: self.base_urls = base_urls self.timeout = timeout self.retry_timeout = retry_timeout + self.log_uri_max_len = log_uri_max_len or 100 super().__init__('') # hack origin base_url param async def get_validators_by_ids( @@ -139,7 +141,7 @@ def custom_before_log(retry_state: 'RetryCallState') -> None: if retry_state.attempt_number <= 1: return msg = 'Retrying consensus uri %s, attempt %s' - args = (endpoint_uri, retry_state.attempt_number) + args = (self._format_uri(endpoint_uri), retry_state.attempt_number) logger.log(logging.INFO, msg, *args) retry_decorator = retry_aiohttp_errors( @@ -150,6 +152,14 @@ def custom_before_log(retry_state: 'RetryCallState') -> None: return await self._async_make_get_request_inner(endpoint_uri) + def _format_uri(self, uri: str): + max_len = self.log_uri_max_len + + if len(uri) <= max_len: + return uri + + return f'{uri[:max_len]}...[cropped]' + async def _async_make_get_request_inner(self, endpoint_uri: str) -> dict[str, Any]: for i, url in enumerate(self.base_urls): try: @@ -174,5 +184,11 @@ def get_consensus_client( endpoints: list[str], timeout: int = 60, retry_timeout: int = 0, + log_uri_max_len: int | None = None, ) -> ExtendedAsyncBeacon: - return ExtendedAsyncBeacon(base_urls=endpoints, timeout=timeout, retry_timeout=retry_timeout) + return ExtendedAsyncBeacon( + base_urls=endpoints, + timeout=timeout, + retry_timeout=retry_timeout, + log_uri_max_len=log_uri_max_len, + ) From 61ad12464089c2fb89b5cf85d3702e2814218bc7 Mon Sep 17 00:00:00 2001 From: Evgeny Gusarov Date: Wed, 27 Sep 2023 13:31:24 +0300 Subject: [PATCH 2/2] Review fix --- sw_utils/consensus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sw_utils/consensus.py b/sw_utils/consensus.py index 9cb3c3a..23a4fa3 100644 --- a/sw_utils/consensus.py +++ b/sw_utils/consensus.py @@ -158,7 +158,7 @@ def _format_uri(self, uri: str): if len(uri) <= max_len: return uri - return f'{uri[:max_len]}...[cropped]' + return f'{uri[:max_len]}...' async def _async_make_get_request_inner(self, endpoint_uri: str) -> dict[str, Any]: for i, url in enumerate(self.base_urls):