Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crop log uri #63

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
Loading