From 8cbf56dec23b6af35269a8937e85ff916b2d0e2f Mon Sep 17 00:00:00 2001 From: pehlicd <65170388+pehlicd@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:44:34 +0100 Subject: [PATCH 1/2] feat(providers): added retry mechanism to google chat provider to overcome rate limiting --- .../google_chat_provider.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/keep/providers/google_chat_provider/google_chat_provider.py b/keep/providers/google_chat_provider/google_chat_provider.py index ba27d4624..a572147cc 100644 --- a/keep/providers/google_chat_provider/google_chat_provider.py +++ b/keep/providers/google_chat_provider/google_chat_provider.py @@ -1,4 +1,7 @@ +import http import os +import time + import pydantic import dataclasses import requests @@ -65,13 +68,23 @@ def _notify(self, message="", **kwargs: dict): if not message: raise ProviderException("Message is required") + def __send_message(url, body, headers, retries=3): + for _ in range(retries): + try: + resp = requests.post(url, json=body, headers=headers) + if resp.status_code == http.HTTPStatus.OK: + return resp + except requests.exceptions.RequestException as e: + self.logger.error(f"Failed to send message to Google Chat: {e}") + time.sleep(1) + payload = { "text": message, } - requestHeaders = {"Content-Type": "application/json; charset=UTF-8"} + request_headers = {"Content-Type": "application/json; charset=UTF-8"} - response = requests.post(webhook_url, json=payload, headers=requestHeaders) + response = __send_message(webhook_url, body=payload, headers=request_headers) if not response.ok: raise ProviderException( From b9cc737f60c034033b478bf6b17c83fa44f61054 Mon Sep 17 00:00:00 2001 From: pehlicd <65170388+pehlicd@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:02:35 +0100 Subject: [PATCH 2/2] feat(providers): added retry mechanism to google chat provider to overcome rate limiting --- .../google_chat_provider.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/keep/providers/google_chat_provider/google_chat_provider.py b/keep/providers/google_chat_provider/google_chat_provider.py index a572147cc..7eef362e6 100644 --- a/keep/providers/google_chat_provider/google_chat_provider.py +++ b/keep/providers/google_chat_provider/google_chat_provider.py @@ -69,14 +69,21 @@ def _notify(self, message="", **kwargs: dict): raise ProviderException("Message is required") def __send_message(url, body, headers, retries=3): - for _ in range(retries): + for attempt in range(retries): try: resp = requests.post(url, json=body, headers=headers) if resp.status_code == http.HTTPStatus.OK: return resp + + self.logger.warning(f"Attempt {attempt + 1} failed with status code {resp.status_code}") + except requests.exceptions.RequestException as e: - self.logger.error(f"Failed to send message to Google Chat: {e}") - time.sleep(1) + self.logger.error(f"Attempt {attempt + 1} failed: {e}") + + if attempt < retries - 1: + time.sleep(1) + + raise requests.exceptions.RequestException(f"Failed to notify message after {retries} attempts") payload = { "text": message, @@ -85,11 +92,8 @@ def __send_message(url, body, headers, retries=3): request_headers = {"Content-Type": "application/json; charset=UTF-8"} response = __send_message(webhook_url, body=payload, headers=request_headers) - - if not response.ok: - raise ProviderException( - f"Failed to notify message to Google Chat: {response.text}" - ) + if response.status_code != http.HTTPStatus.OK: + raise ProviderException(f"Failed to notify message to Google Chat: {response.text}") self.logger.debug("Alert message sent to Google Chat successfully") return "Alert message sent to Google Chat successfully"