Skip to content

Commit

Permalink
Метрика для входящих писем (#92)
Browse files Browse the repository at this point in the history
* add incoming letter metric

* Fix code style issues with Black

* lint

---------

Co-authored-by: Lint Action <[email protected]>
  • Loading branch information
vzalygin and lint-action authored Oct 2, 2024
1 parent c9e7599 commit 1d181f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/client_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
forced_logout_metric,
relogin_metric,
revalidation_metric,
client_handler_errors_metric,
client_handler_error_metric,
incoming_letter_metric,
)

REVALIDATE_INTERVAL = timedelta(hours=5)
Expand Down Expand Up @@ -115,6 +116,7 @@ async def polling(self) -> None:
polling_context
)
for mail_header in mails:
incoming_letter_metric.inc()
log.info(f"new mail for {self.context.samoware_login}")
log.debug(f"email flags: {mail_header.flags}")
mail_body = await samoware_api.get_mail_body_by_id(
Expand Down Expand Up @@ -143,7 +145,7 @@ async def polling(self) -> None:
except asyncio.CancelledError:
return
except UnauthorizedError as error:
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
log.info(f"session for {self.context.samoware_login} expired")
samoware_password = self.db.get_password(self.context.telegram_id)
if samoware_password is None:
Expand All @@ -164,15 +166,15 @@ async def polling(self) -> None:
log.warning(
f"retry_count={retry_count}. ClientOSError. Probably Broken pipe. Retrying in {HTTP_RETRY_DELAY_SEC} seconds. {str(error)}"
)
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
retry_count += 1
await asyncio.sleep(HTTP_RETRY_DELAY_SEC)
except Exception as error:
log.exception("exception in client_handler")
log.warning(
f"retry_count={retry_count}. Retrying longpolling for {self.context.samoware_login} in {HTTP_RETRY_DELAY_SEC} seconds..."
)
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
retry_count += 1
await asyncio.sleep(HTTP_RETRY_DELAY_SEC)
finally:
Expand All @@ -195,7 +197,7 @@ async def login(self, samoware_password: str) -> bool:
return True
except UnauthorizedError as error:
log.info(f"unsuccessful login for user {self.context.samoware_login}")
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
return False
except asyncio.CancelledError:
log.info("login cancelled")
Expand All @@ -204,7 +206,7 @@ async def login(self, samoware_password: str) -> bool:
log.exception(
f"retry_count={retry_count}. exception on login. retrying in {HTTP_RETRY_DELAY_SEC}..."
)
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
retry_count += 1
await asyncio.sleep(HTTP_RETRY_DELAY_SEC)

Expand All @@ -228,7 +230,7 @@ async def revalidate(self) -> bool:
return True
except UnauthorizedError as error:
log.exception("UnauthorizedError on revalidation")
client_handler_errors_metric.labels(type=type(error).__name__).inc()
client_handler_error_metric.labels(type=type(error).__name__).inc()
return False

async def can_not_revalidate(self):
Expand Down
3 changes: 2 additions & 1 deletion src/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
logout_metric = Counter("logout", "Logout events metric")
forced_logout_metric = Counter("forced_logout", "Forced logout events metric")
client_handler_errors_metric = Counter(
client_handler_error_metric = Counter(
"client_handler_error", "Client handler error events metric", labelnames=["type"]
)
incoming_letter_metric = Counter("incoming_letter", "Incoming letter events metric")

0 comments on commit 1d181f3

Please sign in to comment.