Skip to content

Commit

Permalink
Crop log uri (#63)
Browse files Browse the repository at this point in the history
* Crop log uri

* Review fix
  • Loading branch information
evgeny-stakewise authored Sep 27, 2023
1 parent 20bdb91 commit 3cc4a53
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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]}...'

async def _async_make_get_request_inner(self, endpoint_uri: str) -> dict[str, Any]:
for i, url in enumerate(self.base_urls):
try:
Expand All @@ -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,
)

0 comments on commit 3cc4a53

Please sign in to comment.